Ejemplo n.º 1
0
        public override void OnCreate()
        {
            font = new Font("res/fonts/defaultFont.ttf");
            info = new Text("Server", font);

            // A separate thread to make connections on
            new Thread(() =>
            {
                while (!_closeRequested)
                {
                    Connection client = Connection.Listen(42069,
                                                          (connection, data) =>
                    {
                        if (data.Length == 0)
                        {
                            return;
                        }

                        // Responding for a packet
                        if (Packet.GetType(data) == PACKET_TYPES.PLAYER_UPDATE)
                        {
                            PlayerUpdatePacket packet = Packet.Deserialize <PlayerUpdatePacket>(data);

                            lock (_players)
                            {
                                if (_players.ContainsKey(connection))
                                {
                                    packet.id            = _players[connection].id;
                                    _players[connection] = packet;
                                }
                            }

                            if (packet.health <= 0.0f)
                            {
                                MoveToLobby(connection);
                            }
                        }

                        if (Packet.GetType(data) == PACKET_TYPES.PLAYER_SHOOT)
                        {
                            PlayerShootPacket packet = Packet.Deserialize <PlayerShootPacket>(data);

                            lock (_players)
                                if (_players.ContainsKey(connection))
                                {
                                    packet.id = _players[connection].id;
                                }

                            lock (_actions)
                                _actions.Enqueue(packet);
                        }
                    });

                    // Adding new client

                    lock (_lobbyClients)
                        _lobbyClients.Add(client);
                }
            }).Start();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Handle the player update packet received from the server
        /// </summary>
        /// <param name="buffer">packet buffer</param>
        private void HandlePlayerUpdate(Byte[] buffer)
        {
            PlayerUpdatePacket packet = new PlayerUpdatePacket();

            // Parse the player update packet
            packet.Packet = buffer;

            // Get the player information
            PlayerInfo playerInfo = m_playerHandler.PlayerInformation(packet.TeamEvent.PlayerId);

            if (playerInfo != null)
            {
                // Bugfix for shipchange event - PsyOps 12-4-15
                if ((int)packet.ShipEvent.ShipType == 255)
                {
                    packet.ShipEvent.ShipType = playerInfo.Ship;
                }
                else if ((int)packet.ShipEvent.PreviousShipType == 255)
                {
                    packet.ShipEvent.PreviousShipType = playerInfo.Ship;
                }

                // Check if the ship changed
                if (packet.ShipEvent.ShipType != playerInfo.Ship)
                {
                    // Set the player name and moderator level in the event
                    packet.ShipEvent.PlayerName       = playerInfo.PlayerName;
                    packet.ShipEvent.ModLevel         = playerInfo.ModeratorLevel;
                    packet.ShipEvent.PreviousShipType = playerInfo.Ship;

                    // Update the ship type in the player information
                    playerInfo.Ship = packet.ShipEvent.ShipType;

                    // Add the event to the core event queue
                    m_eventQueue.Add(packet.ShipEvent);
                }

                // Check if the frequency changed
                if (packet.TeamEvent.Frequency != playerInfo.Frequency)
                {
                    // Set the player name and moderator level in the event
                    packet.TeamEvent.PlayerName = playerInfo.PlayerName;
                    packet.TeamEvent.ModLevel   = playerInfo.ModeratorLevel;

                    // Update the frequency in the player information
                    playerInfo.Frequency = packet.TeamEvent.Frequency;

                    // Add the event to the core event queue
                    m_eventQueue.Add(packet.TeamEvent);
                }
            }
        }
Ejemplo n.º 3
0
        public override void Handle(NetPeer peer, DataReader dataReader)
        {
            // Later give a server-registered session id, and clean this code up, it smells
            PlayerSelfUpdatePacket playerSelfUpdatePacket = new PlayerSelfUpdatePacket(dataReader);

            foreach (KeyValuePair <(int, NetPeer), PositionData> playerPositionKVP in serverInteractor.playerPositions)
            {
                if (playerPositionKVP.Key.Item2 == peer)
                {
                    PlayerUpdatePacket playerUpdatePacket = new PlayerUpdatePacket(playerPositionKVP.Key.Item1, playerSelfUpdatePacket.playerPos);
                    serverInteractor.networkInteractor.BroadcastBut(peer.Id, playerUpdatePacket.ToPacket(), DeliveryMethod.Unreliable);
                }
            }
        }
Ejemplo n.º 4
0
 // Update is called once per frame
 void Update()
 {
     if (ownId != 0)
     {
         // if we are in the game, we can start processing input.
         if (Input.GetKeyDown(KeyCode.W))
         {
             var newDir       = Direction.Up;
             var playerUpdate = new PlayerUpdatePacket(newDir);
             netManager.Send(playerUpdate);
         }
         if (Input.GetKeyDown(KeyCode.A))
         {
             var newDir       = Direction.Left;
             var playerUpdate = new PlayerUpdatePacket(newDir);
             netManager.Send(playerUpdate);
         }
         if (Input.GetKeyDown(KeyCode.S))
         {
             var newDir       = Direction.Down;
             var playerUpdate = new PlayerUpdatePacket(newDir);
             netManager.Send(playerUpdate);
         }
         if (Input.GetKeyDown(KeyCode.D))
         {
             var newDir       = Direction.Right;
             var playerUpdate = new PlayerUpdatePacket(newDir);
             netManager.Send(playerUpdate);
         }
     }
     if (endGameTarget != -1)
     {
         victoryImage.SetActive(true);
         var pos = players[endGameTarget].transform.position;
         Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, new Vector3(pos.x, pos.y, Camera.main.transform.position.z), 0.02f);
         Camera.main.orthographicSize   = Mathf.Lerp(Camera.main.orthographicSize, 40, 0.02f);
     }
     else
     {
         Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, initCameraPos, 0.02f);
         Camera.main.orthographicSize   = Mathf.Lerp(Camera.main.orthographicSize, initCameraSize, 0.02f);
     }
 }
        public static byte[] MakePlayerUpdatedBuffer(Vector2 touchWorldPos)
        {
            var builder = new FlatBufferBuilder(DefaultBufferBytes);

            PlayerUpdatePacket.StartPlayerUpdatePacket(builder);

            PlayerUpdatePacket.AddOpcode(builder, (short)Opcode.PlayerUpdate);

            var positionOffset = Vec2.CreateVec2(builder, touchWorldPos.x, touchWorldPos.y);

            PlayerUpdatePacket.AddPosX(builder, touchWorldPos.x);
            PlayerUpdatePacket.AddPosY(builder, touchWorldPos.y);

            var playerUpdatedOffset = PlayerUpdatePacket.EndPlayerUpdatePacket(builder);

            builder.Finish(playerUpdatedOffset.Value);

            return(builder.SizedByteArray());
        }
Ejemplo n.º 6
0
        protected override Task OnMessage(MessageEventArgs e)
        {
            using (System.IO.MemoryStream memStream = new System.IO.MemoryStream())
                using (System.IO.BinaryReader reader = new System.IO.BinaryReader(memStream))
                {
                    e.Data.CopyTo(memStream);
                    memStream.Position = 0;

                    PacketType packetType = (PacketType)reader.ReadByte();
                    Packet     packet;

                    switch (packetType)
                    {
                    case PacketType.PlayerAction:
                        packet = PlayerActionPacket.Parse(reader);
                        break;

                    case PacketType.Ping:
                        packet = PingPacket.Parse(reader);
                        break;

                    case PacketType.PlayerUpdate:
                        packet = PlayerUpdatePacket.Parse(reader, this);
                        break;

                    case PacketType.PlayerShoot:
                        packet = PlayerShootPacket.Parse(reader);
                        break;

                    default:
                        return(base.OnMessage(e)); // Packet not recognized. Ignore it.
                    }

                    packet.PacketType = packetType;
                    packet.Handle(this);

                    return(base.OnMessage(e));
                }
        }
Ejemplo n.º 7
0
		internal void EmitPlayerUpdate(PlayerUpdatePacket packet)
		{
			Position = packet.State.Position;
			PlayerUpdate?.Invoke(this, packet);
		}
Ejemplo n.º 8
0
 private void ProcessPacket(PlayerUpdatePacket updatePacket)
 {
     Playground.Players.Single(player => player.Id == updatePacket.Id).Update(updatePacket.Position, updatePacket.Heading);
 }