Powered light ECS (#5028)
* Brrrr ECS * Create lit on powered system * Light bulb ECS * Finishing porting to ECS * Minor touches * Removed events * Removed old comments * Fixed test * To popup system
This commit is contained in:
@@ -1,131 +1,45 @@
|
||||
using System;
|
||||
using Content.Server.Light.EntitySystems;
|
||||
using Content.Shared.Acts;
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.Light;
|
||||
using Content.Shared.Sound;
|
||||
using Content.Shared.Throwing;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Analyzers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Light.Components
|
||||
{
|
||||
public enum LightBulbState
|
||||
{
|
||||
Normal,
|
||||
Broken,
|
||||
Burned,
|
||||
}
|
||||
|
||||
public enum LightBulbType
|
||||
{
|
||||
Bulb,
|
||||
Tube,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Component that represents a light bulb. Can be broken, or burned, which turns them mostly useless.
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
[RegisterComponent, Friend(typeof(LightBulbSystem))]
|
||||
public class LightBulbComponent : Component, IBreakAct
|
||||
{
|
||||
/// <summary>
|
||||
/// Invoked whenever the state of the light bulb changes.
|
||||
/// </summary>
|
||||
public event EventHandler<EventArgs>? OnLightBulbStateChange;
|
||||
public event EventHandler<EventArgs?>? OnLightColorChange;
|
||||
public override string Name => "LightBulb";
|
||||
|
||||
[DataField("color")]
|
||||
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 Color Color = Color.White;
|
||||
|
||||
[DataField("bulb")]
|
||||
public LightBulbType Type = LightBulbType.Tube;
|
||||
|
||||
[DataField("startingState")]
|
||||
public LightBulbState State = LightBulbState.Normal;
|
||||
|
||||
[DataField("BurningTemperature")]
|
||||
private int _burningTemperature = 1400;
|
||||
public int BurningTemperature => _burningTemperature;
|
||||
public int BurningTemperature = 1400;
|
||||
|
||||
[DataField("PowerUse")]
|
||||
private int _powerUse = 40;
|
||||
public int PowerUse => _powerUse;
|
||||
public int PowerUse = 40;
|
||||
|
||||
[DataField("breakSound")]
|
||||
private SoundSpecifier _breakSound = new SoundCollectionSpecifier("GlassBreak");
|
||||
|
||||
/// <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 void UpdateColor()
|
||||
{
|
||||
if (!Owner.TryGetComponent(out SpriteComponent? sprite))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
sprite.Color = Color;
|
||||
}
|
||||
|
||||
protected override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
UpdateColor();
|
||||
}
|
||||
public SoundSpecifier BreakSound = new SoundCollectionSpecifier("GlassBreak");
|
||||
|
||||
// TODO: move me to ECS
|
||||
public void OnBreak(BreakageEventArgs eventArgs)
|
||||
{
|
||||
State = LightBulbState.Broken;
|
||||
}
|
||||
|
||||
public void PlayBreakSound()
|
||||
{
|
||||
SoundSystem.Play(Filter.Pvs(Owner), _breakSound.GetSound(), Owner);
|
||||
EntitySystem.Get<LightBulbSystem>()
|
||||
.SetState(Owner.Uid, LightBulbState.Broken, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user