Use IMapInit in PoweredLightComponent

This commit is contained in:
Pieter-Jan Briers
2020-07-30 23:59:40 +02:00
parent b3d298e064
commit bfe2217112

View File

@@ -8,6 +8,7 @@ using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Server.GameObjects; using Robust.Server.GameObjects;
using Robust.Server.GameObjects.Components.Container; using Robust.Server.GameObjects.Components.Container;
using Robust.Server.GameObjects.EntitySystems; using Robust.Server.GameObjects.EntitySystems;
using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.Audio; using Robust.Shared.Audio;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Systems; using Robust.Shared.GameObjects.Systems;
@@ -23,7 +24,7 @@ namespace Content.Server.GameObjects.Components.Power
/// Component that represents a wall light. It has a light bulb that can be replaced when broken. /// Component that represents a wall light. It has a light bulb that can be replaced when broken.
/// </summary> /// </summary>
[RegisterComponent] [RegisterComponent]
public class PoweredLightComponent : Component, IInteractHand, IInteractUsing public class PoweredLightComponent : Component, IInteractHand, IInteractUsing, IMapInit
{ {
public override string Name => "PoweredLight"; public override string Name => "PoweredLight";
@@ -219,20 +220,7 @@ namespace Content.Server.GameObjects.Components.Power
Owner.GetComponent<PowerReceiverComponent>().OnPowerStateChanged += UpdateLight; Owner.GetComponent<PowerReceiverComponent>().OnPowerStateChanged += UpdateLight;
_lightBulbContainer = ContainerManagerComponent.Ensure<ContainerSlot>("light_bulb", Owner, out var existed); _lightBulbContainer = ContainerManagerComponent.Ensure<ContainerSlot>("light_bulb", Owner);
if (!existed) // Insert a light tube if there wasn't any.
{
switch (BulbType)
{
case LightBulbType.Tube:
_lightBulbContainer.Insert(Owner.EntityManager.SpawnEntity("LightTube", Owner.Transform.GridPosition));
break;
case LightBulbType.Bulb:
_lightBulbContainer.Insert(Owner.EntityManager.SpawnEntity("LightBulb", Owner.Transform.GridPosition));
break;
}
}
} }
public override void OnRemove() public override void OnRemove()
@@ -240,5 +228,18 @@ namespace Content.Server.GameObjects.Components.Power
Owner.GetComponent<PowerReceiverComponent>().OnPowerStateChanged -= UpdateLight; Owner.GetComponent<PowerReceiverComponent>().OnPowerStateChanged -= UpdateLight;
base.OnRemove(); base.OnRemove();
} }
void IMapInit.MapInit()
{
var prototype = BulbType switch
{
LightBulbType.Bulb => "LightBulb",
LightBulbType.Tube => "LightTube",
_ => throw new ArgumentOutOfRangeException()
};
var entity = Owner.EntityManager.SpawnEntity(prototype, Owner.Transform.GridPosition);
_lightBulbContainer.Insert(entity);
}
} }
} }