// 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); } } }
private void AddEntity(EntityAddMessage<UmbraEntityType> msg) { long entityId = msg.EntityId; if(msg.EntityType == UmbraEntityType.Player) { CrawEntityManager.Instance.EntityFactory.CreatePlayer((long?)entityId, msg.Position); } else if(msg.EntityType == UmbraEntityType.NPC) { CrawEntityManager.Instance.EntityFactory.CreateNPC((long?)entityId, msg.Position); } }
public void Start() { //// TEMP //// Vector3 position = new Vector3(0, 0, 0); Entity npc = CrawEntityManager.Instance.EntityFactory.CreateNPC(null, position); EntityAddMessage<UmbraEntityType> msg = new EntityAddMessage<UmbraEntityType>(npc.UniqueId, UmbraEntityType.NPC, position); _networkAgent.BroadcastMessage(msg, true); //// TEMP //// }
public void Start() { //// TEMP //// Vector3 position = new Vector3(0, 0, 0); Entity npc = CrawEntityManager.Instance.EntityFactory.CreateNPC(null, position); EntityAddMessage <UmbraEntityType> msg = new EntityAddMessage <UmbraEntityType>(npc.UniqueId, UmbraEntityType.NPC, position); _networkAgent.BroadcastMessage(msg, true); //// TEMP //// }
private void AddEntity(EntityAddMessage <UmbraEntityType> msg) { long entityId = msg.EntityId; if (msg.EntityType == UmbraEntityType.Player) { CrawEntityManager.Instance.EntityFactory.CreatePlayer((long?)entityId, msg.Position); } else if (msg.EntityType == UmbraEntityType.NPC) { CrawEntityManager.Instance.EntityFactory.CreateNPC((long?)entityId, msg.Position); } }
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; }
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; }