diff --git a/Content.Server/Content.Server.csproj b/Content.Server/Content.Server.csproj index 9d88b66adb..ab8add0b8d 100644 --- a/Content.Server/Content.Server.csproj +++ b/Content.Server/Content.Server.csproj @@ -99,6 +99,7 @@ + diff --git a/Content.Server/GameObjects/Components/Interactable/HandheldLightComponent.cs b/Content.Server/GameObjects/Components/Interactable/HandheldLightComponent.cs index b5479c8190..21341cac22 100644 --- a/Content.Server/GameObjects/Components/Interactable/HandheldLightComponent.cs +++ b/Content.Server/GameObjects/Components/Interactable/HandheldLightComponent.cs @@ -1,14 +1,10 @@ using Content.Server.GameObjects.EntitySystems; +using Content.Server.GameObjects.Components.Power; using SS14.Server.GameObjects; +using SS14.Server.GameObjects.Components.Container; using SS14.Shared.Enums; -using SS14.Shared.GameObjects; using SS14.Shared.Interfaces.GameObjects; -using SS14.Shared.Serialization; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using SS14.Shared.GameObjects; using SS14.Shared.ViewVariables; namespace Content.Server.GameObjects.Components.Interactable @@ -16,10 +12,25 @@ namespace Content.Server.GameObjects.Components.Interactable /// /// Component that represents a handheld lightsource which can be toggled on and off. /// - class HandheldLightComponent : Component, EntitySystems.IUse, EntitySystems.IExamine + class HandheldLightComponent : Component, IUse, IExamine { - PointLightComponent pointLight; - SpriteComponent spriteComponent; + private PointLightComponent _pointLight; + private SpriteComponent _spriteComponent; + [ViewVariables] private ContainerSlot _cellContainer; + + private PowerCellComponent Cell + { + get + { + if (_cellContainer.ContainedEntity == null) + { + return null; + } + + _cellContainer.ContainedEntity.TryGetComponent(out PowerCellComponent cell); + return cell; + } + } public override string Name => "HandheldLight"; @@ -29,12 +40,22 @@ namespace Content.Server.GameObjects.Components.Interactable [ViewVariables] public bool Activated { get; private set; } = false; + public const float Wattage = 10; + public override void Initialize() { base.Initialize(); - pointLight = Owner.GetComponent(); - spriteComponent = Owner.GetComponent(); + _pointLight = Owner.GetComponent(); + _spriteComponent = Owner.GetComponent(); + _cellContainer = + ContainerManagerComponent.Ensure("flashlight_cell_container", Owner, out var existed); + + if (!existed) + { + var cell = Owner.EntityManager.SpawnEntity("PowerCellSmallHyper"); + _cellContainer.Insert(cell); + } } bool IUse.UseEntity(IEntity user) @@ -54,19 +75,56 @@ namespace Content.Server.GameObjects.Components.Interactable // Update sprite and light states to match the activation. if (Activated) { - spriteComponent.LayerSetState(0, "lantern_on"); - pointLight.State = LightState.On; + _spriteComponent.LayerSetState(0, "lantern_on"); + _pointLight.State = LightState.On; } else { - spriteComponent.LayerSetState(0, "lantern_off"); - pointLight.State = LightState.Off; + _spriteComponent.LayerSetState(0, "lantern_off"); + _pointLight.State = LightState.Off; } // Toggle always succeeds. return true; } + public void TurnOff() + { + if (!Activated) + { + return; + } + + _spriteComponent.LayerSetState(0, "lantern_off"); + _pointLight.State = LightState.Off; + Activated = false; + } + + public void TurnOn() + { + if (Activated) + { + return; + } + + var cell = Cell; + if (cell == null) + { + return; + } + + // To prevent having to worry about frame time in here. + // Let's just say you need a whole second of charge before you can turn it on. + // Simple enough. + if (cell.AvailableCharge(1) < Wattage) + { + return; + } + + _spriteComponent.LayerSetState(0, "lantern_on"); + _pointLight.State = LightState.On; + } + string IExamine.Examine() { if (Activated) @@ -76,5 +134,19 @@ namespace Content.Server.GameObjects.Components.Interactable return null; } + + public void OnUpdate(float frameTime) + { + if (!Activated) + { + return; + } + + var cell = Cell; + if (cell == null || !cell.TryDeductWattage(Wattage, frameTime)) + { + TurnOff(); + } + } } } diff --git a/Content.Server/GameObjects/EntitySystems/HandHeldLightSystem.cs b/Content.Server/GameObjects/EntitySystems/HandHeldLightSystem.cs new file mode 100644 index 0000000000..df96775a79 --- /dev/null +++ b/Content.Server/GameObjects/EntitySystems/HandHeldLightSystem.cs @@ -0,0 +1,23 @@ +using Content.Server.GameObjects.Components.Interactable; +using SS14.Shared.GameObjects; +using SS14.Shared.GameObjects.Systems; + +namespace Content.Server.GameObjects.EntitySystems +{ + public class HandHeldLightSystem : EntitySystem + { + public override void Initialize() + { + EntityQuery = new TypeEntityQuery(typeof(HandheldLightComponent)); + } + + public override void Update(float frameTime) + { + foreach (var entity in RelevantEntities) + { + var comp = entity.GetComponent(); + comp.OnUpdate(frameTime); + } + } + } +}