Power works with Anchorable (#1240)

Co-authored-by: py01 <pyronetics01@gmail.com>
This commit is contained in:
py01
2020-07-02 04:02:29 -06:00
committed by GitHub
parent 7291c318e4
commit 7b17698336
9 changed files with 118 additions and 8 deletions

View File

@@ -123,7 +123,8 @@ namespace Content.Server.GameObjects.Components.NodeContainer.NodeGroups
public void RemoveConsumer(PowerConsumerComponent consumer)
{
Debug.Assert(_consumersByPriority[consumer.Priority].Contains(consumer));
_consumersByPriority[consumer.Priority].Add(consumer);
consumer.ReceivedPower = 0;
_consumersByPriority[consumer.Priority].Remove(consumer);
_drawByPriority[consumer.Priority] -= consumer.DrawRate;
UpdateConsumerReceivedPower();
}

View File

@@ -1,4 +1,5 @@
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
using Robust.Server.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.ViewVariables;
@@ -28,8 +29,19 @@ namespace Content.Server.GameObjects.Components.NodeContainer.Nodes
[ViewVariables]
public IEntity Owner { get; private set; }
[ViewVariables]
private bool _needsGroup = true;
/// <summary>
/// If this node should be considered for connection by other nodes.
/// </summary>
private bool Connectable => !_deleting && Anchored;
private bool Anchored => !Owner.TryGetComponent<PhysicsComponent>(out var physics) || physics.Anchored;
/// <summary>
/// Prevents a node from being used by other nodes while midway through removal.
/// </summary>
private bool _deleting = false;
@@ -47,11 +59,21 @@ namespace Content.Server.GameObjects.Components.NodeContainer.Nodes
{
TryAssignGroupIfNeeded();
CombineGroupWithReachable();
if (Owner.TryGetComponent<PhysicsComponent>(out var physics))
{
AnchorUpdate();
physics.AnchoredChanged += AnchorUpdate;
}
}
public void OnContainerRemove()
{
_deleting = true;
if (Owner.TryGetComponent<PhysicsComponent>(out var physics))
{
AnchorUpdate();
physics.AnchoredChanged -= AnchorUpdate;
}
NodeGroup.RemoveNode(this);
}
@@ -75,7 +97,7 @@ namespace Content.Server.GameObjects.Components.NodeContainer.Nodes
public void SpreadGroup()
{
Debug.Assert(!_needsGroup);
foreach (var node in GetReachableCompatibleNodes().Where(node => node._needsGroup == true))
foreach (var node in GetReachableCompatibleNodes().Where(node => node._needsGroup))
{
node.NodeGroup = NodeGroup;
node.SpreadGroup();
@@ -97,12 +119,12 @@ namespace Content.Server.GameObjects.Components.NodeContainer.Nodes
private IEnumerable<Node> GetReachableCompatibleNodes()
{
return GetReachableNodes().Where(node => node.NodeGroupID == NodeGroupID)
.Where(node => node._deleting == false);
.Where(node => node.Connectable);
}
private IEnumerable<INodeGroup> GetReachableCompatibleGroups()
{
return GetReachableCompatibleNodes().Where(node => node._needsGroup == false)
return GetReachableCompatibleNodes().Where(node => !node._needsGroup)
.Select(node => node.NodeGroup)
.Where(group => group != NodeGroup);
}
@@ -127,5 +149,22 @@ namespace Content.Server.GameObjects.Components.NodeContainer.Nodes
{
return _nodeGroupFactory.MakeNodeGroup(NodeGroupID);
}
private void AnchorUpdate()
{
if (Anchored)
{
if (_needsGroup)
{
TryAssignGroupIfNeeded();
CombineGroupWithReachable();
}
}
else
{
NodeGroup.RemoveNode(this);
ClearNodeGroup();
}
}
}
}

View File

@@ -96,6 +96,7 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
.GetEntitiesInRange(Owner, PowerTransferRange);
return nearbyEntities.Select(entity => entity.TryGetComponent<PowerReceiverComponent>(out var receiver) ? receiver : null)
.Where(receiver => receiver != null)
.Where(receiver => receiver.Connectable)
.Where(receiver => receiver.NeedsProvider)
.Where(receiver => receiver.Owner.Transform.GridPosition.Distance(mapManager, Owner.Transform.GridPosition) < Math.Min(PowerTransferRange, receiver.PowerReceptionRange))
.ToList();

View File

@@ -39,6 +39,13 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
public IPowerProvider Provider { get => _provider; set => SetProvider(value); }
private IPowerProvider _provider = PowerProviderComponent.NullProvider;
/// <summary>
/// If this should be considered for connection by <see cref="PowerProviderComponent"/>s.
/// </summary>
public bool Connectable => Anchored;
private bool Anchored => !Owner.TryGetComponent<PhysicsComponent>(out var physics) || physics.Anchored;
[ViewVariables]
public bool NeedsProvider { get; private set; } = true;
@@ -78,10 +85,19 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
{
TryFindAndSetProvider();
}
if (Owner.TryGetComponent<PhysicsComponent>(out var physics))
{
AnchorUpdate();
physics.AnchoredChanged += AnchorUpdate;
}
}
public override void OnRemove()
{
if (Owner.TryGetComponent<PhysicsComponent>(out var physics))
{
physics.AnchoredChanged -= AnchorUpdate;
}
_provider.RemoveReceiver(this);
base.OnRemove();
}
@@ -184,6 +200,21 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
appearance.SetData(PowerDeviceVisuals.Powered, Powered);
}
}
private void AnchorUpdate()
{
if (Anchored)
{
if (NeedsProvider)
{
TryFindAndSetProvider();
}
}
else
{
ClearProvider();
}
}
}
public class PowerStateEventArgs : EventArgs

View File

@@ -1,4 +1,4 @@
- type: entity
- type: entity
id: StorageTank
name: storage tank
description: "A liquids storage tank"

View File

@@ -1,4 +1,4 @@
- type: entity
- type: entity
id: CrateGeneric
name: crate
description: A large container for items.

View File

@@ -1,4 +1,4 @@
- type: entity
- type: entity
id: Mirror
name: mirror
components:

View File

@@ -103,6 +103,8 @@
nodeTypes: { HVPower : ["AdjacentNode"] }
- type: PowerSupplier
supplyRate: 3000
- type: Physics
Anchored: true
- type: Anchorable
- type: entity
@@ -114,6 +116,10 @@
- type: Clickable
- type: InteractionOutline
- type: Collidable
shapes:
- !type:PhysShapeAabb
bounds: "-0.5, -0.5, 0.5, 0.5"
layer: [MobMask, Opaque]
- type: SnapGrid
offset: Center
- type: Sprite
@@ -127,6 +133,8 @@
- type: Damageable
- type: Breakable
thresholdvalue: 100
- type: Physics
Anchored: true
- type: Anchorable
- type: entity
@@ -138,6 +146,10 @@
- type: Clickable
- type: InteractionOutline
- type: Collidable
shapes:
- !type:PhysShapeAabb
bounds: "-0.5, -0.5, 0.5, 0.5"
layer: [MobMask, Opaque]
- type: SnapGrid
offset: Center
- type: Sprite
@@ -149,6 +161,9 @@
nodeTypes: { HVPower : ["AdjacentNode"] }
- type: PowerConsumer
- type: BatteryStorage
- type: Physics
Anchored: true
- type: Anchorable
- type: entity
id: DebugBatteryDischarger
@@ -159,6 +174,10 @@
- type: Clickable
- type: InteractionOutline
- type: Collidable
shapes:
- !type:PhysShapeAabb
bounds: "-0.5, -0.5, 0.5, 0.5"
layer: [MobMask, Opaque]
- type: SnapGrid
offset: Center
- type: Sprite
@@ -170,6 +189,9 @@
nodeTypes: { HVPower : ["AdjacentNode"] }
- type: PowerSupplier
- type: BatteryDischarger
- type: Physics
Anchored: true
- type: Anchorable
- type: entity
id: DebugSmes
@@ -211,6 +233,8 @@
- type: PowerSupplier
- type: BatteryDischarger
activeSupplyRate: 1000
- type: Physics
Anchored: true
- type: Anchorable
- type: entity
@@ -222,6 +246,10 @@
- type: Clickable
- type: InteractionOutline
- type: Collidable
shapes:
- !type:PhysShapeAabb
bounds: "-0.5, -0.5, 0.5, 0.5"
layer: [MobMask, Opaque]
- type: SnapGrid
offset: Center
- type: Sprite
@@ -241,6 +269,9 @@
voltage: Medium
- type: BatteryDischarger
activeSupplyRate: 1000
- type: Physics
Anchored: true
- type: Anchorable
- type: entity
id: DebugApc
@@ -294,6 +325,10 @@
- type: Clickable
- type: InteractionOutline
- type: Collidable
shapes:
- !type:PhysShapeAabb
bounds: "-0.5, -0.5, 0.5, 0.5"
layer: [MobMask, Opaque]
- type: SnapGrid
offset: Center
- type: Sprite
@@ -301,6 +336,9 @@
- type: Icon
texture: Objects/Furniture/wirelessmachine.png
- type: PowerReceiver
- type: Physics
Anchored: true
- type: Anchorable
- type: entity
id: SolarPanel

View File

@@ -1,4 +1,4 @@
- type: entity
- type: entity
parent: BaseItem
name: mop
id: MopItem