// Cohesion is a behavior that causes agents to steer towards a "center of mass"
    Vector3 Cohesion(PedestrianBehavior agent)
    {
        Vector3 forceVector    = Vector3.zero;
        Vector3 centerOfMass   = Vector3.zero;
        uint    neighbourCount = 0;

        List <GameObject> neighbours = agent.GetNeighbours();

        for (int i = 0; i < neighbours.Count; i++)
        {
            // Discard self checking
            if (neighbours[i] != agent.gameObject)
            {
                centerOfMass += neighbours[i].transform.position;
                neighbourCount++;
            }
        }

        if (neighbourCount > 0)
        {
            centerOfMass /= neighbourCount;

            centerOfMas = centerOfMass;

            forceVector = centerOfMass - agent.gameObject.transform.position;
            forceVector.Normalize();
        }
        return(forceVector);
    }
Beispiel #2
0
    public void AddNeighbour(GameObject agent)
    {
        PedestrianBehavior agentBehavior = agent.GetComponent <PedestrianBehavior>();

        if (agentBehavior.mState == PedestrianState.kPedestrianState_Dead)
        {
            return;
        }

        if (agentBehavior.GetLeader() == null && !agentBehavior.IsLeader())
        {
            if (IsLeader() && !mNeighbours.Contains(agent))
            {
                agentBehavior.SetLeader(gameObject);
                mNeighbours.Add(agent);
            }

            //Autogenerate leader of the flocking group
            else if (!IsLeader() && GetLeader() == null)
            {
                ConvertToLeader(true);
                agentBehavior.ConvertToLeader(false);
                agentBehavior.SetLeader(gameObject);
                mNeighbours.Add(agent);
            }
        }
    }
    Vector3 Separation(PedestrianBehavior agent)
    {
        Vector3 forceVector = Vector3.zero;

        List <GameObject> neighbours = agent.GetNeighbours();

        for (int i = 0; i < neighbours.Count; i++)
        {
            // Discard self checking
            if (neighbours[i] != agent.gameObject)
            {
                // Distance squared
                float distance = Mathf.Pow(Vector3.Distance(neighbours[i].transform.position, agent.transform.position), 2.0f);
                if (distance < SeparationDistance)
                {
                    // Calculate the heading vector between the source entity and its neighbour
                    Vector3 headingVector = agent.transform.position - neighbours[i].transform.position;

                    // Calculate the scale value
                    float scale = headingVector.magnitude / (float)Mathf.Sqrt(SeparationDistance);

                    //The closer we are the more intense the force vector will be
                    forceVector = Vector3.Normalize(headingVector) / scale;
                }
            }
        }
        return(forceVector);
    }
    // Alignment is a behavior that causes a particular agent to line up with agents close by
    Vector3 Alignment(PedestrianBehavior agent)
    {
        Vector3 forceVector     = Vector3.zero;
        int     neighboursCount = 0;

        List <GameObject> neighbours = agent.GetNeighbours();

        for (int i = 0; i < neighbours.Count; i++)
        {
            // Discard self checking
            if (neighbours[i] != agent.gameObject)
            {
                forceVector += neighbours[i].GetComponent <PedestrianBehavior>().Velocity;
                neighboursCount++;
            }
        }

        if (neighboursCount > 0)
        {
            forceVector /= neighboursCount;
            forceVector.Normalize();
        }

        return(forceVector);
    }
Beispiel #5
0
 public Pedestrian(PhysicsComponent2D phys, MovementAIComponent2D move, DrawComponent draw, PedestrianState pState)
 {
     movement = move;
     physics = phys;
     this.draw = draw;
     state = pState;
     behavior = PedestrianBehavior.DEFAULT;
     studentType = draw.animation.animationId.Substring(0, 8);
     this.BoundingRectangle = new COMP476Proj.BoundingRectangle(phys.Position, 16, 6);
     draw.Play();
 }
Beispiel #6
0
    public List <GameObject> GetNeighbours()
    {
        if (IsLeader())
        {
            return(mNeighbours);
        }

        else if (GetLeader() != null)
        {
            PedestrianBehavior leader = GetLeader().GetComponent <PedestrianBehavior>();
            return(leader.GetNeighbours());
        }

        return(null);
    }
Beispiel #7
0
    public void RemoveNeighbour(GameObject agent)
    {
        PedestrianBehavior agentBehavior = agent.GetComponent <PedestrianBehavior>();

        if (IsLeader() && mNeighbours.Contains(agent))
        {
            mNeighbours.Remove(agent);
            agentBehavior.RemoveLeader();

            //If the leader has no neighbours
            if (mNeighbours.Count <= 1)
            {
                ConvertToLeader(false);
            }
        }
        else if (mLeader != null)
        {
            if (mLeader == agent && agentBehavior.IsLeader())
            {
                RemoveLeader();
            }
        }
    }
 void Awake()
 {
     mParent = this.transform.parent.GetComponent <PedestrianBehavior>();
 }
 // Use this for initialization
 void Start()
 {
     Animator   = GetComponent <Animator>();
     Pedestrian = gameObject.GetComponent <PedestrianBehavior>();
 }
 /// <summary>
 /// Removes the provided pedestrian from the simulation
 /// </summary>
 /// <param name="pedestrian"> The pedestrian to be removed </param>
 public void RemovePedestrian(PedestrianBehavior pedestrian)
 {
     mAgents.Remove(pedestrian);
     AddPedestrian();
 }
Beispiel #11
0
        public override void Fall(bool isSuperFlash)
        {
            behavior = PedestrianBehavior.KNOCKEDUP;
            
            if (state != PedestrianState.FALL)
            {
                if (isSuperFlash)
                {
                    playSound("SuperFlash");
                }

                transitionToState(PedestrianState.FALL);
            }
            movement.Stop(ref physics);
        }
Beispiel #12
0
        private void updateState(World w)
        {
            IsVisible(Game1.world.streaker.Position, out canSee, out canReach);

            fleePoints = false;

            //--------------------------------------------------------------------------
            //        DEFAULT BEHAVIOR TRANSITIONS --> Before aware of streaker
            //--------------------------------------------------------------------------
            if (behavior == PedestrianBehavior.DEFAULT)
            {
                if (Vector2.Distance(w.streaker.Position, pos) < detectRadius && canSee)
                {
                    playSound("Exclamation");
                    behavior = PedestrianBehavior.AWARE;
                    transitionToState(PedestrianState.FLEE);
                }
            }
            //--------------------------------------------------------------------------
            //               AWARE BEHAVIOR TRANSITION --> Knows about streaker
            //--------------------------------------------------------------------------
            else if (behavior == PedestrianBehavior.AWARE)
            {
                if (Vector2.Distance(w.streaker.Position, pos) < detectRadius && canSee)
                    fleePoints = true;
                else
                    fleePoints = false;
                if (Vector2.Distance(w.streaker.Position, pos) > detectRadius)
                {
                    behavior = PedestrianBehavior.DEFAULT;
                    transitionToState(PedestrianState.WANDER);
                }
            }
            //--------------------------------------------------------------------------
            //               COLLIDE BEHAVIOR TRANSITION
            //--------------------------------------------------------------------------
            else if (behavior == PedestrianBehavior.KNOCKEDUP)
            {
                switch (state)
                {
                    case PedestrianState.FALL:
                        if (draw.animComplete)
                        {
                            SoundManager.GetInstance().PlaySound("Common", "Fall", w.streaker.Position, Position);
                            transitionToState(PedestrianState.GET_UP);
                        }
                        break;
                    case PedestrianState.GET_UP:
                        if (draw.animComplete && Vector2.Distance(w.streaker.Position, pos) < detectRadius)
                        {
                            behavior = PedestrianBehavior.AWARE;
                            transitionToState(PedestrianState.FLEE);
                        }
                        else if (draw.animComplete && Vector2.Distance(w.streaker.Position, pos) >= detectRadius)
                        {
                            behavior = PedestrianBehavior.DEFAULT;
                            transitionToState(PedestrianState.WANDER);
                        }
                        break;
                }
            }

            //--------------------------------------------------------------------------
            //       CHAR STATE --> ACTION
            //--------------------------------------------------------------------------
            
            switch (state)
            {
                case PedestrianState.STATIC:
                    movement.Stop(ref physics);
                    break;
                case PedestrianState.WANDER:
                    movement.Wander(ref physics);
                    break;
                case PedestrianState.FLEE:
                    movement.SetTarget(w.streaker.ComponentPhysics.Position);
                    movement.Flee(ref physics);
                    break;
                case PedestrianState.PATH:
                    //TO DO
                    break;
                case PedestrianState.FALL:
                    movement.Stop(ref physics);
                    break;
                case PedestrianState.GET_UP:
                    movement.Stop(ref physics);
                    break;
                default:
                    break;
            }
            if (!fleePoints)
                fleePointsTime = 500;
            
        }