// this is a little hacky as there aren't actually any entities to process // probably a sign that I'm abusing the entity component system. i think the proper // way to do this is to create a simple entity that just has a component to hold // a reference to the network agent. that's roughly how it would work in unity. // might be best to use a TagSystem like for player input? protected override void ProcessEntities(IDictionary<int, Entity> entities) { List<NetIncomingMessage> messages = _networkAgent.ReadMessages(); foreach(NetIncomingMessage netMessage in messages) { NetworkMessageType messageType = (NetworkMessageType)Enum.ToObject(typeof(NetworkMessageType), netMessage.ReadByte()); if(messageType == NetworkMessageType.PlayerConnect) { PlayerConnectMessage<UmbraEntityType> playerConnectMessage = new PlayerConnectMessage<UmbraEntityType>(); playerConnectMessage.Decode(netMessage); PlayerConnect(playerConnectMessage); } else if(messageType == NetworkMessageType.EntityAdd) { EntityAddMessage<UmbraEntityType> addMessage = new EntityAddMessage<UmbraEntityType>(); addMessage.Decode(netMessage); AddEntity(addMessage); } else if(messageType == NetworkMessageType.EntityMove) { EntityMoveMessage moveMessage = new EntityMoveMessage(); moveMessage.Decode(netMessage); MoveEntity(moveMessage); } else if(messageType == NetworkMessageType.EntityRemove) { EntityRemoveMessage removeMessage = new EntityRemoveMessage(); removeMessage.Decode(netMessage); RemoveEntity(removeMessage); } } }
// this is a little hacky as there aren't actually any entities to process // probably a sign that I'm abusing the entity component system. i think the proper // way to do this is to create a simple entity that just has a component to hold // a reference to the network agent. that's roughly how it would work in unity. // might be best to use a TagSystem like for player input? protected override void ProcessEntities(IDictionary <int, Entity> entities) { List <NetIncomingMessage> messages = _networkAgent.ReadMessages(); foreach (NetIncomingMessage netMessage in messages) { NetworkMessageType messageType = (NetworkMessageType)Enum.ToObject(typeof(NetworkMessageType), netMessage.ReadByte()); if (messageType == NetworkMessageType.PlayerConnect) { PlayerConnectMessage <UmbraEntityType> playerConnectMessage = new PlayerConnectMessage <UmbraEntityType>(); playerConnectMessage.Decode(netMessage); PlayerConnect(playerConnectMessage); } else if (messageType == NetworkMessageType.EntityAdd) { EntityAddMessage <UmbraEntityType> addMessage = new EntityAddMessage <UmbraEntityType>(); addMessage.Decode(netMessage); AddEntity(addMessage); } else if (messageType == NetworkMessageType.EntityMove) { EntityMoveMessage moveMessage = new EntityMoveMessage(); moveMessage.Decode(netMessage); MoveEntity(moveMessage); } else if (messageType == NetworkMessageType.EntityRemove) { EntityRemoveMessage removeMessage = new EntityRemoveMessage(); removeMessage.Decode(netMessage); RemoveEntity(removeMessage); } } }
static void SERVER_OnClientConnected(ConnectedClient client) // this will NOT run in the main thread { ThreadSafeDebug.Log("Client connected from " + client.TcpConnection.RemoteEndPoint.ToString()); string levelPath = LevelManager.Instance.GetCurrentLevelDescription().LevelJSONPath; byte[] bytes = File.ReadAllBytes(levelPath); MapSendingMessge.SendTo(bytes, client.ClientNetworkID); // send map to other client ushort playerID = GetNextPlayerID(); SetLocalPlayerMessage.SendTo(playerID, client.ClientNetworkID); var createdPlayerInfo = new PlayerConnectedMessage.CreatedPlayerInfo() { CharacterModelOverrideType = EnemyType.None, PlayerColor = HumanFactsManager.Instance.FavouriteColors[HumanFactsManager.Instance.GetRandomColorIndex()].ColorValue, PlayerID = playerID, PlayerUpgrades = new Dictionary <UpgradeType, int>() }; PlayerConnectMessage.Send(createdPlayerInfo); var spawnPlayerMessage = new SpawnPlayerMessage.SpawnedPlayerInfo() { PlayerID = playerID, Position = new Vector3(0f, 10f, 0f), Rotation = 0f }; SpawnPlayerMessage.Send(spawnPlayerMessage); }
// Start is called before the first frame update public override void OnClientConnect(NetworkConnection conn) { base.OnClientConnect(conn); PlayerConnectMessage msg = new PlayerConnectMessage { data = "Ok" }; conn.Send(msg); }
// Start is called before the first frame update public override void OnClientConnect(NetworkConnection conn) { base.OnClientConnect(conn); // call this to use this gameobject as the primary controller //NetworkServer.AddPlayerForConnection(conn, gameobject); PlayerConnectMessage msg = new PlayerConnectMessage { data = "Ok" }; conn.Send(msg); }
private void PlayerConnect(PlayerConnectMessage <UmbraEntityType> msg) { long entityId = msg.EntityId; Entity player = CrawEntityManager.Instance.EntityFactory.CreatePlayer((long?)entityId, msg.Position); if (msg.IsSelf) { player.Tag = "PLAYER"; // TODO: this probably shouldn't happen here CameraComponent camera = BlackBoard.GetEntry <CameraComponent>("Camera"); camera.Target = player.GetComponent <TransformComponent>(); } }
// Attribue un prefab de Joueur à la personne qui se connecte void OnCreatePlayer(NetworkConnection conn, PlayerConnectMessage message) { // playerPrefab is the one assigned in the inspector in Network // Manager but you can use different prefabs per race for example Camera c = FindObjectOfType <Camera>(); GameObject g = Instantiate <GameObject>(playerPrefab); g.GetComponent <NetworkPlayerController>().currentCamera = c; //Debug.Log(gameObject.GetComponent<NetworkPlayerController>().currentCamera); // call this to use this gameobject as the primary controller Debug.Log("Connexion du client " + conn.connectionId + ", Nombre de joueurs : " + NetworkServer.connections.Count); NetworkServer.AddPlayerForConnection(conn, g); }
void OnCreateCharacter(NetworkConnection conn, PlayerConnectMessage message) { Debug.Log("Jello ?"); // playerPrefab is the one assigned in the inspector in Network // Manager but you can use different prefabs per race for example Camera c = FindObjectOfType <Camera>(); Debug.Log(c); GameObject g = Instantiate <GameObject>(playerPrefab); g.GetComponent <NetworkPlayerController>().currentCamera = c; //Debug.Log(gameObject.GetComponent<NetworkPlayerController>().currentCamera); // call this to use this gameobject as the primary controller NetworkServer.AddPlayerForConnection(conn, g); }
private void OnPlayerConnect(NetConnection playerConnection) { EntityAddMessage <UmbraEntityType> entityAddMessage; Bag <Entity> entities = _entityWorld.EntityManager.GetEntities(Aspect.All(typeof(UmbraEntityTypeComponent))); // send message to connecting player about existing entities foreach (Entity entity in entities) { UmbraEntityTypeComponent entityType = entity.GetComponent <UmbraEntityTypeComponent>(); TransformComponent transform = entity.GetComponent <TransformComponent>(); entityAddMessage = new EntityAddMessage <UmbraEntityType>(entity.UniqueId, entityType.EntityType, transform.Position); _networkAgent.SendMessage(entityAddMessage, playerConnection); } // send message confirming player's connection PlayerConnectMessage <UmbraEntityType> playerConnectMessage; Vector3 startPos = new Vector3(10, 0, 10); Entity player = CrawEntityManager.Instance.EntityFactory.CreatePlayer(null, startPos); playerConnectMessage = new PlayerConnectMessage <UmbraEntityType>(player.UniqueId, UmbraEntityType.Player, startPos, true); _networkAgent.SendMessage(playerConnectMessage, playerConnection); // message other clients about player's connection playerConnectMessage = new PlayerConnectMessage <UmbraEntityType>(player.UniqueId, UmbraEntityType.Player, startPos, false); foreach (NetConnection connection in _networkAgent.Connections) { if (connection != playerConnection) { _networkAgent.SendMessage(playerConnectMessage, connection); } } _playerEntityIds[playerConnection] = player.UniqueId; }
internal static IRTCommand GetCommand(int opCode, int sender, int?sequence, Stream stream, IRTSessionInternal session, RTData data, int packetSize) { long limit = global::GameSparks.RT.Proto.ProtocolParser.ReadUInt32(stream); LimitedPositionStream lps = PooledObjects.LimitedPositionStreamPool.Pop(); try{ lps.Wrap(stream, limit); switch (opCode) { case OpCodes.LoginResult: return(LoginResult.Deserialize(lps, LoginResult.pool.Pop())); case OpCodes.PingResult: return(PingResult.Deserialize(lps, PingResult.pool.Pop())); case OpCodes.UDPConnectMessage: return(UDPConnectMessage.Deserialize(lps, UDPConnectMessage.pool.Pop())); case OpCodes.PlayerConnectMessage: return(PlayerConnectMessage.Deserialize(lps, PlayerConnectMessage.pool.Pop())); case OpCodes.PlayerDisconnectMessage: return(PlayerDisconnectMessage.Deserialize(lps, PlayerDisconnectMessage.pool.Pop())); default: if (session.ShouldExecute(sender, sequence)) { return(CustomCommand.pool.Pop().Configure(opCode, sender, lps, data, (int)limit, session, packetSize)); } return(null); } } finally { lps.SkipToEnd(); PooledObjects.LimitedPositionStreamPool.Push(lps); } }
private void PlayerConnect(PlayerConnectMessage<UmbraEntityType> msg) { long entityId = msg.EntityId; Entity player = CrawEntityManager.Instance.EntityFactory.CreatePlayer((long?)entityId, msg.Position); if(msg.IsSelf) { player.Tag = "PLAYER"; // TODO: this probably shouldn't happen here CameraComponent camera = BlackBoard.GetEntry<CameraComponent>("Camera"); camera.Target = player.GetComponent<TransformComponent>(); } }
private void OnPlayerConnect(NetConnection playerConnection) { EntityAddMessage<UmbraEntityType> entityAddMessage; Bag<Entity> entities = _entityWorld.EntityManager.GetEntities(Aspect.All(typeof(UmbraEntityTypeComponent))); // send message to connecting player about existing entities foreach(Entity entity in entities) { UmbraEntityTypeComponent entityType = entity.GetComponent<UmbraEntityTypeComponent>(); TransformComponent transform = entity.GetComponent<TransformComponent>(); entityAddMessage = new EntityAddMessage<UmbraEntityType>(entity.UniqueId, entityType.EntityType, transform.Position); _networkAgent.SendMessage(entityAddMessage, playerConnection); } // send message confirming player's connection PlayerConnectMessage<UmbraEntityType> playerConnectMessage; Vector3 startPos = new Vector3(10, 0, 10); Entity player = CrawEntityManager.Instance.EntityFactory.CreatePlayer(null, startPos); playerConnectMessage = new PlayerConnectMessage<UmbraEntityType>(player.UniqueId, UmbraEntityType.Player, startPos, true); _networkAgent.SendMessage(playerConnectMessage, playerConnection); // message other clients about player's connection playerConnectMessage = new PlayerConnectMessage<UmbraEntityType>(player.UniqueId, UmbraEntityType.Player, startPos, false); foreach(NetConnection connection in _networkAgent.Connections) { if(connection != playerConnection) { _networkAgent.SendMessage(playerConnectMessage, connection); } } _playerEntityIds[playerConnection] = player.UniqueId; }