quantum-space-buddies/QSB/ShipSync/WorldObjects/QSBShipComponent.cs
2022-03-02 19:46:33 -08:00

69 lines
1.7 KiB
C#

using QSB.Messaging;
using QSB.ShipSync.Messages.Component;
using QSB.Utility;
using QSB.WorldSync;
namespace QSB.ShipSync.WorldObjects;
internal class QSBShipComponent : WorldObject<ShipComponent>
{
public override void SendInitialState(uint to)
{
if (AttachedObject._damaged)
{
this.SendMessage(new ComponentDamagedMessage());
}
else
{
this.SendMessage(new ComponentRepairedMessage());
}
this.SendMessage(new ComponentRepairTickMessage(AttachedObject._repairFraction));
}
public void SetDamaged()
{
if (AttachedObject._damaged)
{
return;
}
DebugLog.DebugWrite($"[S COMPONENT] {AttachedObject} Set damaged.");
AttachedObject._damaged = true;
AttachedObject._repairFraction = 0f;
AttachedObject.OnComponentDamaged();
AttachedObject.RaiseEvent(nameof(AttachedObject.OnDamaged), AttachedObject);
AttachedObject.UpdateColliderState();
var damageEffect = AttachedObject._damageEffect;
damageEffect.SetEffectBlend(1f);
}
public void SetRepaired()
{
if (!AttachedObject._damaged)
{
return;
}
DebugLog.DebugWrite($"[S COMPONENT] {AttachedObject} Set repaired.");
AttachedObject._damaged = false;
AttachedObject._repairFraction = 1f;
AttachedObject.OnComponentRepaired();
AttachedObject.RaiseEvent(nameof(AttachedObject.OnRepaired), AttachedObject);
AttachedObject.UpdateColliderState();
var damageEffect = AttachedObject._damageEffect;
damageEffect.SetEffectBlend(0f);
}
public void RepairTick(float repairFraction)
{
if (OWMath.ApproxEquals(AttachedObject._repairFraction, repairFraction))
{
return;
}
AttachedObject._repairFraction = repairFraction;
var damageEffect = AttachedObject._damageEffect;
damageEffect.SetEffectBlend(1f - repairFraction);
}
}