示例#1
0
    void UpdatePathfindingMovement()
    {
        RigidBody.velocity = Vector3.zero;

        if (Vector3.Distance(transform.position, mNextLocation) > mMinDistance)
        {
            // Initial version
            Vector3 forward = (mNextLocation - transform.position).normalized;
            if (forward == Vector3.zero)
            {
                transform.forward = new Vector3(1.0f, 0.0f, 0.0f);
            }
            else
            {
                transform.forward = forward;
            }

            //transform.position = Vector3.MoveTowards(transform.position, mNextLocation, Time.deltaTime * MovementSpeed);

            // Test this
            RigidBody.MovePosition(Vector3.MoveTowards(transform.position, mNextLocation, Time.deltaTime * MovementSpeed));
        }
        else
        {
            transform.position = mNextLocation;
            if (!GetNextLocationStep())
            {
                mState = PedestrianState.kPedestrianState_Searching;
            }
        }
    }
    public void InitManual(Vector3 position, Quaternion rotation, int seed)
    {
        if (SimulatorManager.Instance.IsAPI)
        {
            FixedUpdateManager = ApiManager.Instance;
        }
        else
        {
            FixedUpdateManager = SimulatorManager.Instance.PedestrianManager;
        }

        RandomGenerator = new System.Random(seed);
        Path            = new NavMeshPath();

        agent = GetComponent <NavMeshAgent>();
        anim  = GetComponentInChildren <Animator>();
        rb    = GetComponent <Rigidbody>();
        Name  = transform.GetChild(0).name;

        agent.avoidancePriority = 0;

        agent.updatePosition = false;
        agent.updateRotation = false;
        agent.Warp(position);
        agent.transform.rotation = rotation;

        thisPedState = PedestrianState.None;
        Control      = ControlType.Manual;
    }
示例#3
0
 void Waiting()
 {
     if (!CheckCrosswalk() && !CheckCar())
     {
         mState = PedestrianState.kPedestrianState_Walking;
     }
 }
示例#4
0
 public void RemoveLeader()
 {
     mLeader.GetComponent <PedestrianBehavior>().RemoveNeighbour(this.gameObject);
     mLeader = null;
     //mNeighbours.Clear();
     mState = PedestrianState.kPedestrianState_Searching;
 }
 private void Reset()
 {
     StopPEDCoroutines();
     Path.ClearCorners();
     CurrentWP    = 0;
     thisPedState = PedestrianState.None;
 }
示例#6
0
 void Searching()
 {
     //Start pathfinding (get nearest point and get a random point)
     GetNewPath();
     if (GetNextLocationStep())
     {
         mState = PedestrianState.kPedestrianState_Walking;
     }
 }
示例#7
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();
 }
    private IEnumerator IdleAnimation(float duration)
    {
        if (agent == null || duration == 0f)
        {
            yield break;
        }

        thisPedState = PedestrianState.Idle;

        yield return(FixedUpdateManager.WaitForFixedSeconds(duration));

        thisPedState = PedestrianState.Walking;
    }
示例#9
0
    public void changeState(PedestrianState x)
    {
        currState = x;
        wanderingScript.enabled = false;
        searchingScript.enabled = false;
        ridingScript.enabled    = false;
        departingScript.enabled = false;
        switch (currState)
        {
        case PedestrianState.Wandering:
            wanderingScript.enabled = true;
            a.SetBool("Wandering", true);
            a.SetBool("Sitting", false);
            a.SetBool("Searching", false);
            a.SetBool("Found", false);
            Debug.Log("Walk animation");
            //GetComponent<MeshRenderer>().material.color = Color.blue;
            //change animation
            break;

        case PedestrianState.Searching:
            //GetComponent<MeshRenderer>().material.color = Color.red;
            searchingScript.enabled = true;
            a.SetBool("Searching", true);
            a.SetBool("Wandering", false);
            a.SetBool("Sitting", false);
            a.SetBool("Found", false);
            Debug.Log("Jump animation");
            break;

        case PedestrianState.Riding:
            //GetComponent<MeshRenderer>().material.color = Color.green;
            ridingScript.enabled = true;
            a.SetBool("Sitting", true);
            a.SetBool("Searching", false);
            a.SetBool("Searching", false);
            a.SetBool("Found", false);
            Debug.Log("Sit animation");
            break;

        case PedestrianState.Departing:
            //GetComponent<MeshRenderer>().material.color = Color.grey;
            departingScript.enabled = true;
            a.SetBool("Wandering", true);
            a.SetBool("Sitting", false);
            a.SetBool("Searching", false);
            a.SetBool("Found", false);
            Debug.Log("Walk animation");
            break;
        }
    }
示例#10
0
    void OnCollisionEnter(Collision col)
    {
        if (col.gameObject.tag == "Car" && !mCollided)
        {
            if (mLeader)
            {
                mLeader.GetComponent <PedestrianBehavior>().RemoveNeighbour(this.gameObject);
            }

            mCollided = true;
            mState    = PedestrianState.kPedestrianState_Dead;
            GetComponent <RagdollController>().EnableRagdoll();
            GetComponent <Animator>().enabled = false;
        }
    }
示例#11
0
 private void transitionToState(PedestrianState pState)
 {
     switch (pState)
     {
         case PedestrianState.STATIC:
             state = PedestrianState.STATIC;
             draw.animation = SpriteDatabase.GetAnimation(studentType + "_static");
             physics.SetSpeed(false);
             physics.SetAcceleration(false);
             draw.Reset();
             break;
         case PedestrianState.WANDER:
             state = PedestrianState.WANDER;
             draw.animation = SpriteDatabase.GetAnimation(studentType + "_walk");
             physics.SetSpeed(false);
             physics.SetAcceleration(false);
             draw.Reset();
             break;
         case PedestrianState.FLEE:
             state = PedestrianState.FLEE;
             draw.animation = SpriteDatabase.GetAnimation(studentType + "_flee");
             physics.SetSpeed(true);
             physics.SetAcceleration(true);
             draw.Reset();
             break;
         case PedestrianState.FALL:
             state = PedestrianState.FALL;
             draw.animation = SpriteDatabase.GetAnimation(studentType + "_fall");
             physics.SetSpeed(false);
             physics.SetAcceleration(false);
             draw.Reset();
             break;
         case PedestrianState.GET_UP:
             state = PedestrianState.GET_UP;
             draw.animation = SpriteDatabase.GetAnimation(studentType + "_getup");
             physics.SetSpeed(false);
             physics.SetAcceleration(false);
             draw.Reset();
             break;
         case PedestrianState.PATH:
             state = PedestrianState.PATH;
             draw.animation = SpriteDatabase.GetAnimation(studentType + "_walk");
             physics.SetSpeed(false);
             physics.SetAcceleration(false);
             draw.Reset();
             break;
     }
 }
示例#12
0
    // Call this function whenever the game is ready, to start updating the GameObject
    public void Init()
    {
        mState      = PedestrianState.kPedestrianState_Searching;
        mLeader     = null;
        mNeighbours = new List <GameObject>();
        RigidBody   = gameObject.GetComponent <Rigidbody>();
        Velocity    = transform.forward;

        mNN           = this.GetComponent <NN.NeuralNetwork>();
        mNNController = GameObject.FindGameObjectWithTag("Neural Network Controller").GetComponent <NN.NeuralNetwork>();

        ConvertToLeader(StartAsLeader);

        //Begin updating the GameObject
        mInitialized = true;
    }
 public void changeState(PedestrianState x)
 {
     currState = x;
     wanderingScript.enabled = false;
     searchingScript.enabled = false;
     ridingScript.enabled = false;
     departingScript.enabled = false;
     switch (currState) {
     case PedestrianState.Wandering:
         wanderingScript.enabled = true;
         a.SetBool("Wandering",true);
         a.SetBool("Sitting",false);
         a.SetBool("Searching",false);
         a.SetBool("Found",false);
         Debug.Log("Walk animation");
         //GetComponent<MeshRenderer>().material.color = Color.blue;
         //change animation
         break;
     case PedestrianState.Searching:
         //GetComponent<MeshRenderer>().material.color = Color.red;
         searchingScript.enabled = true;
         a.SetBool("Searching",true);
         a.SetBool("Wandering",false);
         a.SetBool("Sitting",false);
         a.SetBool("Found",false);
         Debug.Log("Jump animation");
         break;
     case PedestrianState.Riding:
         //GetComponent<MeshRenderer>().material.color = Color.green;
         ridingScript.enabled = true;
         a.SetBool("Sitting",true);
         a.SetBool("Searching",false);
         a.SetBool("Searching",false);
         a.SetBool("Found",false);
         Debug.Log("Sit animation");
         break;
     case PedestrianState.Departing:
         //GetComponent<MeshRenderer>().material.color = Color.grey;
         departingScript.enabled = true;
         a.SetBool("Wandering",true);
         a.SetBool("Sitting",false);
         a.SetBool("Searching",false);
         a.SetBool("Found",false);
         Debug.Log("Walk animation");
         break;
     }
 }
示例#14
0
    private IEnumerator EvaluateEgoToTrigger(Vector3 pos, float dist)
    {
        // for ego in list of egos
        var players = SimulatorManager.Instance.AgentManager.ActiveAgents;

        while (true)
        {
            for (int i = 0; i < players.Count; i++)
            {
                if (Vector3.Distance(players[i].transform.position, pos) < dist)
                {
                    CurrentTriggerDistance = 0;
                    thisPedState           = PedestrianState.Walking;
                    yield break;
                }
            }
            yield return(new WaitForFixedUpdate());
        }
    }
示例#15
0
    void Walking()
    {
        //If the agent is affected by flocking
        if (GetLeader())
        {
            transform.forward = Velocity.normalized;
            RigidBody.MovePosition(transform.position + (Velocity * Time.deltaTime * MovementSpeed * 1.0f));

            //Check if the distance to the final location has increased
        }
        else
        {
            if (CheckCrosswalk() || CheckCar())
            {
                mState = PedestrianState.kPedestrianState_Waiting;
            }
            else
            {
                UpdatePathfindingMovement();
            }
        }
    }
示例#16
0
    public void WalkRandomly(bool enable)
    {
        Reset();

        if (!enable)
        {
            Control      = ControlType.Manual;
            thisPedState = PedestrianState.None;
            return;
        }

        agent.avoidancePriority = RandomGenerator.Next(1, 100);

        var           position        = agent.transform.position;
        MapPedestrian closest         = null;
        float         closestDistance = float.MaxValue;
        int           closestIndex    = 0;

        foreach (var path in SimulatorManager.Instance.PedestrianManager.pedPaths)
        {
            for (int i = 0; i < path.mapWorldPositions.Count; i++)
            {
                float distance = Vector3.SqrMagnitude(position - path.mapWorldPositions[i]);
                if (distance < closestDistance)
                {
                    closest         = path;
                    closestIndex    = i;
                    closestDistance = distance;
                }
            }
        }
        targets = closest.mapWorldPositions;

        NextTargetIndex = closestIndex;
        Control         = ControlType.Automatic;
    }
示例#17
0
    public void PhysicsUpdate()
    {
        if (Control == ControlType.Automatic)
        {
            if (IsRandomIdle())
            {
                Coroutines[(int)CoroutineID.ChangePedState] = FixedUpdateManager.StartCoroutine(ChangePedState());
            }

            if (thisPedState == PedestrianState.Idle)
            {
                CurrentTurn  = Vector3.zero;
                CurrentSpeed = 0f;
            }
            else
            {
                if (!IsPathReady())
                {
                    NextTargetPos = GetRandomTargetPosition(NextTargetIndex);
                    agent.enabled = true;
                    agent.CalculatePath(NextTargetPos, Path);
                    agent.enabled = false;
                }

                var     corners   = Path.corners;
                Vector3 targetPos = rb.position;
                if (CurrentWP < corners.Length)
                {
                    targetPos = new Vector3(corners[CurrentWP].x, rb.position.y, corners[CurrentWP].z);
                }
                Vector3 direction = targetPos - rb.position;

                CurrentTurn  = direction;
                CurrentSpeed = LinearSpeed;
                thisPedState = PedestrianState.Walking;

                if (direction.magnitude < Accuracy)
                {
                    CurrentWP++;
                    if (CurrentWP >= corners.Length)
                    {
                        Path.ClearCorners();
                        CurrentTargetIndex = NextTargetIndex;
                        NextTargetIndex    = GetNextTargetIndex(CurrentTargetIndex);
                        CurrentWP          = 0;
                    }
                }
            }
        }
        else if (Control == ControlType.Waypoints)
        {
            if (thisPedState == PedestrianState.Idle)
            {
                CurrentTurn  = Vector3.zero;
                CurrentSpeed = 0f;
            }
            else
            {
                if (!IsPathReady())
                {
                    NextTargetPos = targets[NextTargetIndex];
                    agent.enabled = true;
                    agent.CalculatePath(NextTargetPos, Path);
                    agent.enabled = false;
                }

                var     corners   = Path.corners;
                Vector3 targetPos = rb.position;
                if (CurrentWP < corners.Length)
                {
                    targetPos = new Vector3(corners[CurrentWP].x, rb.position.y, corners[CurrentWP].z);
                }
                Vector3 direction = targetPos - rb.position;

                CurrentTurn  = direction;
                CurrentSpeed = LinearSpeed;
                thisPedState = PedestrianState.Walking;

                if (direction.magnitude < Accuracy)
                {
                    CurrentWP++;
                    if (CurrentWP >= corners.Length)
                    {
                        ApiManager.Instance?.AddWaypointReached(gameObject, NextTargetIndex);
                        Coroutines[(int)CoroutineID.IdleAnimation] = FixedUpdateManager.StartCoroutine(IdleAnimation(idle[NextTargetIndex]));

                        Path.ClearCorners();
                        CurrentTargetIndex = NextTargetIndex;
                        NextTargetIndex    = GetNextTargetIndex(CurrentTargetIndex);
                        CurrentWP          = 0;

                        if (NextTargetIndex == targets.Count - 1 && !waypointLoop)
                        {
                            WalkRandomly(false);
                        }
                    }
                }
            }
        }
        else if (Control == ControlType.Manual)
        {
            CurrentTurn  = Vector3.zero;
            CurrentSpeed = 0f;
        }

        PEDTurn();
        PEDMove();
        SetAnimationControllerParameters();
    }
示例#18
0
    private void setState(PedestrianState para_state)
    {
        if(para_state == PedestrianState.IDLE)
        {

            cnm.setToIdle();
            currSpeed = walkSpeed;
            idle_timeOfLastFaceSwitch = Time.time;
            faceSwitchesBeforeWalk = Random.Range(2,5);
        }
        else if(para_state == PedestrianState.WALKING)
        {
            int maxWalkRange = 10;
            int selectedWalkDistance = Random.Range(2,maxWalkRange);

            List<NavNode> walkDestNodes = null;
            walkDestNodes = worldNavGraph.getChildNodesAtHopDistance(worldNavGraph.getNode(terrainHandle.getNavNodeIDForCell(getCellForItem(transform.gameObject.name))),
                                                                   selectedWalkDistance,
                                                                     null,null);

            if((walkDestNodes == null)||(walkDestNodes.Count == 0))
            {
                // Return to idle.
                setState(PedestrianState.IDLE);
            }
            else
            {
                // Select dest node out of potential nodes.
                NavNode chosenDestNode = walkDestNodes[Random.Range(0,walkDestNodes.Count)];

                //List<NavNode> walkPath = worldNavGraph.searchForPath(terrainHandle.getNavNodeIDForCell(getCellForItem(transform.gameObject.name)),
                //                                                       chosenDestNode.getNodeID());

                initCharacterMovement(getCellForItem(((WorldNode) chosenDestNode).getWorldPt()),false,true);
            }

        }
        else if(para_state == PedestrianState.TALKING)
        {
            // Should have been set via the startConversationBehaviour.
        }

        currState = para_state;
    }
示例#19
0
    private void EvaluateWaypointTarget()
    {
        if (thisPedState == PedestrianState.Idle)
        {
            CurrentTurn  = Vector3.zero;
            CurrentSpeed = 0f;
        }
        else
        {
            if (!IsPathReady())
            {
                NextTargetPos = targets[NextTargetIndex];
                agent.enabled = true;
                agent.CalculatePath(NextTargetPos, Path);
                agent.enabled = false;
            }

            var     corners   = Path.corners;
            Vector3 targetPos = rb.position;
            if (CurrentWP < corners.Length)
            {
                targetPos = new Vector3(corners[CurrentWP].x, rb.position.y, corners[CurrentWP].z);
            }
            Vector3 direction = targetPos - rb.position;

            CurrentTurn  = direction;
            CurrentSpeed = LinearSpeed;
            thisPedState = PedestrianState.Walking;

            if (direction.magnitude < Accuracy)
            {
                CurrentWP++;
                if (CurrentWP >= corners.Length)
                {
                    // When waypoint is reached, Ped waits for trigger (if any), then idles (if any), then moves on to next waypoint
                    ApiManager.Instance?.AddWaypointReached(gameObject, NextTargetIndex);

                    Path.ClearCorners();
                    CurrentIdle            = idle[NextTargetIndex];
                    CurrentTriggerDistance = triggerDistance[NextTargetIndex];
                    CurrentTargetIndex     = NextTargetIndex;
                    NextTargetIndex        = GetNextTargetIndex(CurrentTargetIndex);
                    CurrentWP = 0;

                    if (CurrentTriggerDistance > 0f)
                    {
                        thisPedState = PedestrianState.Idle;
                        Coroutines[(int)CoroutineID.WaitForAgent] = FixedUpdateManager.StartCoroutine(EvaluateEgoToTrigger(NextTargetPos, CurrentTriggerDistance));
                    }
                    else if (thisPedState == PedestrianState.Walking && CurrentIdle > 0f)
                    {
                        Coroutines[(int)CoroutineID.IdleAnimation] = FixedUpdateManager.StartCoroutine(IdleAnimation(CurrentIdle));
                    }

                    if (CurrentTargetIndex == targets.Count - 1 && !waypointLoop)
                    {
                        WalkRandomly(false);
                    }
                }
            }
        }
    }