Inheritance: SteeringBehavior
        public DynamicGameEntity(EntityManager entityManager, Vector2 pos) : base(entityManager, pos)
        {
            Velocity = new Vector2();

            Mass              = 1.0F;
            MaxSpeed          = 150.0F;
            SteeringBehaviour = new FlockingBehaviour(this);
        }
        public Sheep(EntityManager entityManager, Vector2 pos) : base(entityManager, pos)
        {
            Mass              = 1.0F;
            MaxSpeed          = 20.0F;
            SteeringBehaviour = new FlockingBehaviour(this);

            // Set animation frames
            animationFrames = new Rectangle[]
            {
                new Rectangle(1, 1, 32, 32),    // North
                new Rectangle(34, 34, 32, 32),  // East
                new Rectangle(34, 1, 32, 32),   // South
                new Rectangle(1, 34, 32, 32),   // West
            };
        }
Example #3
0
    void Start()
    {
        flock = new List <GameObject>();
        for (var i = 0; i < flockSize; i++)
        {
            GameObject boid = (GameObject)GameObject.Instantiate(boidPrefab, transform.position, transform.rotation);
            boid.transform.parent = transform;
            Vector2 pos      = Random.insideUnitCircle * GetComponent <Collider>().bounds.size.x / 2.0f;
            Vector3 position = new Vector3(pos.x, 0, pos.y);
            boid.transform.localPosition = position;
            flock.Add(boid);
        }

        foreach (var boid in flock)
        {
            FlockingBehaviour behaviour = boid.GetComponent <FlockingBehaviour>();
            if (behaviour != null)
            {
                behaviour.StartFlocking(flock, target);
            }
        }
    }
Example #4
0
    IEnumerator FlockUpdate()
    {
        while (true)
        {
            if (idleDelay == 0)
            {
                inRange = new List <GameObject>();

                foreach (GameObject boid in theFlock)
                {
                    Vector3           separation = transform.position - boid.transform.position;
                    FlockingBehaviour fs         = boid.GetComponent <FlockingBehaviour>();

                    if (separation.magnitude > 0.01F && separation.magnitude < range)
                    {
                        inRange.Add(boid);
                    }


                    if (separation.magnitude < 5F && Random.Range(0, 100) > 97 && fs.villagerID != villagerID && fs.pathToFollow.Count > 3 && fs.pathToFollow != pathToFollow)
                    {
                        pathToFollow = fs.pathToFollow;
                    }
                }

                steeringAccumulator = Vector3.zero;

                if (inRange.Count > 0)
                {
                    //	steeringAccumulator += cohesionWeight * CohesionSteering();
                    //	steeringAccumulator += separationWeight * SeparationSteering();
                }

                // if the world has nodes placed in it\
                if (graph.nodes.Count > 2)
                {
                    if (pathToFollow.Count <= 0)
                    {
                        SetTarget();
                    }
                    if (inRange.Count == 0)
                    {
                        steeringAccumulator += seekWeight * SeekNodeSteering();
                    }
                    else
                    {
                        steeringAccumulator += (seekWeight / 2) * SeekNodeSteering();
                    }
                }

                /*
                 * int layermask = ~(1<<8);
                 * RaycastHit hit;
                 * if(Physics.SphereCast(transform.position, 1F, transform.forward, out hit, 2F, layermask))
                 * {
                 *      print ("Hit " + hit.transform.name);
                 * }
                 */

                // steeringAccumulator.y = 0;

                if (steeringAccumulator.magnitude > maxForce)
                {
                    steerForce = steeringAccumulator.normalized * maxForce;
                }
                else
                {
                    steerForce = steeringAccumulator;
                }
            }
            else
            {
                if (steerForce.magnitude > 0)
                {
                    steerForce = Vector3.zero;
                }
                if (rigidbody.velocity.magnitude > 0)
                {
                    rigidbody.velocity *= 0.1f;
                }

                idleDelay -= Time.deltaTime * 20;
                if (idleDelay < 0)
                {
                    idleDelay = 0;
                }
            }

            float waitTime = Random.Range(0.1F, 0.4F);
            yield return(new WaitForSeconds(waitTime));
        }
    }
Example #5
0
 public void Start()
 {
     controller        = GameObject.FindGameObjectWithTag("GameController");
     movementBehaviour = GetComponent <FlockingBehaviour>();
     agent             = GetComponent <NavMeshAgent>();
 }