Example #1
0
    void Start()
    {
        posToVel = new MetricBuffer <Vector3, Vector2>(2, (buffer) => {
            var vel3 = buffer.GetAt(1) - buffer.GetAt(0);

            return(new Vector2(vel3.x, vel3.z));
        }, (a, b) => a + b, Vector2.zero);

        velToSpeed = new MetricSEQ <Vector2, float>((data) => {
            return(data.magnitude);
        }, (a, b) => a + b, 0);

        speedToSpeedChange = new MetricBuffer <float, float>(2, (buffer) => {
            return(buffer.GetAt(1) - buffer.GetAt(0));
        }, (a, b) => a + b, 0);

        //Refer to Equation 2 in "PLEdestrians: A Least-Effort Approach to Crowd Simulation"
        speedToMetabolicEnergy = new MetricSEQ <float, float>((data) => {
            var speed = data / Time.fixedDeltaTime;

            var kineticEnergy = mass * (2.23f + 1.26f * speed * speed);
            kineticEnergy    *= Time.fixedDeltaTime;

            return(kineticEnergy);
        }, (a, b) => a + b, 0);

        velToAccel = new MetricBuffer <Vector2, float>(2, (buffer) => {
            return((buffer.GetAt(1) - buffer.GetAt(0)).magnitude);
        }, (a, b) => a + b, 0);

        velToAngle = new MetricBuffer <Vector2, float>(2, (buffer) => {
            return(Vector2.SignedAngle(buffer.GetAt(0), buffer.GetAt(1)));
        }, (a, b) => a + b, 0);

        angleToAbs = new MetricSEQ <float, float>((data) => {
            return(Mathf.Abs(data));
        }, (a, b) => a + b, 0);

        isAgent = gameObject.CompareTag("Player") ? 1 : -1; // agents are tagged "Player"
        BenchmarkUtility.AddInfoCollector(this);
    }
    private void FixedUpdate()
    {
        Transform        pTrans;
        Transform        p2Trans;
        PlayerController pCont;
        PlayerController p2Cont;

        //print("SPEED: " + Input.GetAxis("Vertical"));

        controlledTransform.gameObject.GetComponent <Animator>().SetFloat("Vertical", Input.GetAxis("Vertical"));

        //print(controlledTransform.gameObject.GetComponent<InfoCollector>().maxSpeed / Time.fixedDeltaTime);

        for (int x = 0; x < playerArr.Length; x++)
        {
            GameObject p = playerArr[x];

            //print(p.ToString()+": "+p.GetComponent<InfoCollector>().maxSpeed / Time.fixedDeltaTime);
            //print(p.GetComponent<Transform>().position + p.GetComponent<NavMeshAgent>().desiredVelocity);
            p.GetComponent <Transform>().position = p.GetComponent <Transform>().position +
                                                    Vector3.Scale(p.GetComponent <NavMeshAgent>().desiredVelocity, new Vector3(Time.deltaTime, Time.deltaTime, Time.deltaTime));
            p.GetComponent <NavMeshAgent>().nextPosition = p.GetComponent <Transform>().position;

            pTrans = p.GetComponent("Transform") as Transform;


            pCont = p.GetComponent("PlayerController") as PlayerController;

            if (p.GetComponent <NavMeshAgent>() != null)
            {
                p.GetComponent <Animator>().SetFloat("Vertical", Vector3.Magnitude(p.GetComponent <NavMeshAgent>().velocity));
            }
            else if (p.GetComponent <RRTAlgo>() != null)
            {
                p.GetComponent <Animator>().SetFloat("Vertical", p.GetComponent <RRTAlgo>().speed);
            }
            else
            {
                Debug.Log("The Animator could not animate: speed needs to be designated");
            }

            if (pTrans.position.z > 47 && pTrans.position.z < 53 && pTrans.position.x > -3 && pTrans.position.x < 3)
            {
                BenchmarkUtility.StopInfoCollector(p.GetComponent <InfoCollector>());

                if (p.GetComponent <NavMeshAgent>() != null)
                {
                    p.GetComponent <NavMeshAgent>().enabled = false;
                }
                //p.GetComponent<UnityAnimationRecorder>().StopRecording();
                //p.SetActive(false);


                p.transform.position = new Vector3(-500 + doneCount * 5, 0, 0);
                doneArr[x]           = true;
                //pCont.setTarget(pTrans.position + new Vector3(0, 0, 20));
                doneCount++;
                //print(doneCount);
            }

            //"Bunching" Prevention Algorithm
            foreach (GameObject p2 in playerArr)
            {
                p2Trans = p.GetComponent("Transform") as Transform;


                p2Cont = p.GetComponent("PlayerController") as PlayerController;

                /*if (!ReferenceEquals(p, p2))
                 * {
                 *  if (Vector3.Distance(p2Cont.agent.destination, pCont.agent.destination) < 2)
                 *  {
                 *
                 *
                 *
                 *      if (Vector3.Distance(p2Trans.position, p2Cont.agent.destination) < 2)
                 *      {
                 *          p2Cont.setTarget(p2Trans.position);
                 *      }
                 *
                 *
                 *  }
                 * }*/
            }
        }

        //print("cont pos: " + controlledTransform.position.ToString());

        if (controlledTransform.position.z > 47 && controlledTransform.position.z < 53 && controlledTransform.position.x > -3 && controlledTransform.position.x < 3)
        {
            controlledTransform.gameObject.GetComponent <FirstPersonAIO>().playerCanMove      = false;
            controlledTransform.gameObject.GetComponentInChildren <CapsuleCollider>().enabled = false;
            //controlledTransform.gameObject.GetComponentInChildren<UnityAnimationRecorder>().StopRecording();
            //controlledTransform.gameObject.SetActive(false);
            controlledTransform.position = new Vector3(-500 + doneCount * 5, -2, 0);
            doneArr[8] = true;
            doneCount++;
            //print(doneCount);
            BenchmarkUtility.StopInfoCollector(controlledTransform.gameObject.GetComponent <InfoCollector>());
        }


        bool allDone = true;

        for (int x = 0; x < 9; x++)
        {
            if (!doneArr[x])
            {
                allDone = false;
            }
            //print(x + " is " + doneArr[x]);
        }

        /*foreach (bool b in doneArr)
         * {
         *
         *  if (!b) { allDone = false; }
         * }*/

        if (allDone && noAnimations)
        {
            print("BIG DAY");

            noAnimations = false;

            foreach (GameObject p in playerArr)
            {
                p.GetComponent <UnityAnimationRecorder>().StopRecording();
            }

            controlledTransform.gameObject.GetComponent <UnityAnimationRecorder>().StopRecording();

            BenchmarkUtility.ComputeStatistics();
        }

        //endGame = Vector3.Distance(controlledTransform.position, goal) < 1;
    }