conveyor & recycler ecs (#4537)
* conveyor done, recycler still todo * done, just need to suss out the physics part. pinged sloth about it * ship it
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.Items;
|
||||
using Content.Server.MachineLinking.Components;
|
||||
using Content.Server.Power.Components;
|
||||
using Content.Shared.Conveyor;
|
||||
using Content.Shared.MachineLinking;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Analyzers;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Maths;
|
||||
@@ -14,151 +16,29 @@ using Robust.Shared.ViewVariables;
|
||||
namespace Content.Server.Conveyor
|
||||
{
|
||||
[RegisterComponent]
|
||||
[Friend(typeof(ConveyorSystem))]
|
||||
public class ConveyorComponent : Component
|
||||
{
|
||||
public override string Name => "Conveyor";
|
||||
|
||||
[ViewVariables] private bool Powered => !Owner.TryGetComponent(out ApcPowerReceiverComponent? receiver) || receiver.Powered;
|
||||
|
||||
/// <summary>
|
||||
/// The angle to move entities by in relation to the owner's rotation.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("angle")]
|
||||
private Angle _angle = Angle.Zero;
|
||||
|
||||
public float Speed => _speed;
|
||||
public Angle Angle = Angle.Zero;
|
||||
|
||||
/// <summary>
|
||||
/// The amount of units to move the entity by per second.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("speed")]
|
||||
private float _speed = 2f;
|
||||
public float Speed = 2f;
|
||||
|
||||
private ConveyorState _state;
|
||||
/// <summary>
|
||||
/// The current state of this conveyor
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
private ConveyorState State
|
||||
{
|
||||
get => _state;
|
||||
set
|
||||
{
|
||||
_state = value;
|
||||
UpdateAppearance();
|
||||
}
|
||||
}
|
||||
|
||||
public override void HandleMessage(ComponentMessage message, IComponent? component)
|
||||
{
|
||||
base.HandleMessage(message, component);
|
||||
switch (message)
|
||||
{
|
||||
case PowerChangedMessage powerChanged:
|
||||
OnPowerChanged(powerChanged);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnPowerChanged(PowerChangedMessage e)
|
||||
{
|
||||
UpdateAppearance();
|
||||
}
|
||||
|
||||
private void UpdateAppearance()
|
||||
{
|
||||
if (Owner.TryGetComponent<AppearanceComponent>(out var appearance))
|
||||
{
|
||||
if (Powered)
|
||||
{
|
||||
appearance.SetData(ConveyorVisuals.State, _state);
|
||||
}
|
||||
else
|
||||
{
|
||||
appearance.SetData(ConveyorVisuals.State, ConveyorState.Off);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the angle in which entities on top of this conveyor
|
||||
/// belt are pushed in
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The angle when taking into account if the conveyor is reversed
|
||||
/// </returns>
|
||||
public Angle GetAngle()
|
||||
{
|
||||
var adjustment = _state == ConveyorState.Reversed ? MathHelper.Pi : 0;
|
||||
var radians = MathHelper.DegreesToRadians(_angle);
|
||||
|
||||
return new Angle(Owner.Transform.LocalRotation.Theta + radians + adjustment);
|
||||
}
|
||||
|
||||
public bool CanRun()
|
||||
{
|
||||
if (State == ConveyorState.Off)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Owner.TryGetComponent(out ApcPowerReceiverComponent? receiver) &&
|
||||
!receiver.Powered)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Owner.HasComponent<ItemComponent>())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool CanMove(IEntity entity)
|
||||
{
|
||||
// TODO We should only check status InAir or Static or MapGrid or /mayber/ container
|
||||
if (entity == Owner)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!entity.TryGetComponent(out IPhysBody? physics) ||
|
||||
physics.BodyType == BodyType.Static)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (entity.HasComponent<ConveyorComponent>())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (entity.HasComponent<IMapGridComponent>())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (entity.IsInContainer())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void SetState(TwoWayLeverSignal signal)
|
||||
{
|
||||
State = signal switch
|
||||
{
|
||||
TwoWayLeverSignal.Left => ConveyorState.Reversed,
|
||||
TwoWayLeverSignal.Middle => ConveyorState.Off,
|
||||
TwoWayLeverSignal.Right => ConveyorState.Forward,
|
||||
_ => ConveyorState.Off
|
||||
};
|
||||
}
|
||||
public ConveyorState State;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,29 @@
|
||||
using Content.Server.MachineLinking.Events;
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.Items;
|
||||
using Content.Server.MachineLinking.Events;
|
||||
using Content.Server.MachineLinking.Models;
|
||||
using Content.Server.Power.Components;
|
||||
using Content.Server.Recycling.Components;
|
||||
using Content.Server.Stunnable.Components;
|
||||
using Content.Shared.Conveyor;
|
||||
using Content.Shared.MachineLinking;
|
||||
using Content.Shared.Movement.Components;
|
||||
using Content.Shared.Notification.Managers;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Physics;
|
||||
using Robust.Shared.Physics.Dynamics;
|
||||
|
||||
namespace Content.Server.Conveyor
|
||||
{
|
||||
public class ConveyorSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private IEntityLookup _entityLookup = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
@@ -17,6 +31,27 @@ namespace Content.Server.Conveyor
|
||||
SubscribeLocalEvent<ConveyorComponent, SignalReceivedEvent>(OnSignalReceived);
|
||||
SubscribeLocalEvent<ConveyorComponent, PortDisconnectedEvent>(OnPortDisconnected);
|
||||
SubscribeLocalEvent<ConveyorComponent, LinkAttemptEvent>(OnLinkAttempt);
|
||||
SubscribeLocalEvent<ConveyorComponent, PowerChangedEvent>(OnPowerChanged);
|
||||
}
|
||||
|
||||
private void OnPowerChanged(EntityUid uid, ConveyorComponent component, PowerChangedEvent args)
|
||||
{
|
||||
UpdateAppearance(component);
|
||||
}
|
||||
|
||||
private void UpdateAppearance(ConveyorComponent component)
|
||||
{
|
||||
if (component.Owner.TryGetComponent<AppearanceComponent>(out var appearance))
|
||||
{
|
||||
if (component.Owner.TryGetComponent<ApcPowerReceiverComponent>(out var receiver) && receiver.Powered)
|
||||
{
|
||||
appearance.SetData(ConveyorVisuals.State, component.State);
|
||||
}
|
||||
else
|
||||
{
|
||||
appearance.SetData(ConveyorVisuals.State, ConveyorState.Off);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnLinkAttempt(EntityUid uid, ConveyorComponent component, LinkAttemptEvent args)
|
||||
@@ -35,7 +70,7 @@ namespace Content.Server.Conveyor
|
||||
|
||||
private void OnPortDisconnected(EntityUid uid, ConveyorComponent component, PortDisconnectedEvent args)
|
||||
{
|
||||
component.SetState(TwoWayLeverSignal.Middle);
|
||||
SetState(component, TwoWayLeverSignal.Middle);
|
||||
}
|
||||
|
||||
private void OnSignalReceived(EntityUid uid, ConveyorComponent component, SignalReceivedEvent args)
|
||||
@@ -43,9 +78,92 @@ namespace Content.Server.Conveyor
|
||||
switch (args.Port)
|
||||
{
|
||||
case "state":
|
||||
component.SetState((TwoWayLeverSignal) args.Value!);
|
||||
SetState(component, (TwoWayLeverSignal) args.Value!);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void SetState(ConveyorComponent component, TwoWayLeverSignal signal)
|
||||
{
|
||||
component.State = signal switch
|
||||
{
|
||||
TwoWayLeverSignal.Left => ConveyorState.Reversed,
|
||||
TwoWayLeverSignal.Middle => ConveyorState.Off,
|
||||
TwoWayLeverSignal.Right => ConveyorState.Forward,
|
||||
_ => ConveyorState.Off
|
||||
};
|
||||
UpdateAppearance(component);
|
||||
}
|
||||
|
||||
public bool CanRun(ConveyorComponent component)
|
||||
{
|
||||
if (component.State == ConveyorState.Off)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (component.Owner.TryGetComponent(out ApcPowerReceiverComponent? receiver) &&
|
||||
!receiver.Powered)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (component.Owner.HasComponent<ItemComponent>())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the angle in which entities on top of this conveyor
|
||||
/// belt are pushed in
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The angle when taking into account if the conveyor is reversed
|
||||
/// </returns>
|
||||
public Angle GetAngle(ConveyorComponent component)
|
||||
{
|
||||
var adjustment = component.State == ConveyorState.Reversed ? MathHelper.Pi/2 : -MathHelper.Pi/2;
|
||||
var radians = MathHelper.DegreesToRadians(component.Angle);
|
||||
|
||||
return new Angle(component.Owner.Transform.LocalRotation.Theta + radians + adjustment);
|
||||
}
|
||||
|
||||
public IEnumerable<(IEntity, IPhysBody)> GetEntitiesToMove(ConveyorComponent comp)
|
||||
{
|
||||
//todo uuuhhh cache this
|
||||
foreach (var entity in _entityLookup.GetEntitiesIntersecting(comp.Owner, LookupFlags.Approximate))
|
||||
{
|
||||
if (entity.Deleted)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (entity == comp.Owner)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!entity.TryGetComponent(out IPhysBody? physics) ||
|
||||
physics.BodyType == BodyType.Static || physics.BodyStatus == BodyStatus.InAir || entity.IsWeightless())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (entity.HasComponent<IMapGridComponent>())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (entity.IsInContainer())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
yield return (entity, physics);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user