public void SendWorldUpdateToAllClients() { List <GameObjectServer> gameObjects = GameServer.instance.GetInteractableObjects(); List <byte> allPackets = new List <byte>(); for (int i = 0; i < gameObjects.Count; i++) { GameObjectServer objectToSend = gameObjects[i]; //Send an update if the object is not a leaf or if the leaf has been modified. if ((objectToSend is LeafServer && objectToSend.Modified) || (objectToSend is PlayerServer)) { objectToSend.Modified = false; BasePacket packetToSend = ServerPacketFactory.CreateUpdatePacket(gameObjects[i]); allPackets.AddRange(PacketUtil.Serialize(packetToSend)); } } SendAll(allPackets.ToArray()); List <byte> destroyPackets = new List <byte>(); lock (GameServer.instance.toDestroyQueue) { foreach (var gameObj in GameServer.instance.toDestroyQueue) { BasePacket packet = PacketFactory.NewDestroyPacket(gameObj); destroyPackets.AddRange(PacketUtil.Serialize(packet)); } } SendAll(destroyPackets.ToArray()); }
public PlayerServer CreateNewPlayer() { //Assign id based on the next spot in the gameObjectDict. int id = gameObjectDict.Count(); //Create two players, one to send as an active player to client. Other to keep track of on server. PlayerServer newPlayer = matchHandler.AddPlayer(); newPlayer.Register(); playerServerList.Add(newPlayer); //Create the active player with the same id as the newPlayer. PlayerServer newActivePlayer = new PlayerServer(newPlayer.Team) { ObjectType = ObjectType.ACTIVE_PLAYER, Id = newPlayer.Id }; newActivePlayer.Transform.Position = newPlayer.Transform.Position; CreatePlayerPacket objPacket = (CreatePlayerPacket)ServerPacketFactory.NewCreatePacket(newPlayer); // Sending this new packet before the new client joins. networkServer.SendAll(PacketUtil.Serialize(objPacket)); return(newActivePlayer); }
/// <summary> /// Sends all the game objects that exist within the game world to /// a client /// </summary> /// <param name="clientSocket"> /// The client to send all the game objects in the world to /// </param> private void SendWorldToClient(Socket clientSocket) { List <byte> allWorldPackets = new List <byte>(); List <GameObjectServer> world = GameServer.instance.GetGameObjectList(); foreach (GameObjectServer val in world) { BasePacket packetToSend = ServerPacketFactory.NewCreatePacket(val); allWorldPackets.AddRange(PacketUtil.Serialize(packetToSend)); } clientSocket.Send(allWorldPackets.ToArray()); }
/// <summary> /// Creates a new player in the game, sends it out to all the clients, /// and then sends that active player to the clientSocket that is /// specified /// </summary> /// <param name="clientSocket"> /// The socket that needs an active player /// </param> private void ProcessNewPlayer(Socket clientSocket) { PlayerServer player = GameServer.instance.CreateNewPlayer(); //Associate player's id with with the socket. playerDictionary.Add(clientSocket, player.Id); BasePacket createPlayPack = ServerPacketFactory.NewCreatePacket(player); // Create createObjectPacket, send to client byte[] data = PacketUtil.Serialize(createPlayPack); Send(clientSocket, data); MatchStartPacket informStart = new MatchStartPacket(MatchHandler.instance.GetMatch().GetTimeElapsed().Milliseconds); Send(clientSocket, PacketUtil.Serialize(informStart)); }