Add Active Buffs management and UI integration
This commit is contained in:
31
Scripts/ActiveBuffUi.cs
Normal file
31
Scripts/ActiveBuffUi.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using Godot;
|
||||
using ParasiticGod.Scripts.Core.Effects;
|
||||
|
||||
namespace ParasiticGod.Scripts;
|
||||
|
||||
[GlobalClass]
|
||||
public partial class ActiveBuffUi : Button
|
||||
{
|
||||
private Buff _buff;
|
||||
|
||||
public void SetBuff(Buff buff)
|
||||
{
|
||||
_buff = buff;
|
||||
Disabled = true;
|
||||
UpdateDisplay();
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if (_buff != null)
|
||||
{
|
||||
UpdateDisplay();
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateDisplay()
|
||||
{
|
||||
Text = _buff.Name;
|
||||
TooltipText = $"x{_buff.Multiplier:F1} to {_buff.Name.Split(' ')[0]}\n{_buff.Duration:F0}s remaining";
|
||||
}
|
||||
}
|
1
Scripts/ActiveBuffUi.cs.uid
Normal file
1
Scripts/ActiveBuffUi.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://chnf0t5xdosys
|
46
Scripts/ActiveBuffsManager.cs
Normal file
46
Scripts/ActiveBuffsManager.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Godot;
|
||||
using ParasiticGod.Scripts.Core;
|
||||
using ParasiticGod.Scripts.Core.Effects;
|
||||
using ParasiticGod.Scripts.Singletons;
|
||||
|
||||
namespace ParasiticGod.Scripts;
|
||||
|
||||
[GlobalClass]
|
||||
public partial class ActiveBuffsManager : Node
|
||||
{
|
||||
[Export] private PackedScene _activeBuffScene;
|
||||
|
||||
private readonly Dictionary<Guid, ActiveBuffUi> _activeBuffUis = new();
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
GameBus.Instance.BuffAdded += OnBuffAdded;
|
||||
GameBus.Instance.BuffRemoved += OnBuffRemoved;
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
if (GameBus.Instance == null) return;
|
||||
GameBus.Instance.BuffAdded -= OnBuffAdded;
|
||||
GameBus.Instance.BuffRemoved -= OnBuffRemoved;
|
||||
}
|
||||
|
||||
private void OnBuffAdded(Buff buff)
|
||||
{
|
||||
var buffInstance = _activeBuffScene.Instantiate<ActiveBuffUi>();
|
||||
AddChild(buffInstance);
|
||||
buffInstance.SetBuff(buff);
|
||||
_activeBuffUis.Add(buff.Id, buffInstance);
|
||||
}
|
||||
|
||||
private void OnBuffRemoved(Buff buff)
|
||||
{
|
||||
if (_activeBuffUis.TryGetValue(buff.Id, out var buffUi))
|
||||
{
|
||||
buffUi.QueueFree();
|
||||
_activeBuffUis.Remove(buff.Id);
|
||||
}
|
||||
}
|
||||
}
|
1
Scripts/ActiveBuffsManager.cs.uid
Normal file
1
Scripts/ActiveBuffsManager.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://ddshg236tlltt
|
@@ -1,4 +1,5 @@
|
||||
using Godot;
|
||||
using ParasiticGod.Scripts.Singletons;
|
||||
|
||||
namespace ParasiticGod.Scripts.Core.Effects;
|
||||
|
||||
@@ -13,11 +14,13 @@ public partial class ApplyBuffEffect : Effect
|
||||
{
|
||||
var newBuff = new Buff
|
||||
{
|
||||
Name = $"{TargetStat} x{Multiplier}",
|
||||
Multiplier = Multiplier,
|
||||
Duration = Duration
|
||||
};
|
||||
|
||||
gameState.ActiveBuffs.Add(newBuff);
|
||||
GameBus.Instance.NotifyBuffAdded(newBuff);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
|
@@ -1,7 +1,11 @@
|
||||
using System;
|
||||
|
||||
namespace ParasiticGod.Scripts.Core.Effects;
|
||||
|
||||
public class Buff
|
||||
{
|
||||
public Guid Id { get; } = Guid.NewGuid(); // Unique identifier
|
||||
public string Name { get; set; } // For display purposes
|
||||
public float Multiplier { get; set; } = 1.0f;
|
||||
public double Duration { get; set; }
|
||||
}
|
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using ParasiticGod.Scripts.Singletons;
|
||||
|
||||
namespace ParasiticGod.Scripts.Core;
|
||||
|
||||
@@ -23,6 +24,7 @@ public class GameLogic
|
||||
buff.Duration -= delta;
|
||||
if (buff.Duration <= 0)
|
||||
{
|
||||
GameBus.Instance.NotifyBuffRemoved(buff);
|
||||
state.ActiveBuffs.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
|
@@ -18,6 +18,8 @@ public partial class GameBus : Node
|
||||
public event Action<MiracleDefinition> MiraclePerformed;
|
||||
public event Action<List<MiracleDefinition>> MiraclesUnlocked;
|
||||
public event Action<MiracleDefinition> MiracleCompleted;
|
||||
public event Action<Buff> BuffAdded;
|
||||
public event Action<Buff> BuffRemoved;
|
||||
public event Action PopulationVisualsUpdated;
|
||||
|
||||
public override void _EnterTree()
|
||||
@@ -80,6 +82,16 @@ public partial class GameBus : Node
|
||||
PopulationVisualsUpdated?.Invoke();
|
||||
}
|
||||
|
||||
public void NotifyBuffAdded(Buff buff)
|
||||
{
|
||||
BuffAdded?.Invoke(buff);
|
||||
}
|
||||
|
||||
public void NotifyBuffRemoved(Buff buff)
|
||||
{
|
||||
BuffRemoved?.Invoke(buff);
|
||||
}
|
||||
|
||||
public void SubscribeToStat(Stat stat, Action<double> listener) => _gameState.Subscribe(stat, listener);
|
||||
public void UnsubscribeFromStat(Stat stat, Action<double> listener) => _gameState.Unsubscribe(stat, listener);
|
||||
|
||||
|
Reference in New Issue
Block a user