|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using FunkEngine; |
| 4 | +using Godot; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Status Effect class. |
| 8 | +/// Preferably set up as a static default status, then apply status using GetInstance, but custom statuses can be defined elsewhere. |
| 9 | +/// Invoke StatusEnd to remove status. |
| 10 | +/// </summary> |
| 11 | +public partial class StatusEffect : TextureRect, IBattleEvent |
| 12 | +{ //TODO: Status effects that are permanent, and status effects that don't take up a slot/are invisible |
| 13 | + public static readonly string LoadPath = "res://Classes/StatusEffects/StatusIcon.tscn"; |
| 14 | + public PuppetTemplate Sufferer { get; private set; } |
| 15 | + public int Count { get; private set; } |
| 16 | + |
| 17 | + public string StatusName { get; private set; } |
| 18 | + |
| 19 | + [Export] |
| 20 | + private Label CountLabel { get; set; } |
| 21 | + |
| 22 | + internal delegate void StatusEndHandler(StatusEffect status); |
| 23 | + internal event StatusEndHandler StatusEnd; |
| 24 | + |
| 25 | + #region DefaultStatuses |
| 26 | + private static readonly Action<BattleEventArgs, StatusEffect> BlockEffect = (e, self) => |
| 27 | + { |
| 28 | + if (e is BattleDirector.Harbinger.OnDamageInstanceArgs dmgArgs) |
| 29 | + { |
| 30 | + if (dmgArgs.Dmg.Target != self.Sufferer || dmgArgs.Dmg.Damage <= 0) |
| 31 | + return; |
| 32 | + dmgArgs.Dmg.ModifyDamage(0, 0); |
| 33 | + self.DecCount(); |
| 34 | + } |
| 35 | + }; |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// On the owner receiving a damage instance, if valid (correct target and dmg > 0) sets damage to 0 and reduces count. |
| 39 | + /// </summary> |
| 40 | + public static readonly StatusEffect Block = new StatusEffect() |
| 41 | + .InitStatus( |
| 42 | + "Block", |
| 43 | + BlockEffect, |
| 44 | + BattleEffectTrigger.OnDamageInstance, |
| 45 | + GD.Load<Texture2D>("res://Classes/StatusEffects/Assets/Status_Block.png") |
| 46 | + ) |
| 47 | + .SetTags(true); |
| 48 | + |
| 49 | + private static readonly Action<BattleEventArgs, StatusEffect> MulliganEffect = (e, self) => |
| 50 | + { |
| 51 | + if (e is not BattleDirector.Harbinger.NoteHitArgs { Timing: Timing.Miss }) |
| 52 | + return; |
| 53 | + e.BD.NPB.SetIgnoreMiss(true); //Intercept the miss |
| 54 | + self.DecCount(); |
| 55 | + }; |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// If the player missed, take damage, but don't receive combo penalty. |
| 59 | + /// </summary> |
| 60 | + public static readonly StatusEffect Mulligan = new StatusEffect() |
| 61 | + .InitStatus( |
| 62 | + "Mulligan", |
| 63 | + MulliganEffect, |
| 64 | + BattleEffectTrigger.NoteHit, |
| 65 | + GD.Load<Texture2D>("res://Classes/StatusEffects/Assets/Status_Mulligan.png") |
| 66 | + ) |
| 67 | + .SetTags(true); |
| 68 | + #endregion |
| 69 | + |
| 70 | + private BattleEffectTrigger _trigger; |
| 71 | + private Action<BattleEventArgs, StatusEffect> _effect; |
| 72 | + |
| 73 | + public BattleEffectTrigger GetTrigger() |
| 74 | + { |
| 75 | + return _trigger; |
| 76 | + } |
| 77 | + |
| 78 | + public void OnTrigger(BattleEventArgs e) |
| 79 | + { |
| 80 | + _effect(e, this); |
| 81 | + } |
| 82 | + |
| 83 | + public StatusEffect InitStatus( |
| 84 | + string name, |
| 85 | + Action<BattleEventArgs, StatusEffect> effect, |
| 86 | + BattleEffectTrigger trigger, |
| 87 | + Texture2D texture = null |
| 88 | + ) |
| 89 | + { |
| 90 | + _effect = effect; |
| 91 | + _trigger = trigger; |
| 92 | + StatusName = name; |
| 93 | + Texture = texture; |
| 94 | + return this; |
| 95 | + } |
| 96 | + |
| 97 | + public StatusEffect GetInstance(int count = 1) |
| 98 | + { |
| 99 | + StatusEffect result = GD.Load<PackedScene>(LoadPath).Instantiate<StatusEffect>(); |
| 100 | + result.SetCount(count); |
| 101 | + result.InitStatus(Name, _effect, _trigger, Texture); |
| 102 | + result.SetTags(_stackable, _refreshes); |
| 103 | + return result; |
| 104 | + } |
| 105 | + |
| 106 | + public void SetOwner(PuppetTemplate owner) |
| 107 | + { |
| 108 | + Sufferer = owner; |
| 109 | + } |
| 110 | + |
| 111 | + public void IncCount(int count = 1) |
| 112 | + { |
| 113 | + SetCount(Count + count); |
| 114 | + } |
| 115 | + |
| 116 | + public void DecCount(int count = 1) |
| 117 | + { |
| 118 | + SetCount(Count - count); |
| 119 | + } |
| 120 | + |
| 121 | + public void SetCount(int count) |
| 122 | + { |
| 123 | + Count = count; |
| 124 | + CountLabel.Text = Count.ToString(); |
| 125 | + if (Count <= 0) |
| 126 | + { |
| 127 | + StatusEnd?.Invoke(this); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + /// <summary> |
| 132 | + /// Re-applying a status increases the count. |
| 133 | + /// </summary> |
| 134 | + private bool _stackable; |
| 135 | + |
| 136 | + /// <summary> |
| 137 | + /// Re-applying a status sets the count to the higher counte |
| 138 | + /// </summary> |
| 139 | + private bool _refreshes; |
| 140 | + |
| 141 | + public StatusEffect SetTags(bool stackable = false, bool refreshes = false) |
| 142 | + { |
| 143 | + _stackable = stackable; |
| 144 | + _refreshes = refreshes; |
| 145 | + return this; |
| 146 | + } |
| 147 | + |
| 148 | + //Called if a puppet is receiving a duplicate effect. |
| 149 | + public void StackEffect(StatusEffect incomingEffect) |
| 150 | + { |
| 151 | + if (incomingEffect.StatusName != StatusName) |
| 152 | + return; |
| 153 | + if (_stackable) |
| 154 | + { |
| 155 | + IncCount(incomingEffect.Count); |
| 156 | + } |
| 157 | + |
| 158 | + if (_refreshes && incomingEffect.Count >= Count) |
| 159 | + { |
| 160 | + SetCount(incomingEffect.Count); |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments