Esempio n. 1
0
        /// <summary>
        ///     Attaches a player session to an entity, optionally kicking any sessions already attached to it.
        /// </summary>
        /// <param name="entity">The entity to attach the player to</param>
        /// <param name="player">The player to attach to the entity</param>
        /// <param name="force">Whether to kick any existing players from the entity</param>
        /// <param name="forceKicked">The player that was forcefully kicked, or null.</param>
        /// <returns>Whether the attach succeeded, or not.</returns>
        public bool Attach(IEntity entity, IPlayerSession player, bool force, out IPlayerSession?forceKicked)
        {
            // Null by default.
            forceKicked = null;

            // Cannot attach to a deleted entity.
            if (entity.Deleted)
            {
                return(false);
            }

            var uid = entity.Uid;

            // Check if there was a player attached to the entity already...
            if (ComponentManager.TryGetComponent(uid, out ActorComponent actor))
            {
                // If we're not forcing the attach, this fails.
                if (!force)
                {
                    return(false);
                }

                // Set the event's force-kicked session before detaching it.
                forceKicked = actor.PlayerSession;

                // This detach cannot fail, as a player is attached to this entity.
                // It's important to note that detaching the player removes the component.
                RaiseLocalEvent(uid, new DetachPlayerEvent());
            }

            // We add the actor component.
            actor = ComponentManager.AddComponent <ActorComponent>(entity);
            actor.PlayerSession = player;
            player.SetAttachedEntity(entity);

            // The player is fully attached now, raise an event!
            RaiseLocalEvent(uid, new PlayerAttachedEvent(entity, player, forceKicked));
            return(true);
        }