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;
        }
Example #2
0
        /// <summary>
        /// Dispatched when a guid is generated.
        /// </summary>
        /// <typeparam name="TPeer">The peer type.</typeparam>
        /// <param name="response"></param>
        /// <param name="peer">The peer.</param>
        protected virtual void OnSessionClaimResponse <TPeer>(ServerSessionClaimResponse response, TPeer peer)
            where TPeer : INetPeer, IResponsePayloadSender
        {
            //TODO: Logging
            //TODO: Should we disconnect them? Should we just prevent them from sending more requests?
            //If the session inquiry failed then we should fail the session claim request.
            if (!response.isSuccessful)
            {
                peer.SendResponse(new ClaimSessionResponsePayload(PlayerSpawnResponseCode.Unknown), DeliveryMethod.ReliableUnordered, true, 0);
                return;
            }

            NetworkEntityGuid guid = NetworkEntityGuidBuilder.New()
                                     .WithId(response.UserId)
                                     .WithType(EntityType.Player)
                                     .Build();

            IEntitySpawnResults details = PlayerEntityFactory.SpawnPlayerEntity(new NetworkPlayerSpawnContext(guid, peer));

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

            //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 ClaimSessionResponsePayload(pos, rot, guid), DeliveryMethod.ReliableUnordered, true, 0);
        }
        public IEntitySpawnResults TrySpawnEntity(Vector3 position, Quaternion rotation, Vector3 scale, ISpawnContext context)
        {
            IEntitySpawnResults results = TrySpawnEntity(position, rotation, context);

            if (results.Result == SpawnResult.Success)
            {
                results.EntityGameObject.transform.localScale = scale;
            }

            return(results);
        }
        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));
        }
Example #5
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);
            }
        }
 protected virtual IEntitySpawnResults OnEntityConstructionCompleted(IPlayerSpawnContext context, IEntitySpawnResults result)
 {
     //Access point that allows implementers of this type to extend the construction process.
     return(result);
 }
        /// <inheritdoc />
        protected override IEntitySpawnResults OnEntityConstructionCompleted([NotNull] IPlayerSpawnContext context, [NotNull] IEntitySpawnResults result)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (result == null)
            {
                throw new ArgumentNullException(nameof(result));
            }

            //Check if the spawn was a success. If not we don't want to add information about it to the network services.
            if (result.Result != SpawnResult.Success)
            {
                return(result);
            }

            //TODO: Verify that none of this is in the services/collections all ready. MAJOR ISSUES if we are getting multiple of the same entity.
            EntityCollection.Add(context.EntityGuid, new EntityContainer(context.EntityGuid, result.EntityGameObject));
            PeerCollection.Add(context.OwnerPeer);
            ConnectionRegistry.Register(context.OwnerPeer.PeerDetails.ConnectionID, context.EntityGuid);

            return(result);
        }