Example #1
0
 public override GameMessage GetSpawnMessage()
 {
     GameMessage msg = new GameMessage();
     msg.DataType = GameState.DataTypeSpawnEntity;
     msg.index = ID;
     byte[] array = new byte[24];
     BitConverter.GetBytes(Position.X).CopyTo(array, 0);
     BitConverter.GetBytes(Position.Y).CopyTo(array, 4);
     BitConverter.GetBytes(Velocity.X).CopyTo(array, 8);
     BitConverter.GetBytes(Velocity.Y).CopyTo(array, 12);
     BitConverter.GetBytes(typeID).CopyTo(array, 16);
     BitConverter.GetBytes(health).CopyTo(array, 20);
     msg.SetMessage(array);
     return msg;
 }
Example #2
0
 public override ONet.GameMessage GetStateMessage()
 {
     GameMessage msg = new GameMessage();
     msg.DataType = GameState.DataTypeEntityUpdate;
     msg.index = ID;
     byte[] array = new byte[20];
     BitConverter.GetBytes(Position.X).CopyTo(array, 0);
     BitConverter.GetBytes(Position.Y).CopyTo(array, 4);
     BitConverter.GetBytes(Velocity.X).CopyTo(array, 8);
     BitConverter.GetBytes(Velocity.Y).CopyTo(array, 12);
     BitConverter.GetBytes(health).CopyTo(array, 16);
     msg.SetMessage(array);
     return msg;
 }
Example #3
0
 public GameMessage ReassignIndexMessage(int oldIndex, int newIndex)
 {
     GameMessage msg = new GameMessage();
     msg.DataType = GameState.DataTypeReassignID;
     msg.index = oldIndex;
     byte[] array = new byte[4];
     BitConverter.GetBytes(newIndex).CopyTo(array, 0);
     msg.SetMessage(array);
     return msg;
 }
Example #4
0
 public override GameMessage GetSpawnMessage()
 {
     GameMessage msg = new GameMessage();
     msg.DataType = GameState.DataTypeSpawnEntity;
     msg.index = ID;
     byte[] array = new byte[28];
     BitConverter.GetBytes(Position.X).CopyTo(array, 0);
     BitConverter.GetBytes(Position.Y).CopyTo(array, 4);
     BitConverter.GetBytes(Velocity.X).CopyTo(array, 8);
     BitConverter.GetBytes(Velocity.Y).CopyTo(array, 12);
     BitConverter.GetBytes(typeID).CopyTo(array, 16);
     BitConverter.GetBytes(ownerID).CopyTo(array, 20);
     array[24] = color.R;
     array[25] = color.G;
     array[26] = color.B;
     BitConverter.GetBytes(isDown).CopyTo(array, 27);
     msg.SetMessage(array);
     return msg;
 }
Example #5
0
 void Respawn()
 {
     GameMessage msg = new GameMessage();
     msg.DataType = DataTypeRequest;
     msg.index = IndexRespawnShip;
     msg.SetMessage(new byte[1]);
     _messages.Add(msg);
     respawning = false;
     overlay.IsVisible = false;
 }
Example #6
0
 public GameMessage GetStateMessage()
 {
     GameMessage msg = new GameMessage();
     byte[] array = new byte[20];
     msg.index = _id;
     msg.DataType = GameState.DataTypeEntityUpdate;
     BitConverter.GetBytes(_position.X).CopyTo(array, 0);
     BitConverter.GetBytes(_position.Y).CopyTo(array, 4);
     BitConverter.GetBytes((float)0.0f).CopyTo(array, 8);
     BitConverter.GetBytes((float)0.0f).CopyTo(array, 12);
     BitConverter.GetBytes((float)0.0f).CopyTo(array, 16);
     msg.SetMessage(array);
     return msg;
 }
Example #7
0
 private void InitialisePlayerShip(int clientIndex)
 {
     GameMessage initMessage = new GameMessage();
     initMessage.DataType = GameState.DataTypeMetaInfo;
     initMessage.index = GameState.IndexInitialisePlayerShip;
     byte[] arr = new byte[4];
     BitConverter.GetBytes(playerInfo[clientIndex].EntityID).CopyTo(arr, 0);
     initMessage.SetMessage(arr);
     messages[clientIndex].Add(initMessage);
 }
Example #8
0
 private void GenerateMetaInfo()
 {
     int numPlayers = 0;
     int deadPlayers = 0;
     List<int> deadShips = new List<int>();
     foreach (KeyValuePair<int, PlayerShip> kvp in ships)
     {
         ++numPlayers;
         if (kvp.Value.isDead)
         {
             RemoveEntity(kvp.Value);
             GameMessage deathMessage = new GameMessage();
             deathMessage.DataType = DataTypeMetaInfo;
             deathMessage.index = IndexPlayerDeath;
             deathMessage.SetMessage(new byte[1]);
             playerInfo[kvp.Key].Alive = false;
             messages[kvp.Key].Add(deathMessage);
             ++deadPlayers;
             deadShips.Add(kvp.Key);
         }
         GameMessage healthMessage = new GameMessage();
         healthMessage.DataType = DataTypeMetaInfo;
         healthMessage.index = IndexHealthUpdate;
         healthMessage.SetMessage(BitConverter.GetBytes(kvp.Value.health));
         GameMessage scoreMessage = new GameMessage();
         scoreMessage.DataType = DataTypeMetaInfo;
         scoreMessage.index = IndexScoreUpdate;
         scoreMessage.SetMessage(BitConverter.GetBytes(playerInfo[kvp.Key].Score));
         messages[kvp.Key].Add(healthMessage);
         messages[kvp.Key].Add(scoreMessage);
     }
     foreach (int index in deadShips)
     {
         ships.Remove(index);
     }
     /*
     if (deadPlayers == numPlayers)
     {
         GameMessage gameOverMessage = new GameMessage();
         gameOverMessage.DataType = DataTypeMetaInfo;
         gameOverMessage.index = IndexGameOver;
         gameOverMessage.SetMessage(new byte[1]);
         broadcastMessages.Add(gameOverMessage);
         gameActive = false;
     }
     */
 }
Example #9
0
        public void ClientConnect(int clientNumber, GameMessage message)
        {
            try
            {
                playerInfo.Add(clientNumber, new PlayerInfo(clientNumber));
                _messageStacks.Add(clientNumber, new MessageStack<GameMessage>(500));
                messages.Add(clientNumber, new List<GameMessage>());

                _infoStack.Push(String.Format("Client {0} connected from address {1}", clientNumber, _server.Connections[clientNumber].Socket.RemoteEndPoint));
                PlayerShip ship = new PlayerShip();
                int clientShipIndex = AddEntity(ship);
                ship.Place(new Vector2(rand.Next(Game1.width - 100) + 50.0f, Game1.height - 50.0f));
                ship.color = shipColors[clientNumber % 16];

                ships.Add(clientNumber, ship);

                foreach (IEntity entity in entities.Values)
                {
                    if (entity != ship)
                    {
                        messages[clientNumber].Add(entity.GetSpawnMessage());
                    }
                }

                foreach (KeyValuePair<int, List<GameMessage>> kvp in messages)
                {
                    kvp.Value.Add(ship.GetSpawnMessage());
                }

                GameMessage initMessage = new GameMessage();
                initMessage.DataType = GameState.DataTypeMetaInfo;
                initMessage.index = GameState.IndexInitialisePlayerShip;
                byte[] arr = new byte[7];
                BitConverter.GetBytes(clientShipIndex).CopyTo(arr, 0);

                arr[4] = shipColors[clientNumber % 16].R;
                arr[5] = shipColors[clientNumber % 16].G;
                arr[6] = shipColors[clientNumber % 16].B;

                initMessage.SetMessage(arr);
                messages[clientNumber].Add(initMessage);

                playerInfo[clientNumber].EntityID = clientShipIndex;
                lastMessage.Add(clientNumber, lastTime);

            }
            catch (Exception e)
            {
                _errorStack.Push(e.Message);
            }
        }