protected override void OnIncomingHandlableMessage(IEventMessage message, GameObjectEntitySpawnEventPayload payload, IMessageParameters parameters, InstanceClientPeer peer)
        {
            logger.Debug($"Recieved GameObject spawn event with Id: {payload.EntityGuid.EntityId} and Type: {payload.PrefabId}.");

            IEntitySpawnResults details = objectFactory.TrySpawnEntity(payload.Position.ToVector3(), payload.Rotation.ToQuaternion(),
                                                                       payload.Scale.ToVector3(), new NetworkGameObjectPrefabSpawnContext(payload.EntityGuid, payload.PrefabId, peer));

            if (details.Result != SpawnResult.Success)
            {
                Debug.LogError($"Failed to create GameObject for Entity Id: {payload.EntityGuid.EntityId} Type: {payload.PrefabId}.");
                return;
            }

            entityCollection.Add(payload.EntityGuid, new EntityContainer(payload.EntityGuid, details.EntityGameObject));

            //TODO: Set default state.
            IEntityStateContainer state = details.EntityGameObject.GetComponentInChildren <IEntityStateContainer>();

            if (state == null)
            {
                return;
            }

            state.State = payload.CurrentState;
        }
        protected override void OnIncomingHandlableMessage(IEventMessage message, PlayerEntitySpawnEventPayload payload, IMessageParameters parameters, InstanceClientPeer peer)
        {
            logger.Info($"Recieved spawn event for ID: {payload.EntityGuid.EntityId}.");

            IEntitySpawnResults details = playerFactory.SpawnPlayerEntity(payload.Position.ToVector3(), payload.Rotation.ToQuaternion(), new NetworkPlayerSpawnContext(payload.EntityGuid, peer));

            if (details.Result != SpawnResult.Success)
            {
                throw new InvalidOperationException($"Failed to spawn entity with Type: {payload.EntityGuid.EntityType} Id: {payload.EntityGuid.EntityId}.");
            }

            //TODO: Should we do this in the factory?
            //Add to collection
            entityCollection.Add(payload.EntityGuid, new EntityContainer(payload.EntityGuid, details.EntityGameObject));
        }
        protected override void OnIncomingHandlableMessage(IResponseMessage message, ClaimSessionResponsePayload payload, IMessageParameters parameters, InstanceClientPeer peer)
        {
            //TODO: Implement spawning
            if (Logger.IsInfoEnabled)
            {
                Logger.Info($"Recieved player spawn response. {payload.ResponseCode} {payload.Position} {payload.Rotation}");
            }

            GameObject entityGameObject = gameobjectFactory.CreateBuilder()
                                          .With(Service <INetPeer> .As(peer))
                                          .With(Service <NetworkEntityGuid> .As(payload.EntityGuid))
                                          .Create(playerPrefab, payload.Position.ToVector3(), payload.Rotation.ToQuaternion());

            //TODO: Should we do this in the factory?
            //Add to collection
            entityCollection.Add(payload.EntityGuid, new EntityContainer(payload.EntityGuid, entityGameObject));

            OnCreatedEntity(entityGameObject);
        }
Пример #4
0
        protected override void OnIncomingHandlableMessage(IRequestMessage message, PlayerSpawnRequestPayload payload, IMessageParameters parameters, InstanceClientSession peer)
        {
            //TODO: Right now we don't have a full handshake for entering an instance. So we need to make up a GUID for the entering player
            //Important to check if we've actually already created an entity for this connection
            //We don't have that implemenented though for the demo

            //rely on the factory implementation to handle placement details such as position and rotation
            NetworkEntityGuid   guid    = entityGuidFactory.Create(EntityType.Player);
            IEntitySpawnResults details = playerEntityFactory.SpawnPlayerEntity(new NetworkPlayerSpawnContext(guid, peer));

            if (details.Result != SpawnResult.Success)
            {
                throw new InvalidOperationException($"Failed to create Entity for {peer.ToString()}. Failure: {details.Result.ToString()}");
            }

            //TODO: This is temporary stuff for demo
            entityCollection.Add(guid, new EntityContainer(guid, details.EntityGameObject));
            peerCollection.Add(peer);
            connectionRegistry.Register(peer.PeerDetails.ConnectionID, guid);

            //TODO: Clean this up
            Vector3Surrogate pos = details.EntityGameObject.transform.position.ToSurrogate();

            QuaternionSurrogate rot = details.EntityGameObject.transform.rotation.ToSurrogate();

            //Send the response to the player who requested to spawn
            peer.SendResponse(new PlayerSpawnResponsePayload(PlayerSpawnResponseCode.Success, pos, rot, guid), DeliveryMethod.ReliableUnordered, true, 0);

            //TODO: Remove this. This is demo code.
            foreach (var entity in entityCollection.Values.Where(e => e.NetworkGuid.EntityType == EntityType.GameObject))
            {
                ITagProvider <GameObjectPrefab> prefabTag = entity.WorldObject.GetComponent <ITagProvider <GameObjectPrefab> >();
                IEntityStateContainer           state     = entity.WorldObject.GetComponentInChildren <IEntityStateContainer>();

                peer.SendEvent(new GameObjectEntitySpawnEventPayload(entity.NetworkGuid, entity.WorldObject.transform.position.ToSurrogate(),
                                                                     entity.WorldObject.transform.rotation.ToSurrogate(), entity.WorldObject.transform.localScale.ToSurrogate(), prefabTag.Tag, state.State),
                               DeliveryMethod.ReliableOrdered, false, 0);
            }
        }