// Update is called once per frame
 void Update()
 {
     if (allowUpdates)
     {
         //updates my objects
         timer -= Time.deltaTime;
         for (int i = 0; i < bloidList2.Count; ++i)
         {
             currentObj = bloidList2[i].GetComponent <BoidBehavior>();
             if (timer <= 0)
             {
                 currentObj.SendMessage("SetDirection");
             }
             currentObj.SendMessage("simulatePos");
         }
         BloidData newData = receiveData();
         //update all objects
         if (newData.objectId >= 0)
         {
             for (int i = 0; i < bloidList1.Count && !found; i++)
             {
                 // find the object and update it
                 if (bloidList1[i].GetComponent <BoidBehavior>().objId == newData.objectId)
                 {
                     bloidList1[i].GetComponent <BoidBehavior>().SendMessage("setPos", new Vector3(newData.x, newData.y, newData.z));
                     bloidList1[i].GetComponent <BoidBehavior>().direction = newData.direction;
                     found = true;
                 }
             }
             // this was done for spawning events that never happened
             if (!found && newData.objectId <= 10000)
             {
                 GameObject dorkus = Instantiate(boid, new Vector3(newData.x, newData.y, newData.z), Quaternion.identity);
                 dorkus.GetComponent <BoidBehavior>().objId = newData.objectId;
                 bloidList1.Add(dorkus);
             }
             found = false;
         }
         if (timer <= 0)
         {
             timer = MAX_TIME;
         }
     }
 }
Пример #2
0
    // Update is called once per frame
    void Update()
    {
        if (allowUpdates)
        {
            //simulates local position of local objects
            for (int i = 0; i < bloidListLocal.Count; ++i)
            {
                currentObj = bloidListLocal[i].GetComponent <BoidBehavior>();
                currentObj.SendMessage("simulatePos");
            }

            BloidData newData = receiveData();

            //get timestamp from newdata and set relative times
            float currentTime = Time.deltaTime;
            float timeStamp   = Time.deltaTime + newData.timeStamp;
            float timeStep    = Time.deltaTime + (timeStamp * 2.0f);

            //tmp variables for loop iteration
            BoidBehavior tmp;
            Vector3      initialPos, timestepPos, timestepX2Pos, average, velFinal;

            //simulate all objects
            if (newData.objectId >= 0)
            {
                for (int i = 0; i < bloidListAll.Count && !found; i++)
                {
                    // find the object and update it
                    tmp = bloidListAll[i].GetComponent <BoidBehavior>();


                    if (tmp.objId == newData.objectId)
                    {
                        //calculate position with dead reckoning
                        //at current time
                        initialPos = deadReckon(tmp.transform.position, tmp.velocity, tmp.acceleration, currentTime);
                        //at timestamp
                        timestepPos = deadReckon(tmp.transform.position, tmp.velocity, tmp.acceleration, timeStamp);
                        //at double the timestamp
                        timestepX2Pos = deadReckon(tmp.transform.position, tmp.velocity, tmp.acceleration, timeStep);

                        //average vector of the three for the position
                        average.x = (initialPos.x + timestepPos.x + timestepX2Pos.x) / 3;
                        average.y = (initialPos.y + timestepPos.y + timestepX2Pos.y) / 3;
                        average.z = (initialPos.z + timestepPos.z + timestepX2Pos.z) / 3;

                        //set velocity of boid for future
                        velFinal = tmp.velocity + (tmp.acceleration * timeStamp);

                        //send position, direction, and final velocity to individual bloid, nice
                        tmp.velocity = velFinal;
                        tmp.SendMessage("setPos", average);
                        tmp.direction = newData.direction;
                        found         = true;
                    }
                }
                // this was done for spawning events that never happened
                if (!found && newData.objectId <= 10000)
                {
                    GameObject dorkus = Instantiate(boid, new Vector3(newData.x, newData.y, newData.z), Quaternion.identity);
                    dorkus.GetComponent <BoidBehavior>().objId = newData.objectId;
                    bloidListAll.Add(dorkus);
                }
                found = false;
            }
        }
    }