refactor: Magic system (#1138)
Co-authored-by: Ed <96445749+TheShuEd@users.noreply.github.com>
This commit is contained in:
@@ -8,27 +8,18 @@ using Content.Shared.Rounding;
|
||||
|
||||
namespace Content.Shared._CP14.MagicEnergy;
|
||||
|
||||
public partial class SharedCP14MagicEnergySystem : EntitySystem
|
||||
public abstract class SharedCP14MagicEnergySystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly AlertsSystem _alerts = default!;
|
||||
[Dependency] private readonly SharedAmbientSoundSystem _ambient = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
SubscribeLocalEvent<CP14MagicEnergyAmbientSoundComponent, CP14SlotCrystalPowerChangedEvent>(OnSlotPowerChanged);
|
||||
|
||||
SubscribeLocalEvent<CP14MagicEnergyContainerComponent, ComponentStartup>(OnComponentStartup);
|
||||
SubscribeLocalEvent<CP14MagicEnergyContainerComponent, ComponentShutdown>(OnComponentShutdown);
|
||||
|
||||
SubscribeLocalEvent<CP14MagicEnergyExaminableComponent, ExaminedEvent>(OnExamined);
|
||||
}
|
||||
|
||||
private void OnSlotPowerChanged(Entity<CP14MagicEnergyAmbientSoundComponent> ent, ref CP14SlotCrystalPowerChangedEvent args)
|
||||
{
|
||||
if (TryComp<AmbientSoundComponent>(ent, out var ambient))
|
||||
{
|
||||
_ambient.SetAmbience(ent, args.Powered);
|
||||
}
|
||||
SubscribeLocalEvent<CP14MagicEnergyAmbientSoundComponent, CP14SlotCrystalPowerChangedEvent>(OnSlotPowerChanged);
|
||||
}
|
||||
|
||||
private void OnComponentStartup(Entity<CP14MagicEnergyContainerComponent> ent, ref ComponentStartup args)
|
||||
@@ -38,135 +29,12 @@ public partial class SharedCP14MagicEnergySystem : EntitySystem
|
||||
|
||||
private void OnComponentShutdown(Entity<CP14MagicEnergyContainerComponent> ent, ref ComponentShutdown args)
|
||||
{
|
||||
if (ent.Comp.MagicAlert == null)
|
||||
if (ent.Comp.MagicAlert is null)
|
||||
return;
|
||||
|
||||
_alerts.ClearAlert(ent, ent.Comp.MagicAlert.Value);
|
||||
}
|
||||
|
||||
public string GetEnergyExaminedText(EntityUid uid, CP14MagicEnergyContainerComponent ent)
|
||||
{
|
||||
var power = (int)(ent.Energy / ent.MaxEnergy * 100);
|
||||
|
||||
var color = "#3fc488";
|
||||
if (power < 66)
|
||||
color = "#f2a93a";
|
||||
if (power < 33)
|
||||
color = "#c23030";
|
||||
|
||||
return Loc.GetString("cp14-magic-energy-scan-result",
|
||||
("item", MetaData(uid).EntityName),
|
||||
("power", power),
|
||||
("color", color));
|
||||
}
|
||||
|
||||
public void ChangeEnergy(EntityUid uid,
|
||||
FixedPoint2 energy,
|
||||
out FixedPoint2 changedEnergy,
|
||||
out FixedPoint2 overloadEnergy,
|
||||
CP14MagicEnergyContainerComponent? component = null,
|
||||
bool safe = false)
|
||||
{
|
||||
changedEnergy = 0;
|
||||
overloadEnergy = 0;
|
||||
|
||||
if (!Resolve(uid, ref component, false))
|
||||
return;
|
||||
|
||||
if (!safe)
|
||||
{
|
||||
//Overload
|
||||
if (component.Energy + energy > component.MaxEnergy && component.UnsafeSupport)
|
||||
{
|
||||
overloadEnergy = (component.Energy + energy) - component.MaxEnergy;
|
||||
RaiseLocalEvent(uid,
|
||||
new CP14MagicEnergyOverloadEvent()
|
||||
{
|
||||
OverloadEnergy = (component.Energy + energy) - component.MaxEnergy,
|
||||
});
|
||||
}
|
||||
|
||||
//Burn out
|
||||
if (component.Energy + energy < 0 && component.UnsafeSupport)
|
||||
{
|
||||
overloadEnergy = component.Energy + energy;
|
||||
RaiseLocalEvent(uid,
|
||||
new CP14MagicEnergyBurnOutEvent()
|
||||
{
|
||||
BurnOutEnergy = -energy - component.Energy
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
var oldEnergy = component.Energy;
|
||||
var newEnergy = Math.Clamp((float)component.Energy + (float)energy, 0, (float)component.MaxEnergy);
|
||||
|
||||
changedEnergy = newEnergy - oldEnergy;
|
||||
component.Energy = newEnergy;
|
||||
|
||||
if (oldEnergy != newEnergy)
|
||||
{
|
||||
RaiseLocalEvent(uid,
|
||||
new CP14MagicEnergyLevelChangeEvent()
|
||||
{
|
||||
OldValue = oldEnergy,
|
||||
NewValue = newEnergy,
|
||||
MaxValue = component.MaxEnergy,
|
||||
});
|
||||
}
|
||||
|
||||
UpdateMagicAlert((uid, component));
|
||||
}
|
||||
|
||||
public void TransferEnergy(EntityUid from,
|
||||
EntityUid to,
|
||||
FixedPoint2 energy,
|
||||
out FixedPoint2 changedEnergy,
|
||||
out FixedPoint2 overloadEnergy,
|
||||
CP14MagicEnergyContainerComponent? fromComponent = null,
|
||||
CP14MagicEnergyContainerComponent? toComponent = null,
|
||||
bool safe = false)
|
||||
{
|
||||
changedEnergy = 0;
|
||||
overloadEnergy = 0;
|
||||
|
||||
if (!Resolve(from, ref fromComponent) || !Resolve(to, ref toComponent))
|
||||
return;
|
||||
|
||||
var transferEnergy = energy;
|
||||
//We check how much space is left in the container so as not to overload it, but only if it does not support overloading
|
||||
if (!toComponent.UnsafeSupport || safe)
|
||||
{
|
||||
var freeSpace = toComponent.MaxEnergy - toComponent.Energy;
|
||||
transferEnergy = FixedPoint2.Min(freeSpace, energy);
|
||||
}
|
||||
|
||||
ChangeEnergy(from, -transferEnergy, out var change, out var overload, fromComponent, safe);
|
||||
ChangeEnergy(to , -(change + overload), out changedEnergy, out overloadEnergy, toComponent, safe);
|
||||
}
|
||||
|
||||
public bool HasEnergy(EntityUid uid, FixedPoint2 energy, CP14MagicEnergyContainerComponent? component = null, bool safe = false)
|
||||
{
|
||||
if (!Resolve(uid, ref component))
|
||||
return false;
|
||||
|
||||
if (safe == false && component.UnsafeSupport)
|
||||
return true;
|
||||
|
||||
return component.Energy >= energy;
|
||||
}
|
||||
|
||||
private void UpdateMagicAlert(Entity<CP14MagicEnergyContainerComponent> ent)
|
||||
{
|
||||
if (ent.Comp.MagicAlert == null)
|
||||
return;
|
||||
|
||||
var level = ContentHelpers.RoundToLevels(MathF.Max(0f, (float)ent.Comp.Energy),
|
||||
(float)ent.Comp.MaxEnergy,
|
||||
_alerts.GetMaxSeverity(ent.Comp.MagicAlert.Value));
|
||||
_alerts.ShowAlert(ent, ent.Comp.MagicAlert.Value, (short)level);
|
||||
}
|
||||
|
||||
private void OnExamined(Entity<CP14MagicEnergyExaminableComponent> ent, ref ExaminedEvent args)
|
||||
{
|
||||
if (!TryComp<CP14MagicEnergyContainerComponent>(ent, out var magicContainer))
|
||||
@@ -175,7 +43,121 @@ public partial class SharedCP14MagicEnergySystem : EntitySystem
|
||||
if (!args.IsInDetailsRange)
|
||||
return;
|
||||
|
||||
args.PushMarkup(GetEnergyExaminedText(ent, magicContainer));
|
||||
args.PushMarkup(GetEnergyExaminedText((ent, magicContainer)));
|
||||
}
|
||||
|
||||
private void OnSlotPowerChanged(Entity<CP14MagicEnergyAmbientSoundComponent> ent, ref CP14SlotCrystalPowerChangedEvent args)
|
||||
{
|
||||
_ambient.SetAmbience(ent, args.Powered);
|
||||
}
|
||||
|
||||
public void ChangeEnergy(Entity<CP14MagicEnergyContainerComponent?> ent,
|
||||
FixedPoint2 energy,
|
||||
out FixedPoint2 deltaEnergy,
|
||||
out FixedPoint2 overloadEnergy,
|
||||
bool safe = false)
|
||||
{
|
||||
deltaEnergy = 0;
|
||||
overloadEnergy = 0;
|
||||
|
||||
if (!Resolve(ent, ref ent.Comp, false))
|
||||
return;
|
||||
|
||||
if (!safe)
|
||||
{
|
||||
// Overload
|
||||
if (ent.Comp.Energy + energy > ent.Comp.MaxEnergy && ent.Comp.UnsafeSupport)
|
||||
{
|
||||
overloadEnergy = ent.Comp.Energy + energy - ent.Comp.MaxEnergy;
|
||||
RaiseLocalEvent(ent, new CP14MagicEnergyOverloadEvent(overloadEnergy));
|
||||
}
|
||||
|
||||
// Burn out
|
||||
if (ent.Comp.Energy + energy < 0 && ent.Comp.UnsafeSupport)
|
||||
{
|
||||
overloadEnergy = ent.Comp.Energy + energy;
|
||||
RaiseLocalEvent(ent, new CP14MagicEnergyBurnOutEvent(-energy - ent.Comp.Energy));
|
||||
}
|
||||
}
|
||||
|
||||
var oldEnergy = ent.Comp.Energy;
|
||||
var newEnergy = Math.Clamp((float) ent.Comp.Energy + (float) energy, 0, (float) ent.Comp.MaxEnergy);
|
||||
|
||||
deltaEnergy = newEnergy - oldEnergy;
|
||||
ent.Comp.Energy = newEnergy;
|
||||
|
||||
if (oldEnergy != newEnergy)
|
||||
RaiseLocalEvent(ent, new CP14MagicEnergyLevelChangeEvent(oldEnergy, newEnergy, ent.Comp.MaxEnergy));
|
||||
|
||||
UpdateMagicAlert((ent, ent.Comp));
|
||||
}
|
||||
|
||||
public void TransferEnergy(Entity<CP14MagicEnergyContainerComponent?> sender,
|
||||
Entity<CP14MagicEnergyContainerComponent?> receiver,
|
||||
FixedPoint2 energy,
|
||||
out FixedPoint2 deltaEnergy,
|
||||
out FixedPoint2 overloadEnergy,
|
||||
bool safe = false)
|
||||
{
|
||||
deltaEnergy = 0;
|
||||
overloadEnergy = 0;
|
||||
|
||||
if (!Resolve(sender, ref sender.Comp) || !Resolve(receiver, ref receiver.Comp))
|
||||
return;
|
||||
|
||||
var transferEnergy = energy;
|
||||
//We check how much space is left in the container so as not to overload it, but only if it does not support overloading
|
||||
if (!receiver.Comp.UnsafeSupport || safe)
|
||||
{
|
||||
var freeSpace = receiver.Comp.MaxEnergy - receiver.Comp.Energy;
|
||||
transferEnergy = FixedPoint2.Min(freeSpace, energy);
|
||||
}
|
||||
|
||||
ChangeEnergy(sender, -transferEnergy, out var change, out var overload, safe);
|
||||
ChangeEnergy(receiver , -(change + overload), out deltaEnergy, out overloadEnergy, safe);
|
||||
}
|
||||
|
||||
public bool HasEnergy(EntityUid uid, FixedPoint2 energy, CP14MagicEnergyContainerComponent? component = null, bool safe = false)
|
||||
{
|
||||
if (!Resolve(uid, ref component))
|
||||
return false;
|
||||
|
||||
if (!safe && component.UnsafeSupport)
|
||||
return true;
|
||||
|
||||
return component.Energy >= energy;
|
||||
}
|
||||
|
||||
public string GetEnergyExaminedText(Entity<CP14MagicEnergyContainerComponent> ent)
|
||||
{
|
||||
var power = (int) (ent.Comp.Energy / ent.Comp.MaxEnergy * 100);
|
||||
|
||||
// TODO: customization for examined
|
||||
|
||||
var color = "#3fc488";
|
||||
if (power < 66)
|
||||
color = "#f2a93a";
|
||||
|
||||
if (power < 33)
|
||||
color = "#c23030";
|
||||
|
||||
return Loc.GetString("cp14-magic-energy-scan-result",
|
||||
("item", MetaData(ent).EntityName),
|
||||
("power", power),
|
||||
("color", color));
|
||||
}
|
||||
|
||||
private void UpdateMagicAlert(Entity<CP14MagicEnergyContainerComponent> ent)
|
||||
{
|
||||
if (ent.Comp.MagicAlert is null)
|
||||
return;
|
||||
|
||||
var level = ContentHelpers.RoundToLevels(
|
||||
MathF.Max(0f, (float) ent.Comp.Energy),
|
||||
(float) ent.Comp.MaxEnergy,
|
||||
_alerts.GetMaxSeverity(ent.Comp.MagicAlert.Value));
|
||||
|
||||
_alerts.ShowAlert(ent, ent.Comp.MagicAlert.Value, (short) level);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,9 +166,16 @@ public partial class SharedCP14MagicEnergySystem : EntitySystem
|
||||
/// </summary>
|
||||
public sealed class CP14MagicEnergyLevelChangeEvent : EntityEventArgs
|
||||
{
|
||||
public FixedPoint2 OldValue;
|
||||
public FixedPoint2 NewValue;
|
||||
public FixedPoint2 MaxValue;
|
||||
public readonly FixedPoint2 OldValue;
|
||||
public readonly FixedPoint2 NewValue;
|
||||
public readonly FixedPoint2 MaxValue;
|
||||
|
||||
public CP14MagicEnergyLevelChangeEvent(FixedPoint2 oldValue, FixedPoint2 newValue, FixedPoint2 maxValue)
|
||||
{
|
||||
OldValue = oldValue;
|
||||
NewValue = newValue;
|
||||
MaxValue = maxValue;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -194,7 +183,12 @@ public sealed class CP14MagicEnergyLevelChangeEvent : EntityEventArgs
|
||||
/// </summary>
|
||||
public sealed class CP14MagicEnergyOverloadEvent : EntityEventArgs
|
||||
{
|
||||
public FixedPoint2 OverloadEnergy;
|
||||
public readonly FixedPoint2 OverloadEnergy;
|
||||
|
||||
public CP14MagicEnergyOverloadEvent(FixedPoint2 overloadEnergy)
|
||||
{
|
||||
OverloadEnergy = overloadEnergy;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -202,5 +196,10 @@ public sealed class CP14MagicEnergyOverloadEvent : EntityEventArgs
|
||||
/// </summary>
|
||||
public sealed class CP14MagicEnergyBurnOutEvent : EntityEventArgs
|
||||
{
|
||||
public FixedPoint2 BurnOutEnergy;
|
||||
public readonly FixedPoint2 BurnOutEnergy;
|
||||
|
||||
public CP14MagicEnergyBurnOutEvent(FixedPoint2 burnOutEnergy)
|
||||
{
|
||||
BurnOutEnergy = burnOutEnergy;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user