Files
tbd-station-14/Content.Server/GameObjects/Components/Power/LightBulbComponent.cs
Víctor Aguilera Puerto 4f2ae14b3f Light bulbs can now be colored! (#154)
-Also fixes a bug where bulb type didn't matter when inserting a new bulb into a light fixture.
-And a bug where bulb state changing didn't change the light itself.

P.S. Also note I didn't add any colored lightbulb prototypes, as I don't think they're necessary right now. To see the changes, please use VV! In the future, it might be possible to change the lightbulb's color with a multitool or something in-game.
2019-03-23 22:30:05 +01:00

99 lines
2.8 KiB
C#

using System;
using SS14.Shared.GameObjects;
using SS14.Shared.Maths;
using SS14.Shared.Serialization;
using SS14.Shared.ViewVariables;
using SpriteComponent = SS14.Server.GameObjects.SpriteComponent;
namespace Content.Server.GameObjects.Components.Power
{
public enum LightBulbState
{
Normal,
Broken,
Burned,
}
public enum LightBulbType
{
Tube,
}
/// <summary>
/// Component that represents a light bulb. Can be broken, or burned, which turns them mostly useless.
/// </summary>
public class LightBulbComponent : Component
{
/// <summary>
/// Invoked whenever the state of the light bulb changes.
/// </summary>
public event EventHandler<EventArgs> OnLightBulbStateChange;
public event EventHandler<EventArgs> OnLightColorChange;
private Color _color = Color.White;
[ViewVariables(VVAccess.ReadWrite)] public Color Color
{
get { return _color; }
set
{
_color = value;
OnLightColorChange?.Invoke(this, null);
UpdateColor();
}
}
public override string Name => "LightBulb";
public LightBulbType Type = LightBulbType.Tube;
/// <summary>
/// The current state of the light bulb. Invokes the OnLightBulbStateChange event when set.
/// It also updates the bulb's sprite accordingly.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)] public LightBulbState State
{
get { return _state; }
set
{
var sprite = Owner.GetComponent<SpriteComponent>();
OnLightBulbStateChange?.Invoke(this, EventArgs.Empty);
_state = value;
switch (value)
{
case LightBulbState.Normal:
sprite.LayerSetState(0, "normal");
break;
case LightBulbState.Broken:
sprite.LayerSetState(0, "broken");
break;
case LightBulbState.Burned:
sprite.LayerSetState(0, "burned");
break;
}
}
}
private LightBulbState _state = LightBulbState.Normal;
public override void ExposeData(ObjectSerializer serializer)
{
serializer.DataField(ref Type, "bulb", LightBulbType.Tube);
serializer.DataField(ref _color, "color", Color.White);
}
public void UpdateColor()
{
var sprite = Owner.GetComponent<SpriteComponent>();
sprite.Color = Color;
}
public override void Initialize()
{
base.Initialize();
UpdateColor();
}
}
}