Effect system and snowpea

This commit is contained in:
Фёдор Веселов 2024-10-02 19:46:51 +05:00
commit e797918e23
18 changed files with 353 additions and 16 deletions

View file

@ -7,11 +7,20 @@ namespace Newlon;
// Class for general-used constants and utility tools
//
public partial class Utility
public class Utility
{
//
// Code-accessed enumenators
//
#region Enums
public enum EffectSlots {FREEZE, STUN, POISON};
#endregion
public const int EffectSlotCount = 3;
public const int TileWidth = 50;
public const int TileHeight = 60;
public const int LayersCount = 3;
public static readonly Vector2I LeftFieldBoundary = new(305,76);
public static readonly Vector2I RightFieldBoundary = new(755,376);

View file

@ -1,5 +1,5 @@
using Godot;
using System;
using Newlon.Systems.Effects;
namespace Newlon.Components;
@ -13,6 +13,8 @@ public partial class LinearProjectile : Area2D, IProjectile
private float _speed;
[Export]
private int _damage;
[Export]
private Effect _impactEffect;
private int _line;
public int Line { get => _line; set { _line = value; } }
@ -27,6 +29,8 @@ public partial class LinearProjectile : Area2D, IProjectile
if (entity != null && entity.Line == _line)
{
entity.TakeDamage(_damage);
if (entity is IEffectHandler effectHandler && _impactEffect != null)
effectHandler.GiveEffect(_impactEffect);
QueueFree();
}
}

View file

@ -1,8 +1,10 @@
using Godot;
using System.Collections.Generic;
using Godot;
using Newlon.Systems.Effects;
namespace Newlon.Components.Zombies;
public partial class RuntimeZombieData : Node2D, IEntity, ILocalTimescale
public partial class RuntimeZombieData : Node2D, IEntity, ILocalTimescale, IEffectHandler
{
[Signal]
public delegate void OnHPChangedEventHandler(int deltaHP);
@ -19,7 +21,7 @@ public partial class RuntimeZombieData : Node2D, IEntity, ILocalTimescale
private float _localTimescale = 1.0f;
public int Hp => _hp;
public int MaxHp => _maxHP;
public int MaxHp => _maxHP;
public int Line => _line;
@ -36,6 +38,15 @@ public partial class RuntimeZombieData : Node2D, IEntity, ILocalTimescale
public override void _Ready()
{
_hp = _maxHP;
// Effect timers setup
for(int i = 0; i < Utility.EffectSlotCount; i++)
{
var timer = new Timer() {Autostart = false, OneShot = true};
_effectSlotTimers[i] = timer;
AddChild(timer);
timer.Timeout += () => {EndEffectAtSlot(i);};
}
}
public void Heal(int amount)
@ -59,4 +70,44 @@ public partial class RuntimeZombieData : Node2D, IEntity, ILocalTimescale
QueueFree();
}
}
#region Effects system
private readonly Effect[] _activeEffectSlots = new Effect[Utility.EffectSlotCount];
private readonly Timer[] _effectSlotTimers = new Timer[Utility.EffectSlotCount];
// Effect handling
public void GiveEffect(Effect what)
{
int slot = (int)what.Slot;
if(_activeEffectSlots[slot] != null)
{
_effectSlotTimers[slot].Stop();
_activeEffectSlots[slot].Exit(this);
}
_effectSlotTimers[slot].WaitTime = what.Duration;
_effectSlotTimers[slot].Start();
what.Enter(this);
_activeEffectSlots[slot] = what;
}
public void EndEffect(Effect what)
{
what.Exit(this);
_activeEffectSlots[(int)what.Slot] = null;
}
public void ProcessEffects()
{
for(int i = 0; i < Utility.EffectSlotCount; i++)
_activeEffectSlots[i].Process(this);
}
private void EndEffectAtSlot(int slot)
{
_activeEffectSlots[slot].Exit(this);
_activeEffectSlots[slot] = null;
}
#endregion
}

View file

@ -4,7 +4,10 @@ namespace Newlon.Components.Zombies;
public partial class ZombieMover : Node
{
private float _speed;
[Export(hintString: "suffix:tile/sec")]
private float _speedMultiplier = 1.0f;
private float _speed = 1;
private Node2D _zombie;
@ -15,7 +18,12 @@ public partial class ZombieMover : Node
public override void _PhysicsProcess(double delta)
{
_zombie.Position -= _zombie.Transform.X * _speed * (float)delta * Utility.TileWidth * GetParent<RuntimeZombieData>().LocalTimescale;
_zombie.Position -= _zombie.Transform.X
* _speed
* (float)delta
* Utility.TileWidth
* GetParent<RuntimeZombieData>().LocalTimescale
* _speedMultiplier;
}
public void SetSpeed(float speed)

View file

@ -1,11 +1,12 @@
using Godot;
using System;
namespace Newlon.Systems.Effects;
public abstract partial class Effect : Resource
{
public abstract void Enter();
public abstract void Process();
public abstract void Exit();
[Export] public float Duration;
[Export] public Utility.EffectSlots Slot;
public abstract void Enter(Node target);
public abstract void Process(Node target);
public abstract void Exit(Node target);
}

View file

@ -2,5 +2,7 @@ namespace Newlon.Systems.Effects;
public interface IEffectHandler
{
void GiveEffect(Effect what);
void EndEffect(Effect what);
void ProcessEffects();
}

View file

@ -0,0 +1,35 @@
using Godot;
using Newlon.Components;
using System;
namespace Newlon.Systems.Effects;
public partial class SlownessEffect : Effect
{
[Export] public Color ColorOverride;
[Export] public float Multiplier;
public override void Enter(Node target)
{
if(target is IEffectHandler handler)
{
if(target is ILocalTimescale timescalable)
timescalable.LocalTimescale *= Multiplier;
if(target is CanvasItem canvasItem)
canvasItem.Modulate = ColorOverride;
}
}
public override void Exit(Node target)
{
if(target is ILocalTimescale timescalable)
timescalable.LocalTimescale /= Multiplier;
if(target is CanvasItem canvasItem)
canvasItem.Modulate = Colors.White;
}
public override void Process(Node target)
{
}
}