private void OnAfterInteract(EntityUid uid, SprayComponent component, AfterInteractEvent args)
    {
        if (args.Handled)
        {
            return;
        }

        args.Handled = true;

        if (!_solutionContainerSystem.TryGetSolution(uid, SprayComponent.SolutionName, out var solution))
        {
            return;
        }

        var ev = new SprayAttemptEvent(args.User);

        RaiseLocalEvent(uid, ev, false);
        if (ev.Cancelled)
        {
            return;
        }

        var curTime = _gameTiming.CurTime;

        if (TryComp <ItemCooldownComponent>(uid, out var cooldown) &&
            curTime < cooldown.CooldownEnd)
        {
            return;
        }

        if (solution.CurrentVolume <= 0)
        {
            _popupSystem.PopupEntity(Loc.GetString("spray-component-is-empty-message"), uid,
                                     Filter.Entities(args.User));
            return;
        }

        var playerPos = Transform(args.User).Coordinates;

        if (args.ClickLocation.GetGridUid(EntityManager) != playerPos.GetGridUid(EntityManager))
        {
            return;
        }

        var direction     = (args.ClickLocation.Position - playerPos.Position).Normalized;
        var threeQuarters = direction * 0.75f;
        var quarter       = direction * 0.25f;

        var amount = Math.Max(Math.Min((solution.CurrentVolume / component.TransferAmount).Int(), component.VaporAmount), 1);

        var spread = component.VaporSpread / amount;

        for (var i = 0; i < amount; i++)
        {
            var rotation = new Angle(direction.ToAngle() + Angle.FromDegrees(spread * i) -
                                     Angle.FromDegrees(spread * (amount - 1) / 2));

            var(_, diffPos) = args.ClickLocation - playerPos;
            var diffNorm   = diffPos.Normalized;
            var diffLength = diffPos.Length;

            var target = Transform(args.User).Coordinates
                         .Offset((diffNorm + rotation.ToVec()).Normalized * diffLength + quarter);

            if (target.TryDistance(EntityManager, playerPos, out var distance) && distance > component.SprayDistance)
            {
                target = Transform(args.User).Coordinates
                         .Offset(diffNorm * component.SprayDistance);
            }

            var newSolution = _solutionContainerSystem.SplitSolution(uid, solution, component.TransferAmount);

            if (newSolution.TotalVolume <= FixedPoint2.Zero)
            {
                break;
            }

            var vapor = Spawn(component.SprayedPrototype,
                              playerPos.Offset(distance < 1 ? quarter : threeQuarters));
            Transform(vapor).LocalRotation = rotation;

            if (TryComp(vapor, out AppearanceComponent? appearance))
            {
                appearance.SetData(VaporVisuals.Color, solution.Color.WithAlpha(1f));
                appearance.SetData(VaporVisuals.State, true);
            }

            // Add the solution to the vapor and actually send the thing
            var vaporComponent = Comp <VaporComponent>(vapor);
            _vaporSystem.TryAddSolution(vaporComponent, newSolution);

            // impulse direction is defined in world-coordinates, not local coordinates
            var impulseDirection = Transform(vapor).WorldRotation.ToVec();
            _vaporSystem.Start(vaporComponent, impulseDirection, component.SprayVelocity, target, component.SprayAliveTime);

            if (component.Impulse > 0f && TryComp(args.User, out PhysicsComponent? body))
            {
                body.ApplyLinearImpulse(-impulseDirection * component.Impulse);
            }
        }

        SoundSystem.Play(component.SpraySound.GetSound(), Filter.Pvs(uid), uid, AudioHelpers.WithVariation(0.125f));

        RaiseLocalEvent(uid,
                        new RefreshItemCooldownEvent(curTime, curTime + TimeSpan.FromSeconds(component.CooldownTime)), true);
    }
Exemple #2
0
    private void OnAfterInteract(EntityUid uid, SprayComponent component, AfterInteractEvent args)
    {
        if (args.Handled)
        {
            return;
        }

        args.Handled = true;

        if (!_solutionContainerSystem.TryGetSolution(uid, SprayComponent.SolutionName, out var solution))
        {
            return;
        }

        var ev = new SprayAttemptEvent(args.User);

        RaiseLocalEvent(uid, ev, false);
        if (ev.Cancelled)
        {
            return;
        }

        var curTime = _gameTiming.CurTime;

        if (TryComp <ItemCooldownComponent>(uid, out var cooldown) &&
            curTime < cooldown.CooldownEnd)
        {
            return;
        }

        if (solution.CurrentVolume <= 0)
        {
            _popupSystem.PopupEntity(Loc.GetString("spray-component-is-empty-message"), uid,
                                     Filter.Entities(args.User));
            return;
        }

        var xformQuery = GetEntityQuery <TransformComponent>();
        var userXform  = xformQuery.GetComponent(args.User);

        var userMapPos  = userXform.MapPosition;
        var clickMapPos = args.ClickLocation.ToMap(EntityManager);

        var diffPos = clickMapPos.Position - userMapPos.Position;

        if (diffPos == Vector2.Zero || diffPos == Vector2.NaN)
        {
            return;
        }

        var diffLength = diffPos.Length;
        var diffNorm   = diffPos.Normalized;
        var diffAngle  = diffNorm.ToAngle();

        // Vectors to determine the spawn offset of the vapor clouds.
        var threeQuarters = diffNorm * 0.75f;
        var quarter       = diffNorm * 0.25f;

        var amount = Math.Max(Math.Min((solution.CurrentVolume / component.TransferAmount).Int(), component.VaporAmount), 1);
        var spread = component.VaporSpread / amount;

        for (var i = 0; i < amount; i++)
        {
            var rotation = new Angle(diffAngle + Angle.FromDegrees(spread * i) -
                                     Angle.FromDegrees(spread * (amount - 1) / 2));

            // Calculate the destination for the vapor cloud. Limit to the maximum spray distance.
            var target = userMapPos
                         .Offset((diffNorm + rotation.ToVec()).Normalized * diffLength + quarter);

            var distance = target.Position.Length;
            if (distance > component.SprayDistance)
            {
                target = userMapPos.Offset(diffNorm * component.SprayDistance);
            }

            var newSolution = _solutionContainerSystem.SplitSolution(uid, solution, component.TransferAmount);

            if (newSolution.TotalVolume <= FixedPoint2.Zero)
            {
                break;
            }

            // Spawn the vapor cloud onto the grid/map the user is present on. Offset the start position based on how far the target destination is.
            var vaporPos   = userMapPos.Offset(distance < 1 ? quarter : threeQuarters);
            var vapor      = Spawn(component.SprayedPrototype, vaporPos);
            var vaporXform = xformQuery.GetComponent(vapor);

            vaporXform.WorldRotation = rotation;

            if (TryComp(vapor, out AppearanceComponent? appearance))
            {
                appearance.SetData(VaporVisuals.Color, solution.Color.WithAlpha(1f));
                appearance.SetData(VaporVisuals.State, true);
            }

            // Add the solution to the vapor and actually send the thing
            var vaporComponent = Comp <VaporComponent>(vapor);
            _vaporSystem.TryAddSolution(vaporComponent, newSolution);

            // impulse direction is defined in world-coordinates, not local coordinates
            var impulseDirection = rotation.ToVec();
            _vaporSystem.Start(vaporComponent, vaporXform, impulseDirection, component.SprayVelocity, target, component.SprayAliveTime, args.User);
        }

        SoundSystem.Play(component.SpraySound.GetSound(), Filter.Pvs(uid), uid, AudioHelpers.WithVariation(0.125f));

        RaiseLocalEvent(uid,
                        new RefreshItemCooldownEvent(curTime, curTime + TimeSpan.FromSeconds(component.CooldownTime)), true);
    }