예제 #1
0
 // updates the time value
 public void UpdateTime()
 {
     // if the time is not set
     if (timer != null && timerText != null)
     {
         // updates the timer text.
         timerText.text = timer.GetCurrentTimeValue().ToString("F2");
     }
 }
예제 #2
0
    // sends the data to the clients
    void SendDataToClients()
    {
        // Time (float) - 4 bytes
        // Number of Players (Int) - 4 Bytes
        // Number of Active Item Boxes (Int) - 4 bytes
        // Players
        // Items


        // data to be sent out to clients.
        byte[] sendData = new byte[serverBufferSize];
        int    index    = 0;

        // gets all items currently on the field.
        FieldItem[] spawnedItems = FindObjectsOfType <FieldItem>(false);

        // Timer
        {
            // timer data
            byte[] data;

            // the value for the timer
            float value = -1.0F;

            // if the timer has been set, get the current time.
            // if there is no time, it's sent to 999 so that the game doesn't pre-maturely end.
            if (timer != null)
            {
                // gets the timer value
                value = timer.GetCurrentTimeValue();

                // if it's a non-number, make it 999.
                if (float.IsNaN(value))
                {
                    value = 999.0F;
                }
            }
            else
            {
                // no timer
                value = 999.0F;
            }

            // convert to bytes
            data = BitConverter.GetBytes(value);

            Buffer.BlockCopy(data, 0, sendData, index, data.Length);
            index += data.Length;
        }

        // Number of Players
        {
            byte[] data = BitConverter.GetBytes(players.Count);
            Buffer.BlockCopy(data, 0, sendData, index, data.Length);
            index += data.Length;
        }

        // Number of Item Boxes
        {
            byte[] data = BitConverter.GetBytes(spawnedItems.Length); // number of items
            Buffer.BlockCopy(data, 0, sendData, index, data.Length);
            index += data.Length;
        }

        // Players
        // getting player data
        // this sends the data of all four players.
        // // On the client side, the controlled player ignores the data of the player with the same number.
        for (int i = 0; i < players.Count; i++)
        {
            byte[] pData = players[i].GetData();
            pData.CopyTo(sendData, index);
            index += pData.Length;
        }


        // Item Boxes
        {
            // copies the transformation data into the array (type, position, and rotation).
            for (int i = 0; i < spawnedItems.Length; i++)
            {
                byte[] iData = spawnedItems[i].GetData();
                iData.CopyTo(sendData, index);
                index += iData.Length;
            }
        }

        // sets the send buffer data
        server.server.SetSendBufferData(sendData);
    }