Advertisement
lunoland

Tangledeep Statuses

Sep 23rd, 2019
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.58 KB | None | 0 0
  1. public class Game {
  2.    
  3.     public static event Status.Callback onTurnStart; // Global events.
  4.     public static event Status.Callback onTurnEnd;
  5.  
  6.     public void Tick() {
  7.         if (onTurnStart != null) { onTurnStart(); }
  8.         // ...
  9.         if (onTurnEnd != null) { onTurnEnd(); }
  10.     }
  11. }
  12.  
  13. public class Actor {
  14.     public List<Status> statuses = new List<Status>(40);
  15.     public event Status.Callback onBlock; // Local event.
  16.  
  17.     public void Cleanup() { // Call this before disposing of an actor! Explained below in the Poisoned class.
  18.         for (int i = statuses.Count - 1; i >= 0; i--) {
  19.             statuses[i].Cleanup();
  20.         }
  21.     }
  22. }
  23.  
  24. public class Status {
  25.     public delegate void Callback(); // i.e. Tick, Consume, or Run.
  26.    
  27.     public Actor actor;
  28.  
  29.     public virtual void Tick() { }
  30.     public virtual void Consume() { }
  31.     public virtual void Run() { }
  32.     public abstract void Cleanup;
  33. }
  34.  
  35. public class Poisoned : Status {
  36.     public int damage;
  37.     public int duration;
  38.  
  39.     public Poisoned(Actor actor, int damage, int duration) {
  40.         this.actor = actor;
  41.         this.damage = damage;
  42.         this.duration = duration;
  43.    
  44.         Game.onTurnEnd += Run;
  45.     }
  46.  
  47.     public override void Consume() {
  48.         actor.PlaySound("relieved_sigh.wav");
  49.         Cleanup();
  50.     }
  51.  
  52.     public override void Run() {
  53.         duration--;
  54.         actor.TakeDamage(damage);
  55.         if (duration == 0) { Consume(); }
  56.     }
  57.    
  58.     // The trouble with this pattern of event subscription is that objects can't be
  59.     // garbage collected until they unsubscribe from any events that are still in scope.
  60.     // So before disposing of any actor, you'd have to take care to run through the
  61.     // actor's statuses and call Cleanup to unsubscribe from any global events.
  62.     public override void Cleanup() {
  63.         Game.onTurnEnd -= Run;
  64.         actor.statuses.Remove(this);
  65.     }
  66. }
  67. // e.g. To poison an actor for 30 damage over 3 ticks:
  68. // haplessActor.statuses.Add(new Poisoned(haplessActor, 10, 3));
  69.  
  70. public class PaladinBlock : Status {
  71.     public int duration;
  72.  
  73.     public PaladinBlock(Actor actor, int duration) {
  74.         this.actor = blocker;
  75.         this.duration = duration;
  76.    
  77.         Game.onTurnEnd += Tick;
  78.         actor.onBlock += Run;
  79.     }
  80.  
  81.     public override void Tick() {
  82.         duration--;
  83.         if (duration == 0) { Consume(); }
  84.     }
  85.  
  86.     public override void Consume() {
  87.         // Do whatever happens when the duration runs out.
  88.         Cleanup();
  89.     }
  90.    
  91.     public override void Run() {
  92.         // Do cool block-y smite-y things.
  93.     }
  94.    
  95.     public override void Cleanup() {
  96.         actor.onBlock -= Run;
  97.         actor.statuses.Remove(this);
  98.     }
  99. }
  100. // e.g. To have a paladin prepare a counter-attack that triggers on block for a tick:
  101. // somePaladin.statuses.Add(new PaladinBlock(somePaladin, 1));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement