Mapping Actions (#6877)

This commit is contained in:
Leon Friedrich
2022-03-02 12:12:34 +13:00
committed by GitHub
parent c3e118bf19
commit 36fcca8337
7 changed files with 1335 additions and 7 deletions

View File

@@ -1,8 +1,14 @@
using Content.Shared.Decals;
using Content.Client.Actions;
using Content.Shared.Actions;
using Content.Shared.Actions.ActionTypes;
using Content.Shared.Decals;
using Robust.Client.GameObjects;
using Robust.Client.Input;
using Robust.Shared.Input;
using Robust.Shared.Input.Binding;
using Robust.Shared.Map;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Client.Decals;
@@ -12,6 +18,8 @@ public sealed class DecalPlacementSystem : EntitySystem
{
[Dependency] private readonly IInputManager _inputManager = default!;
[Dependency] private readonly InputSystem _inputSystem = default!;
[Dependency] private readonly IPrototypeManager _protoMan = default!;
[Dependency] private readonly IMapManager _mapMan = default!;
private string? _decalId;
private Color _decalColor = Color.White;
@@ -82,6 +90,70 @@ public sealed class DecalPlacementSystem : EntitySystem
return true;
}, true)).Register<DecalPlacementSystem>();
SubscribeLocalEvent<FillActionSlotEvent>(OnFillSlot);
SubscribeLocalEvent<PlaceDecalActionEvent>(OnPlaceDecalAction);
}
private void OnPlaceDecalAction(PlaceDecalActionEvent args)
{
if (args.Handled)
return;
if (!_mapMan.TryFindGridAt(args.Target, out var grid))
return;
args.Handled = true;
var coords = EntityCoordinates.FromMap(grid.GridEntityId, args.Target, EntityManager);
if (args.Snap)
{
var newPos = new Vector2(
(float) (MathF.Round(coords.X - 0.5f, MidpointRounding.AwayFromZero) + 0.5),
(float) (MathF.Round(coords.Y - 0.5f, MidpointRounding.AwayFromZero) + 0.5)
);
coords = coords.WithPosition(newPos);
}
coords = coords.Offset(new Vector2(-0.5f, -0.5f));
var decal = new Decal(coords.Position, args.DecalId, args.Color, Angle.FromDegrees(args.Rotation), args.ZIndex, args.Cleanable);
RaiseNetworkEvent(new RequestDecalPlacementEvent(decal, coords));
}
private void OnFillSlot(FillActionSlotEvent ev)
{
if (!_active || _placing)
return;
if (ev.Action != null)
return;
if (_decalId == null || !_protoMan.TryIndex<DecalPrototype>(_decalId, out var decalProto))
return;
var actionEvent = new PlaceDecalActionEvent()
{
DecalId = _decalId,
Color = _decalColor,
Rotation = _decalAngle.Degrees,
Snap = _snap,
ZIndex = _zIndex,
Cleanable = _cleanable,
};
ev.Action = new WorldTargetAction()
{
Name = $"{_decalId} ({_decalColor.ToHex()}, {(int) _decalAngle.Degrees})", // non-unique actions may be considered duplicates when saving/loading.
Icon = decalProto.Sprite,
Repeat = true,
CheckCanAccess = false,
CheckCanInteract = false,
Range = -1,
Event = actionEvent,
IconColor = _decalColor,
};
}
public override void Shutdown()
@@ -110,3 +182,24 @@ public sealed class DecalPlacementSystem : EntitySystem
_inputSystem.SetEntityContextActive();
}
}
public sealed class PlaceDecalActionEvent : PerformWorldTargetActionEvent
{
[DataField("decalId", customTypeSerializer:typeof(PrototypeIdSerializer<DecalPrototype>))]
public string DecalId = string.Empty;
[DataField("color")]
public Color Color;
[DataField("rotation")]
public double Rotation;
[DataField("snap")]
public bool Snap;
[DataField("zIndex")]
public int ZIndex;
[DataField("cleanable")]
public bool Cleanable;
}