/// <summary> /// The main entry point for the application. /// </summary> static void Main() { Write("SERVER", "Starting up."); NetPeerConfiguration config = new NetPeerConfiguration("Adventure"); config.Port = 14242; config.PingInterval = 2; config.ConnectionTimeout = 4; //config.EnableMessageType(NetIncomingMessageType.ConnectionApproval); Server = new NetServer(config); Server.Start(); clients = new Dictionary <long, Player>(); // ======================= LOOP ========================== while (!IsExiting) { DateTime start = DateTime.Now; #region Network Recieving try { NetIncomingMessage msg = null; while ((msg = Server.ReadMessage()) != null) { MessageHandle(msg); } } catch (Exception e) { Write("ERROR", e.ToString()); } #endregion // DO LOGIC HERE foreach (Player p in clients.Values) { if (p.NeedsSendingMovement) { PlayerMovePacket.New(p.Username, p.Position, p.Rotation).Send(Server); p.NeedsSendingMovement = false; } } /* FOR REFERENCE * // Broadcast the message: * NetOutgoingMessage om = server.CreateMessage(); * om.Write(senderName); * om.Write(text); * server.SendMessage(om, server.Connections, NetDeliveryMethod.ReliableOrdered, 0); */ // Reenable if we want set logic loop lengths. double loopDuration = DateTime.Now.Subtract(start).TotalMilliseconds; if (loopDuration < 1000f / LoopsPerSecond) { System.Threading.Thread.Sleep((int)(1000f / LoopsPerSecond - loopDuration)); } } }
public static void ParseDataMessage(NetIncomingMessage msg) { PacketType packetType = (PacketType)msg.ReadByte(); switch (packetType) { case PacketType.PlayerConnectPacket: PlayerConnectPacket connectPacket = PlayerConnectPacket.FromMessage(msg); clients.Add(msg.SenderConnection.RemoteUniqueIdentifier, new Player(connectPacket.PlayerName)); Write("SERVER", "PlayerConnectPacket from " + connectPacket.PlayerName + " (" + msg.SenderConnection.RemoteUniqueIdentifier.ToString() + ")"); PlayerListPacket.New(clients.Values.ToList()).Send(Server); // tell all clients when someone joins. break; case PacketType.PlayerChatPacket: Player p = clients[msg.SenderConnection.RemoteUniqueIdentifier]; PlayerChatPacket chatPacket = PlayerChatPacket.FromMessage(msg); Write(p.Username, chatPacket.Message); break; case PacketType.PlayerMovePacket: p = clients[msg.SenderConnection.RemoteUniqueIdentifier]; PlayerMovePacket movePacket = PlayerMovePacket.FromMessage(msg); Write(p.Username, "MOVEPACKET x=" + movePacket.Position.X + " y=" + movePacket.Position.Y + " r=" + movePacket.Rotation); p.Rotation = movePacket.Rotation; p.Position = movePacket.Position; p.NeedsSendingMovement = true; break; default: Write("SERVER", "Unknown packet type."); break; } }
public void OnPlayerMove(PlayerMoveEvent ev) { var fromChunkX = ev.From.X >> 4; var fromChunkY = ev.From.Y >> 4; var toChunkX = ev.From.X >> 4; var toChunkY = ev.From.Y >> 4; var toChunk = Server.Map.GetChunk(toChunkX, toChunkY); var nearPlayers = ev.Player.GetPlayersNear(); var movePacket = new PlayerMovePacket() { From = ev.From, To = ev.To, UserId = ev.Player.UserId }; foreach (var nearPlayer in nearPlayers) { nearPlayer.Tcp.Send(movePacket); } // Changed chunk if (fromChunkX != toChunkX || fromChunkY != toChunkY) { var fromChunk = Server.Map.GetChunk(fromChunkX, fromChunkY); fromChunk.PlayersInChunk.Remove(ev.Player); toChunk.PlayersInChunk.Add(ev.Player); } }
[EventMethod] // When client finishes updating assets public void OnPlayerMovePath(PlayerMovePacket packet) { var player = Server.GetPlayerByConnectionId(packet.ClientId); var distanceMoved = player.GetPosition().GetDistance(packet.To); var timeToMove = Formulas.GetTimeToMoveBetweenTwoTiles(player.speed); var now = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; var lastMovementArrival = now + timeToMove; // Player tryng to hack ? if (distanceMoved > 1 || now < player.CanMoveAgainTime) { // send player back to the position client-side player.Tcp.Send(new SyncPacket() { Position = player.GetPosition() }); return; } // subtract the player latency for possibility of lag for a smoother movement player.CanMoveAgainTime = now + timeToMove - player.Tcp.Latency; // Updating player position locally player.X = packet.To.X; player.Y = packet.To.Y; // updating in database PlayerService.UpdatePlayerPosition(player, player.X, player.Y); }
public void OnPlayerMove(PlayerMovePacket packet) { var playerObj = GameObject.Find(packet.UserId); if (playerObj != null) { var movingEntity = playerObj.GetComponent <MovingEntityBehaviour>(); movingEntity.Route.Add(packet.To); } }
public static void SendMoveDirectionPacket(IComponent moveComponent) { var entity = moveComponent.Entity; var data = new PlayerMovePacket() { XDirection = entity.XDirection, SteamId = moveComponent.Entity.EntityId }; var movePackage = NetworkManager.MessagePackager.Package(data); NetworkManager.SendMessage(movePackage); }
private void ReceivePlayerMove(IRecvPacket packet) { PlayerMovePacket p = (PlayerMovePacket)packet; announce_UnhandledPacket(packet); }
/// <summary> /// Called when a player move packet is received /// </summary> /// <param name="packet"></param> public void HandleMovePacket(PlayerMovePacket packet) { m_navAgent.SetDestination(packet.GetPosition()); }
public override void Update(GameTime gameTime) { NetIncomingMessage msg; while ((msg = Main.Client.ReadMessage()) != null) { if (msg.MessageType == NetIncomingMessageType.Data) { PacketType packetType = (PacketType)msg.ReadByte(); switch (packetType) { case PacketType.PlayerConnectPacket: PlayerConnectPacket connectPacket = PlayerConnectPacket.FromMessage(msg); break; case PacketType.PlayerChatPacket: PlayerChatPacket chatPacket = PlayerChatPacket.FromMessage(msg); break; case PacketType.PlayerMovePacket: PlayerMovePacket movePacket = PlayerMovePacket.FromMessage(msg); if (movePacket.Username == Program.UserName) { player.Rotation = movePacket.Rotation; player.Position = movePacket.Position; } else { } break; case PacketType.PlayerListPacket: PlayerListPacket listPacket = PlayerListPacket.FromMessage(msg); HandlePlayerList(listPacket.Players); break; default: break; } } } input.Update(gameTime, camera); colorCycle.Update(gameTime); camera.Update(gameTime); player.Update(gameTime, camera); if (input.NeedsMoveSend) { PlayerMovePacket.New(Program.UserName, input.Position, input.Rotation).Send(Main.Client); input.SentMove(); } foreach (Character npc in npcs) { npc.Update(gameTime, camera); } foreach (Character c in players.Values.AsEnumerable()) { c.Update(gameTime, camera); } cameraLock.Syncronize(); Bullet.Update(gameTime); }