Flashlights now run off a power cell. (#121)

* Flashlight runs off a power cell now.

* Cleanup
This commit is contained in:
Pieter-Jan Briers
2018-11-11 20:05:09 +01:00
committed by GitHub
parent 51bc17f76d
commit 4720182cf4
3 changed files with 112 additions and 16 deletions

View File

@@ -99,6 +99,7 @@
<Compile Include="GameObjects\EntitySystems\Click\ExamineSystem.cs" /> <Compile Include="GameObjects\EntitySystems\Click\ExamineSystem.cs" />
<Compile Include="GameObjects\EntitySystems\Click\InteractionSystem.cs" /> <Compile Include="GameObjects\EntitySystems\Click\InteractionSystem.cs" />
<Compile Include="GameObjects\EntitySystems\DoorSystem.cs" /> <Compile Include="GameObjects\EntitySystems\DoorSystem.cs" />
<Compile Include="GameObjects\EntitySystems\HandHeldLightSystem.cs" />
<Compile Include="GameObjects\EntitySystems\HandsSystem.cs" /> <Compile Include="GameObjects\EntitySystems\HandsSystem.cs" />
<Compile Include="GameObjects\EntitySystems\PowerApcSystem.cs" /> <Compile Include="GameObjects\EntitySystems\PowerApcSystem.cs" />
<Compile Include="GameObjects\EntitySystems\PowerSmesSystem.cs" /> <Compile Include="GameObjects\EntitySystems\PowerSmesSystem.cs" />

View File

@@ -1,14 +1,10 @@
using Content.Server.GameObjects.EntitySystems; using Content.Server.GameObjects.EntitySystems;
using Content.Server.GameObjects.Components.Power;
using SS14.Server.GameObjects; using SS14.Server.GameObjects;
using SS14.Server.GameObjects.Components.Container;
using SS14.Shared.Enums; using SS14.Shared.Enums;
using SS14.Shared.GameObjects;
using SS14.Shared.Interfaces.GameObjects; using SS14.Shared.Interfaces.GameObjects;
using SS14.Shared.Serialization; using SS14.Shared.GameObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SS14.Shared.ViewVariables; using SS14.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Interactable namespace Content.Server.GameObjects.Components.Interactable
@@ -16,10 +12,25 @@ namespace Content.Server.GameObjects.Components.Interactable
/// <summary> /// <summary>
/// Component that represents a handheld lightsource which can be toggled on and off. /// Component that represents a handheld lightsource which can be toggled on and off.
/// </summary> /// </summary>
class HandheldLightComponent : Component, EntitySystems.IUse, EntitySystems.IExamine class HandheldLightComponent : Component, IUse, IExamine
{ {
PointLightComponent pointLight; private PointLightComponent _pointLight;
SpriteComponent spriteComponent; 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"; public override string Name => "HandheldLight";
@@ -29,12 +40,22 @@ namespace Content.Server.GameObjects.Components.Interactable
[ViewVariables] [ViewVariables]
public bool Activated { get; private set; } = false; public bool Activated { get; private set; } = false;
public const float Wattage = 10;
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
pointLight = Owner.GetComponent<PointLightComponent>(); _pointLight = Owner.GetComponent<PointLightComponent>();
spriteComponent = Owner.GetComponent<SpriteComponent>(); _spriteComponent = Owner.GetComponent<SpriteComponent>();
_cellContainer =
ContainerManagerComponent.Ensure<ContainerSlot>("flashlight_cell_container", Owner, out var existed);
if (!existed)
{
var cell = Owner.EntityManager.SpawnEntity("PowerCellSmallHyper");
_cellContainer.Insert(cell);
}
} }
bool IUse.UseEntity(IEntity user) bool IUse.UseEntity(IEntity user)
@@ -54,19 +75,56 @@ namespace Content.Server.GameObjects.Components.Interactable
// Update sprite and light states to match the activation. // Update sprite and light states to match the activation.
if (Activated) if (Activated)
{ {
spriteComponent.LayerSetState(0, "lantern_on"); _spriteComponent.LayerSetState(0, "lantern_on");
pointLight.State = LightState.On; _pointLight.State = LightState.On;
} }
else else
{ {
spriteComponent.LayerSetState(0, "lantern_off"); _spriteComponent.LayerSetState(0, "lantern_off");
pointLight.State = LightState.Off; _pointLight.State = LightState.Off;
} }
// Toggle always succeeds. // Toggle always succeeds.
return true; 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() string IExamine.Examine()
{ {
if (Activated) if (Activated)
@@ -76,5 +134,19 @@ namespace Content.Server.GameObjects.Components.Interactable
return null; return null;
} }
public void OnUpdate(float frameTime)
{
if (!Activated)
{
return;
}
var cell = Cell;
if (cell == null || !cell.TryDeductWattage(Wattage, frameTime))
{
TurnOff();
}
}
} }
} }

View File

@@ -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<HandheldLightComponent>();
comp.OnUpdate(frameTime);
}
}
}
}