Beispiel #1
0
    //update loop at constant rate
    //used to push game data
    public IEnumerator UpdateServer()
    {
        while (true)
        {
            yield return(new WaitForSeconds(intervalS));

            PlayerInfo[] playerInfos = new PlayerInfo[players.Length];
            for (int i = 0; i < players.Length; i++)
            {
                Vector2 vel = players[i].gameObject.GetComponent <Rigidbody2D>().velocity;
                playerInfos[i] = new PlayerInfo((int)players[i].playerID, players[i].transform.position.x, players[i].transform.position.y, vel.x, vel.y);
            }
            PlayerUpdates updates = new PlayerUpdates(playerInfos);
            SendPlayerUpdates(updates);

            for (int i = 0; i < pingCallback.Length; i++)
            {
                if (pingCallback[i])
                {
                    pingCallback[i] = false;
                    byte[] pingcallback = UDPClient.StringToBytes("PingResult");
                    SendToClient(connectedClients[i], pingcallback);
                    SendToClient(connectedClients[i], pingcallback);
                }
            }
            //Debug.Log("updating server");
            UpdateServer();
        }
    }
Beispiel #2
0
        public override void ReadPacket()
        {
            SequenceNumber = ReadUShort();

            // Read the byte flag representing update types and reconstruct it
            var updateTypeFlag = ReadByte();
            // Keep track of value of current bit
            var currentTypeValue = 1;

            for (var i = 0; i < (int)UpdateType.Count; i++)
            {
                // If this bit was set in our flag, we add the type to the list
                if ((updateTypeFlag & currentTypeValue) != 0)
                {
                    UpdateTypes.Add((UpdateType)currentTypeValue);
                }

                // Increase the value of current bit
                currentTypeValue *= 2;
            }

            // Based on the update types, we read the corresponding values
            if (UpdateTypes.Contains(UpdateType.PlayerUpdate))
            {
                // First we read the length of the player update list
                var numPlayerUpdates = ReadByte();

                // Then we read all the values into PlayerUpdate instances
                for (var i = 0; i < numPlayerUpdates; i++)
                {
                    // We create a new instance
                    var playerUpdate = new PlayerUpdate();

                    // Read the information into the new instance
                    ReadPlayerUpdate(playerUpdate);

                    // Add the instance to the list
                    PlayerUpdates.Add(playerUpdate);
                }
            }

            if (UpdateTypes.Contains(UpdateType.EntityUpdate))
            {
                // First we read the length of the entity update list
                var numEntityUpdates = ReadByte();

                // Then we read all the values into the EntityUpdates dictionary
                for (var i = 0; i < numEntityUpdates; i++)
                {
                    // Create a new EntityUpdate instance
                    var entityUpdate = new EntityUpdate();

                    // Read the values into the instance
                    ReadEntityUpdate(entityUpdate);

                    // Add it to the list
                    EntityUpdates.Add(entityUpdate);
                }
            }
        }
Beispiel #3
0
    public bool Register(myUpdate update)
    {
        switch (update.GetUpdateType())
        {
        case myUpdate.UpdateType.GUI:
            GUIUpdates.Add(update.GetPriorityInType(), update);
            return(true);

        case myUpdate.UpdateType.Map:
            MapUpdates.Add(update.GetPriorityInType(), update);
            return(true);

        case myUpdate.UpdateType.PoolThing:
            //储存引用
            SortedList <int, myUpdate> temp;
            //看看有没有这个id
            if (PoolThingUpdates.TryGetValue(update.gameObject.GetInstanceID(), out temp))
            {
                //如果有,则将这个组件加入到对应敌人的更新队列中去
                temp.Add(update.GetPriorityInType(), update);
            }
            else
            {
                //如果没有,增加这个敌人的队列,并且把这个组件加入进去
                temp = new SortedList <int, myUpdate>();
                temp.Add(update.GetPriorityInType(), update);
                PoolThingUpdates.Add(update.gameObject.GetInstanceID(), temp);
            }

            return(true);

        case myUpdate.UpdateType.Player:
            PlayerUpdates.Add(update.GetPriorityInType(), update);
            return(true);

        case myUpdate.UpdateType.Enemy:
            //储存引用
            SortedList <int, myUpdate> temp1;
            //看看有没有这个id
            if (EnemyUpdates.TryGetValue(update.gameObject.GetInstanceID(), out temp1))
            {
                //如果有,则将这个组件加入到对应敌人的更新队列中去
                temp1.Add(update.GetPriorityInType(), update);
            }
            else
            {
                //如果没有,增加这个敌人的队列,并且把这个组件加入进去
                temp1 = new SortedList <int, myUpdate>();
                temp1.Add(update.GetPriorityInType(), update);
                EnemyUpdates.Add(update.gameObject.GetInstanceID(), temp1);
            }

            return(true);
        }
        return(false);
    }
Beispiel #4
0
 private void Initialize(SortedList <int, myUpdate> theList)
 {
     listKeys = theList.Keys;
     for (int i = 0; i < listKeys.Count; i++)
     {
         if (PlayerUpdates.TryGetValue(listKeys[i], out temp))
         {
             temp.Initialize();
         }
     }
 }
Beispiel #5
0
    protected override void HandleSerializedData(SerializeBase data)
    {
        //return;
        //Debug.Log("handling data...");
        //Debug.Log("gametimer: " + data.gameTime);
        testFloat = 99;
        if (GameTimer - data.gameTime > (float)Ping / 2000f + 0.01f || GameTimer - data.gameTime < (float)Ping / 2000f - 0.01f)
        {
            GameTimer = data.gameTime + ((float)Ping / 2000f);
        }
        Type t = data.GetType();

        testFloat = 91;
        if (t.Equals(typeof(PlayerUpdates)))
        {
            newUpdates = (PlayerUpdates)data;
            //pseudo code:

            /*
             *    PlayerUpdates updates = (PlayerUpdates)data;
             *    for (int i = 0; i < updates.PlayerInfos.Length; i++)
             *    {
             *    PlayerInfo info = updates.PlayerInfos[i];
             *    GameObject player = Array.Find(players, x => (int)x.playerID == info.playerID).gameObject;
             *    player.transform.position = new Vector3(info.xPos, info.yPos, player.transform.position.z);
             *
             *
             *    //find player object and execute set position method... players[info.playerID].SetPosition(info.xPos, info.yPos);
             *    //no need to update server after this
             *    }
             */
        }
        //CLIENT
        //if(t.Equals(typeof(MatchInfo)))
        //{
        //MatchInfo matchInfo = (MatchInfo)data;
        //if(matchInfo.gameEnd)
        //{
        //get matchInfo.scores
        //goto lobby menu, ready for next match
        //return;
        //}
        //if(matchInfo.playerdied)//do something? TODO design syncing
        //}
    }
Beispiel #6
0
    //part of the gamestate, player information(position, velocity, powerups)
    public void SendPlayerUpdates(PlayerUpdates updates)
    {
        Debug.Log("sending updates...");
        byte[] serializedData = SerializeClass(updates);
        for (int i = 0; i < Clients.Count; i++)
        {
            if (Clients[i] != null)
            {
                Debug.Log("sending updates to : " + Clients[i].endPoint.Port);
                sockets[i].Send(serializedData, serializedData.Length, Clients[i].endPoint);
            }
        }

        /*
         * //Debug.Log("sending player updates...");
         * foreach(UDPClient client in connectedClients)
         * {
         *  SendToClient(client, SerializeClass(updates));
         * }*/
    }
Beispiel #7
0
        public override void ReadPacket()
        {
            ReadHeaders(Packet);

            // Read the byte flag representing which packets
            // are included in this update
            var dataPacketIdFlag = Packet.ReadUShort();
            // Keep track of value of current bit
            var currentTypeValue = 1;

            for (var i = 0; i < Enum.GetNames(typeof(ClientPacketId)).Length; i++)
            {
                // If this bit was set in our flag, we add the type to the list
                if ((dataPacketIdFlag & currentTypeValue) != 0)
                {
                    DataPacketIds.Add((ClientPacketId)i);
                }

                // Increase the value of current bit
                currentTypeValue *= 2;
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerConnect))
            {
                PlayerConnect.ReadData(Packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerDisconnect))
            {
                PlayerDisconnect.ReadData(Packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerEnterScene))
            {
                PlayerEnterScene.ReadData(Packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerAlreadyInScene))
            {
                PlayerAlreadyInScene.ReadData(Packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerLeaveScene))
            {
                PlayerLeaveScene.ReadData(Packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerUpdate))
            {
                PlayerUpdates.ReadData(Packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.EntityUpdate))
            {
                EntityUpdates.ReadData(Packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerDeath))
            {
                PlayerDeath.ReadData(Packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerTeamUpdate))
            {
                PlayerTeamUpdate.ReadData(Packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerSkinUpdate))
            {
                PlayerSkinUpdate.ReadData(Packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerEmoteUpdate))
            {
                PlayerEmoteUpdate.ReadData(Packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.GameSettingsUpdated))
            {
                GameSettingsUpdate.ReadData(Packet);
            }
        }
Beispiel #8
0
        public override Packet CreatePacket()
        {
            var packet = new Packet();

            WriteHeaders(packet);

            // Construct the ushort flag representing which packets are included
            // in this update, we need a ushort since we have more than 8 possible packet IDs
            ushort dataPacketIdFlag = 0;
            // Keep track of value of current bit
            ushort currentTypeValue = 1;

            for (var i = 0; i < Enum.GetNames(typeof(ClientPacketId)).Length; i++)
            {
                // Cast the current index of the loop to a ClientPacketId and check if it is
                // contained in the update type list, if so, we add the current bit to the flag
                if (DataPacketIds.Contains((ClientPacketId)i))
                {
                    dataPacketIdFlag |= currentTypeValue;
                }

                currentTypeValue *= 2;
            }

            packet.Write(dataPacketIdFlag);

            if (DataPacketIds.Contains(ClientPacketId.PlayerConnect))
            {
                PlayerConnect.WriteData(packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerDisconnect))
            {
                PlayerDisconnect.WriteData(packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerEnterScene))
            {
                PlayerEnterScene.WriteData(packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerAlreadyInScene))
            {
                PlayerAlreadyInScene.WriteData(packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerLeaveScene))
            {
                PlayerLeaveScene.WriteData(packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerUpdate))
            {
                PlayerUpdates.WriteData(packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.EntityUpdate))
            {
                EntityUpdates.WriteData(packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerDeath))
            {
                PlayerDeath.WriteData(packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerTeamUpdate))
            {
                PlayerTeamUpdate.WriteData(packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerSkinUpdate))
            {
                PlayerSkinUpdate.WriteData(packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerEmoteUpdate))
            {
                PlayerEmoteUpdate.WriteData(packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.GameSettingsUpdated))
            {
                GameSettingsUpdate.WriteData(packet);
            }

            _containsReliableData = DataPacketIds.Contains(ClientPacketId.PlayerConnect) ||
                                    DataPacketIds.Contains(ClientPacketId.PlayerDisconnect) ||
                                    DataPacketIds.Contains(ClientPacketId.PlayerEnterScene) ||
                                    DataPacketIds.Contains(ClientPacketId.PlayerAlreadyInScene) ||
                                    DataPacketIds.Contains(ClientPacketId.PlayerLeaveScene) ||
                                    DataPacketIds.Contains(ClientPacketId.PlayerDeath) ||
                                    DataPacketIds.Contains(ClientPacketId.PlayerTeamUpdate) ||
                                    DataPacketIds.Contains(ClientPacketId.PlayerSkinUpdate) ||
                                    DataPacketIds.Contains(ClientPacketId.GameSettingsUpdated);

            packet.WriteLength();

            return(packet);
        }
Beispiel #9
0
    protected void DoPlayerUpdates(PlayerUpdates Pupdates)
    {
        string        test123 = "";
        PlayerUpdates updates = Pupdates;

        for (int i = 0; i < updates.PlayerInfos.Length; i++)
        {
            PlayerInfo     info = updates.PlayerInfos[i];
            PlayerMovement playerMov;


            playerMov = Array.Find(players, x => (int)x.playerID == info.playerID);

            GameObject player = playerMov.gameObject;

            //int oldPositionIndex = playerMov.oldPositionPointer - (int)Mathf.Round((GameTimer - Pupdates.gameTime) / 0.015f);

            /*
             * int oldPositionIndex = playerMov.oldPositionPointer - (int)((GameTimer - Pupdates.gameTime) / 0.015f);
             * oldPositionIndex = oldPositionIndex < 0 ? oldPositionIndex + playerMov.oldPositions.Length : oldPositionIndex;
             */
            //int oldPositionIndex = 5;

            if (true) //!playerMov.NetworkControl)
            {
                if (playerMov.networkPositions.Count < 3 || playerMov.networkPositions[playerMov.networkPositions.Count - 1].GameTime < updates.gameTime)
                {
                    playerMov.networkPositions.Add(new PlayerMovement.NetworkPosition(new Vector2(info.xPos, info.yPos), updates.gameTime, new Vector2(info.xVel, info.yVel)));
                }
            }

            /*
             * //Debug.Log((playerMov.oldPositions[oldPositionIndex] - new Vector2(info.xPos, info.yPos)).magnitude);
             * if (((playerMov.oldPositions[oldPositionIndex] - new Vector2(info.xPos, info.yPos)).magnitude > 3.55f && playerMov.NetworkControl) || !playerMov.NetworkControl)
             * {
             *  player.transform.position = new Vector3(info.xPos, info.yPos, player.transform.position.z);
             *  player.GetComponent<Rigidbody2D>().velocity = new Vector2(info.xVel, info.yVel);
             *
             * }
             * playerMov.testClone.transform.position = new Vector3(info.xPos, info.yPos, player.transform.position.z);
             * playerMov.testClone2.transform.position = playerMov.oldPositions[oldPositionIndex];
             */
            /*  //following debug information for players
             * for (int j = 0; j < playerMov.testClones.Length; j++)
             * {
             *  playerMov.testClones[j].transform.position = playerMov.oldPositions[j];
             * }*/


            /*
             * else if(playerMov.NetworkControl)
             * {
             *  Vector2 diffPos = playerMov.oldPositions[oldPositionIndex] - new Vector2(info.xPos, info.yPos);
             *  test123 += diffPos.magnitude;
             *  for (int j = 0; j < playerMov.oldPositions.Length; j++)
             *  {
             *      playerMov.oldPositions[j] -= (Vector2)diffPos;
             *  }
             *  player.transform.position -= (Vector3)diffPos;
             *  player.GetComponent<Rigidbody2D>().velocity = new Vector2(info.xVel, info.yVel);
             * }*/
        }
        testString = test123;
    }