using System;
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.Audio;
using Robust.Server.GameObjects;
using Robust.Server.GameObjects.EntitySystems;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Random;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
using Robust.Shared.Utility;
namespace Content.Server.GameObjects.Components.Power
{
public enum LightBulbState
{
Normal,
Broken,
Burned,
}
public enum LightBulbType
{
Bulb,
Tube,
}
///
/// Component that represents a light bulb. Can be broken, or burned, which turns them mostly useless.
///
[RegisterComponent]
public class LightBulbComponent : Component, ILand
{
#pragma warning disable 649
[Dependency] private readonly IPrototypeManager _prototypeManager;
[Dependency] private readonly IRobustRandom _random;
#pragma warning restore 649
///
/// Invoked whenever the state of the light bulb changes.
///
public event EventHandler OnLightBulbStateChange;
public event EventHandler 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;
private int _burningTemperature;
public int BurningTemperature => _burningTemperature;
private int _powerUse;
public int PowerUse => _powerUse;
///
/// The current state of the light bulb. Invokes the OnLightBulbStateChange event when set.
/// It also updates the bulb's sprite accordingly.
///
[ViewVariables(VVAccess.ReadWrite)] public LightBulbState State
{
get { return _state; }
set
{
var sprite = Owner.GetComponent();
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);
serializer.DataFieldCached(ref _burningTemperature, "BurningTemperature", 1400);
serializer.DataFieldCached(ref _powerUse, "PowerUse", 40);
}
public void UpdateColor()
{
var sprite = Owner.GetComponent();
sprite.Color = Color;
}
public override void Initialize()
{
base.Initialize();
UpdateColor();
}
public void Land(LandEventArgs eventArgs)
{
if (State == LightBulbState.Broken)
return;
var soundCollection = _prototypeManager.Index("glassbreak");
var file = _random.Pick(soundCollection.PickFiles);
EntitySystem.Get().PlayFromEntity(file, Owner);
State = LightBulbState.Broken;
}
}
}