示例#1
0
    private IEnumerator AttackState() //Doesnt work
    {
        while (lifeStates == LifeStates.Attack)
        {
            stateText.text = "Predator State: " + LifeStates.Attack.ToString();

            foreach (FlockAgent predatorAgent in flock.agents)
            {
                Vector2 velocity = attackBehavior.CalculateMove(predatorAgent, GetNearbyObjects(predatorAgent), flock);
                predatorAgent.Move(velocity);

                foreach (FlockAgent preyAgent in prey.GetFlock().agents) //go through list of agents in prey
                {
                    // check if prey is in range of attack range (1f)
                    if (Vector3.Distance(preyAgent.transform.position, predatorAgent.transform.position) < attackRadius)
                    {
                        Destroy(prey.gameObject);
                        print("Prey are being eaten");

                        if (prey.GetFlock().agents.Count <= 0) //if predators have eaten all prey
                        {
                            print("Prey are gone");
                            lifeStates = LifeStates.Wander; //go back to wander state
                        }
                    }
                }
            }
            print("Predator are eating prey");
        }
        NextState();
        yield return(null);
    }