コード例 #1
0
    /// <summary>
    /// Called where you want the user to start blocking
    /// Creates a new hard fixture to bodyblock
    /// Also makes the user static to prevent prediction issues
    /// </summary>
    /// <param name="uid"> The entity with the blocking component</param>
    /// <param name="component"> The <see cref="BlockingComponent"/></param>
    /// <param name="user"> The entity who's using the item to block</param>
    /// <returns></returns>
    public bool StartBlocking(EntityUid item, BlockingComponent component, EntityUid user)
    {
        if (component.IsBlocking)
        {
            return(false);
        }

        var xform = Transform(user);

        var shieldName = Name(item);

        var blockerName = Identity.Entity(user, EntityManager);
        var msgUser     = Loc.GetString("action-popup-blocking-user", ("shield", shieldName));
        var msgOther    = Loc.GetString("action-popup-blocking-other", ("blockerName", blockerName), ("shield", shieldName));

        if (component.BlockingToggleAction != null)
        {
            //Don't allow someone to block if they're in a container.
            if (_containerSystem.IsEntityInContainer(user) || !_mapManager.TryFindGridAt(xform.MapPosition, out var grid))
            {
                CantBlockError(user);
                return(false);
            }

            //Don't allow someone to block if someone else is on the same tile or if they're inside of a doorway
            var playerTileRef = xform.Coordinates.GetTileRef();
            if (playerTileRef != null)
            {
                var intersecting = _lookup.GetEntitiesIntersecting(playerTileRef.Value);
                var mobQuery     = GetEntityQuery <MobStateComponent>();
                var doorQuery    = GetEntityQuery <DoorComponent>();
                var xformQuery   = GetEntityQuery <TransformComponent>();

                foreach (var uid in intersecting)
                {
                    if (uid != user && mobQuery.HasComponent(uid) || xformQuery.GetComponent(uid).Anchored&& doorQuery.HasComponent(uid))
                    {
                        TooCloseError(user);
                        return(false);
                    }
                }
            }

            //Don't allow someone to block if they're somehow not anchored.
            _transformSystem.AnchorEntity(xform);
            if (!xform.Anchored)
            {
                CantBlockError(user);
                return(false);
            }
            _actionsSystem.SetToggled(component.BlockingToggleAction, true);
            _popupSystem.PopupEntity(msgUser, user, Filter.Entities(user));
            _popupSystem.PopupEntity(msgOther, user, Filter.Pvs(user).RemoveWhereAttachedEntity(e => e == user));
        }

        if (TryComp <PhysicsComponent>(user, out var physicsComponent))
        {
            var fixture = new Fixture(physicsComponent, component.Shape)
            {
                ID             = BlockingComponent.BlockFixtureID,
                Hard           = true,
                CollisionLayer = (int)CollisionGroup.WallLayer
            };

            _fixtureSystem.TryCreateFixture(physicsComponent, fixture);
        }

        component.IsBlocking = true;

        return(true);
    }