public void HandleIncomingData(NetIncomingMessage msg) { MessageType type = (MessageType)msg.ReadInt32(); if (type == MessageType.PlayerMoved) { float x = msg.ReadFloat(); float y = msg.ReadFloat(); Entity.Entity.Direction direction = (Entity.Entity.Direction)msg.ReadInt32(); _connections[msg.SenderConnection.RemoteUniqueIdentifier].Player.SetCoordinates(new Vector2(x, y)); _connections[msg.SenderConnection.RemoteUniqueIdentifier].Player.SetDirection(direction); SendPlayerMove(msg.SenderConnection.RemoteUniqueIdentifier); } if (type == MessageType.MapChanged) { int j = msg.ReadInt32(); int i = msg.ReadInt32(); int tile = msg.ReadInt32(); _map.Layer2[j, i] = tile; SendMapChange(j, i, tile); } }
void HandleIncomingData(NetIncomingMessage msg) { MessageType type = (MessageType)msg.ReadInt32(); if (type == MessageType.ConnectionInfo) { _localId = msg.ReadInt64(); Players.Add(_localId, new Player()); Players[_localId].LoadContent(); Players[_localId].DrawAtOrigin = true; Player = Players[_localId]; } if (type == MessageType.PlayerDisconnected) { long id = msg.ReadInt64(); Players.Remove(id); } if (type == MessageType.MapInfo) { int rows = msg.ReadInt32(); int cols = msg.ReadInt32(); Map.Layer1 = new int[rows, cols]; Map.Layer2 = new int[rows, cols]; for (int j = 0; j < rows; j++) { for (int i = 0; i < cols; i++) { Map.Layer1[j, i] = msg.ReadInt32(); } } for (int j = 0; j < rows; j++) { for (int i = 0; i < cols; i++) { Map.Layer2[j, i] = msg.ReadInt32(); } } } if (type == MessageType.PlayerConnected) { long id = msg.ReadInt64(); float x = msg.ReadFloat(); float y = msg.ReadFloat(); Entity.Entity.Direction direction = (Entity.Entity.Direction)msg.ReadInt32(); Players.Add(id, new Player()); Players[id].LoadContent(); Players[id].SetCoordinates(new Vector2(x, y)); Players[id].SetDirection(direction); } if (type == MessageType.PlayerMoved) { long id = msg.ReadInt64(); float x = msg.ReadFloat(); float y = msg.ReadFloat(); Entity.Entity.Direction direction = (Entity.Entity.Direction)msg.ReadInt32(); if (Players.ContainsKey(id)) { if (id == _localId && direction == Entity.Entity.Direction.None) { Players[id].SetCoordinates(new Vector2(x, y)); } else if (id != _localId) { Players[id].SetCoordinates(new Vector2(x, y)); Players[id].SetDirection(direction); } } } if (type == MessageType.MapChanged) { int j = msg.ReadInt32(); int i = msg.ReadInt32(); int tile = msg.ReadInt32(); Map.Layer2[j, i] = tile; } }
public void Update() { if (_client.Map.Layer1 == null) { return; } if (Core.Input.IsKeyPressed(Keys.O)) { _optionsPanel.ToggleDisplay(); } if (_optionsPanel.Visible) { _optionsPanel.Update(); if (_optionsPanel.MouseIsIntersecting()) { return; } } var mstate = Mouse.GetState(); _selectedItem = (_selectedItem + Core.Input.GetScrollWheelDelta() + 3) % 3; var kstate = Keyboard.GetState(); Entity.Entity.Direction oldDirection = _client.Player.MovementDirection; if (kstate.IsKeyDown(Keys.W) && kstate.IsKeyDown(Keys.A)) { _client.Player.SetDirection(Entity.Entity.Direction.NorthWest); } else if (kstate.IsKeyDown(Keys.W) && kstate.IsKeyDown(Keys.D)) { _client.Player.SetDirection(Entity.Entity.Direction.NorthEast); } else if (kstate.IsKeyDown(Keys.S) && kstate.IsKeyDown(Keys.A)) { _client.Player.SetDirection(Entity.Entity.Direction.SouthWest); } else if (kstate.IsKeyDown(Keys.S) && kstate.IsKeyDown(Keys.D)) { _client.Player.SetDirection(Entity.Entity.Direction.SouthEast); } else if (kstate.IsKeyDown(Keys.W)) { _client.Player.SetDirection(Entity.Entity.Direction.North); } else if (kstate.IsKeyDown(Keys.A)) { _client.Player.SetDirection(Entity.Entity.Direction.West); } else if (kstate.IsKeyDown(Keys.S)) { _client.Player.SetDirection(Entity.Entity.Direction.South); } else if (kstate.IsKeyDown(Keys.D)) { _client.Player.SetDirection(Entity.Entity.Direction.East); } else { _client.Player.SetDirection(Entity.Entity.Direction.None); } // Send direction update if (_client.Player.MovementDirection != oldDirection) { _client.SendPlayerMove(); } // Send map changes if (Core.Input.LeftButtonDown() || Core.Input.RightButtonDown()) { int tileX = (int)Math.Floor(GetMouseTileCoordinates().X); int tileY = (int)Math.Floor(GetMouseTileCoordinates().Y); int i = (int)Math.Floor(GetMouseTileCoordinates().X); int j = (int)Math.Floor(GetMouseTileCoordinates().Y); int tileId = -1; if (Core.Input.LeftButtonDown()) { tileId = _selectedItem + 1; } if (tileX > 0 && tileX < _client.Map.Layer2.GetLength(1) - 1 && tileY > 0 && tileY < _client.Map.Layer2.GetLength(0) - 1) { if (_client.Map.Layer2[j, i] != tileId) { _client.Map.Layer2[j, i] = tileId; Networking.Client.Instance.SendMapChange(j, i); Engine.Content.Instance.PlaySound("Temp/hit"); } } } // Update each players foreach (Entity.Player player in _client.Players.Values) { player.Update(); } }