public FireSide ShouldFire(Transform boat) { RaycastHit hit; FireSide result = FireSide.NONE; Vector3 leftDirection = -boat.right * attackRange; Vector3 rightDirection = boat.right * attackRange; if (Physics.Raycast(boat.position, leftDirection, out hit, attackRange)) { BoatAgent target = hit.transform.gameObject.GetComponent <BoatAgent>(); if (target && target.Team == Team.PLAYER_1) { result = FireSide.LEFT; } if (debug) { Debug.DrawLine(boat.position, hit.point, Color.green, Time.deltaTime); } } else { if (debug) { Debug.DrawRay(boat.position, leftDirection * attackRange, Color.red, Time.deltaTime); } } if (Physics.Raycast(boat.position, rightDirection, out hit, attackRange)) { BoatAgent target = hit.transform.gameObject.GetComponent <BoatAgent>(); if (target && target.Team == Team.PLAYER_1) { if (result == FireSide.NONE) { result = FireSide.RIGHT; } else { result = FireSide.BOTH; } } if (debug) { Debug.DrawLine(boat.position, hit.point, Color.green, Time.deltaTime); } } else { if (debug) { Debug.DrawRay(boat.position, rightDirection * attackRange, Color.red, Time.deltaTime); } } return(result); }
public FireSide ShouldFire() { FireSide result = FireSide.NONE; int nbBoat = transform.childCount - 1; int nbLeft = 0; int nbRight = 0; Transform currentBoat; int minShotSelf = (int)Mathf.Max(Mathf.Ceil((float)nbBoat / (float)fireRatio), 1); int minShotTarget = (int)Mathf.Max(Mathf.Ceil((float)currentTarget.RemainingBoats / (float)fireRatio), 1); int minShot = Mathf.Min(minShotSelf, minShotTarget); for (int i = 0; i != nbBoat; i++) { currentBoat = transform.GetChild(i); FireSide boatFireSide = ShouldFire(currentBoat); switch (boatFireSide) { case FireSide.NONE: break; case FireSide.LEFT: nbLeft++; break; case FireSide.RIGHT: nbRight++; break; case FireSide.BOTH: nbLeft++; nbRight++; break; } } if (nbLeft >= minShot) { result = FireSide.LEFT; } if (nbRight >= minShot) { if (result == FireSide.NONE) { result = FireSide.RIGHT; } else { result = FireSide.BOTH; } } return(result); }
public void Fire(FireSide side) { switch (side) { case FireSide.RIGHT: FireRight(); break; case FireSide.LEFT: FireLeft(); break; case FireSide.BOTH: FireRight(); FireLeft(); break; } }
// OnStateEnter is called when a transition starts and the state machine starts to evaluate this state //override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) //{ // //} // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { AIFleetController controller = animator.GetComponent <AIFleetController>(); if (controller.ValidateTarget() == false) { animator.Play("Patrol"); } else if (!controller.IsInAttackRange()) { animator.Play("Chase"); } else { controller.ChaseTarget(); FireSide fireSide = controller.ShouldFire(); if (fireSide != FireSide.NONE) { controller.Fire(fireSide); animator.Play("Chase"); } } }