Files
tbd-station-14/Content.Shared/GameObjects/Components/Pulling/SharedPullableComponent.cs
chairbender 5f788c3318 Alerts System and UI (#2529)
* #272 add bordered panel for effects bar

* #272 avoid mouse overlapping tooltip when near edges,
change tooltip colors to match mockups

* #272 WIP defining status effect states as YML and
sending them as encoded integers

* #272 refactor to use new alert system

* #272 refactor to use new alert system

* #272 fix various bugs with new alert system and update
alerts to have color

* #272 WIP

* #272 rename status effects to alerts

* #272 WIP reworking alert internals to avoid code dup
and eliminate enum

* #272 refactor alerts to use
categories and fix various bugs

* #272 more alert bugfixes

* #272 alert ordering

* #272 callback-based approach for alert clicks

* #272 add debug commands for alerts

* #272 utilize new GridContainer capabilities for sizing of alerts tab

* #272 scale alerts height based on
window size

* #272 fix tooltip flicker

* #272 transparent alert panel

* #272 adjust styles to match injazz mockups more, add cooldown info in tooltip

* #272 adjust styles to match injazz mockups more, add cooldown info in tooltip

* #272 alert prototype tests

* #272 alert manager tests

* #272 alert order tests

* #272 simple unit test for alerts component

* #272 integration test for alerts

* #272 rework alerts to use enums instead
of id / category

* #272 various cleanups for PR

* #272 use byte for more compact alert messages

* #272 rename StatusEffects folder to Alerts,
add missing NetSerializable
2020-11-10 15:22:19 +11:00

272 lines
6.8 KiB
C#

#nullable enable
using System;
using Content.Shared.Alert;
using Content.Shared.GameObjects.Components.Mobs;
using Content.Shared.GameObjects.EntitySystems;
using Content.Shared.Physics;
using Content.Shared.Physics.Pull;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.ComponentDependencies;
using Robust.Shared.GameObjects.Components;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Physics;
using Robust.Shared.Serialization;
namespace Content.Shared.GameObjects.Components.Pulling
{
public abstract class SharedPullableComponent : Component, ICollideSpecial
{
public override string Name => "Pullable";
public override uint? NetID => ContentNetIDs.PULLABLE;
[ComponentDependency] private readonly IPhysicsComponent? _physics = default!;
private IEntity? _puller;
public virtual IEntity? Puller
{
get => _puller;
private set
{
if (_puller == value)
{
return;
}
_puller = value;
Dirty();
if (_physics == null)
{
return;
}
PullController controller;
if (value == null)
{
if (_physics.TryGetController(out controller))
{
controller.StopPull();
}
return;
}
controller = _physics.EnsureController<PullController>();
controller.StartPull(value);
}
}
public bool BeingPulled => Puller != null;
public bool CanStartPull(IEntity puller)
{
if (!puller.HasComponent<SharedPullerComponent>())
{
return false;
}
if (_physics == null)
{
return false;
}
if (_physics.Anchored)
{
return false;
}
if (puller == Owner)
{
return false;
}
if (!puller.IsInSameOrNoContainer(Owner))
{
return false;
}
return true;
}
public bool TryStartPull(IEntity puller)
{
if (!CanStartPull(puller))
{
return false;
}
TryStopPull();
Puller = puller;
if (Puller != puller)
{
return false;
}
return true;
}
public bool TryStopPull()
{
if (!BeingPulled)
{
return false;
}
Puller = null;
return true;
}
public bool TogglePull(IEntity puller)
{
if (BeingPulled)
{
if (Puller == puller)
{
return TryStopPull();
}
else
{
TryStopPull();
return TryStartPull(puller);
}
}
return TryStartPull(puller);
}
public bool TryMoveTo(EntityCoordinates to)
{
if (Puller == null)
{
return false;
}
if (_physics == null)
{
return false;
}
if (!_physics.TryGetController(out PullController controller))
{
return false;
}
return controller.TryMoveTo(Puller.Transform.Coordinates, to);
}
public override ComponentState GetComponentState()
{
return new PullableComponentState(Puller?.Uid);
}
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
base.HandleComponentState(curState, nextState);
if (!(curState is PullableComponentState state))
{
return;
}
if (state.Puller == null)
{
Puller = null;
return;
}
Puller = Owner.EntityManager.GetEntity(state.Puller.Value);
}
public override void HandleMessage(ComponentMessage message, IComponent? component)
{
base.HandleMessage(message, component);
if (!(message is PullMessage pullMessage) ||
pullMessage.Pulled.Owner != Owner)
{
return;
}
switch (message)
{
case PullStartedMessage msg:
AddPullingStatuses(msg.Puller.Owner);
break;
case PullStoppedMessage msg:
RemovePullingStatuses(msg.Puller.Owner);
break;
}
}
private void AddPullingStatuses(IEntity puller)
{
if (Owner.TryGetComponent(out SharedAlertsComponent? pulledStatus))
{
pulledStatus.ShowAlert(AlertType.Pulled);
}
if (puller.TryGetComponent(out SharedAlertsComponent? ownerStatus))
{
ownerStatus.ShowAlert(AlertType.Pulling, onClickAlert: OnClickAlert);
}
}
private void OnClickAlert(ClickAlertEventArgs args)
{
EntitySystem
.Get<SharedPullingSystem>()
.GetPulled(args.Player)?
.GetComponentOrNull<SharedPullableComponent>()?
.TryStopPull();
}
private void RemovePullingStatuses(IEntity puller)
{
if (Owner.TryGetComponent(out SharedAlertsComponent? pulledStatus))
{
pulledStatus.ClearAlert(AlertType.Pulled);
}
if (puller.TryGetComponent(out SharedAlertsComponent? ownerStatus))
{
ownerStatus.ClearAlert(AlertType.Pulling);
}
}
public override void OnRemove()
{
TryStopPull();
base.OnRemove();
}
public bool PreventCollide(IPhysBody collidedWith)
{
if (_puller == null || _physics == null)
{
return false;
}
return (_physics.CollisionLayer & collidedWith.CollisionMask) == (int) CollisionGroup.MobImpassable;
}
}
[Serializable, NetSerializable]
public class PullableComponentState : ComponentState
{
public readonly EntityUid? Puller;
public PullableComponentState(EntityUid? puller) : base(ContentNetIDs.PULLABLE)
{
Puller = puller;
}
}
}