Timescalable components

This commit is contained in:
Rendo 2025-07-08 00:05:23 +05:00
commit e527d1da44
11 changed files with 83 additions and 24 deletions

View file

@ -4,6 +4,7 @@ using Newlon.Systems.Effects;
namespace Newlon.Components;
[GlobalClass]
public partial class Entity : Node2D
{
#region Health points
@ -58,13 +59,13 @@ public partial class Entity : Node2D
#endregion
#region Effects
[Export] private Array<Effect> _effectImmunities = new();
private readonly Dictionary<string,Effect> _activeEffectSlots = new();
private readonly Dictionary<string,Timer> _effectSlotTimers = new();
private readonly Dictionary<string, Effect> _activeEffectSlots = new();
private readonly Dictionary<string, Timer> _effectSlotTimers = new();
[Signal] public delegate void EffectStartedEventHandler(Effect what);
[Signal] public delegate void EffectEndedEventHandler(Effect what);
[Signal] public delegate void EffectContinuedEventHandler(Effect what);
public virtual void GiveEffect(Effect what)
{
@ -108,7 +109,7 @@ public partial class Entity : Node2D
var timer = new Timer() { Autostart = false, OneShot = true };
AddChild(timer);
timer.Timeout += () => { EndEffectAtSlot(key); };
_effectSlotTimers.Add(key, timer);
}
@ -120,7 +121,7 @@ public partial class Entity : Node2D
public void ProcessEffects()
{
foreach(string key in _activeEffectSlots.Keys)
foreach (string key in _activeEffectSlots.Keys)
_activeEffectSlots[key]?.Process(this);
}
@ -148,8 +149,8 @@ public partial class Entity : Node2D
#endregion
#region Godot overrides
public override void _Ready()
{
HP = MaxHP;
}
{
HP = MaxHP;
}
#endregion
}

View file

@ -0,0 +1,17 @@
using Godot;
namespace Newlon.Components;
public partial class TimeScalableAnimationTree : AnimationTree
{
[Export] private Entity entity;
public override void _Ready()
{
entity.OnLocalTimescaleChanged += OnTimescaleChanged;
}
private void OnTimescaleChanged(float timescale)
{
Set("parameters/TimeScale/scale", timescale);
}
}

View file

@ -0,0 +1 @@
uid://dwlwi42smgxkb

View file

@ -0,0 +1,21 @@
using Godot;
namespace Newlon.Components;
public partial class TimeScalableTimer : Timer
{
[Export] private Entity entity;
private float internal_timescale;
public override void _Ready()
{
internal_timescale = entity.LocalTimescale;
entity.OnLocalTimescaleChanged += OnTimescaleChanged;
}
private void OnTimescaleChanged(float timescale)
{
WaitTime *= internal_timescale / timescale;
internal_timescale = timescale;
}
}

View file

@ -0,0 +1 @@
uid://c4jy0cnbnx33h