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

@@ -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
/// <summary>
/// Component that represents a handheld lightsource which can be toggled on and off.
/// </summary>
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<PointLightComponent>();
spriteComponent = Owner.GetComponent<SpriteComponent>();
_pointLight = Owner.GetComponent<PointLightComponent>();
_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)
@@ -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();
}
}
}
}