Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Game {
- public static event Status.Callback onTurnStart; // Global events.
- public static event Status.Callback onTurnEnd;
- public void Tick() {
- if (onTurnStart != null) { onTurnStart(); }
- // ...
- if (onTurnEnd != null) { onTurnEnd(); }
- }
- }
- public class Actor {
- public List<Status> statuses = new List<Status>(40);
- public event Status.Callback onBlock; // Local event.
- public void Cleanup() { // Call this before disposing of an actor! Explained below in the Poisoned class.
- for (int i = statuses.Count - 1; i >= 0; i--) {
- statuses[i].Cleanup();
- }
- }
- }
- public class Status {
- public delegate void Callback(); // i.e. Tick, Consume, or Run.
- public Actor actor;
- public virtual void Tick() { }
- public virtual void Consume() { }
- public virtual void Run() { }
- public abstract void Cleanup;
- }
- public class Poisoned : Status {
- public int damage;
- public int duration;
- public Poisoned(Actor actor, int damage, int duration) {
- this.actor = actor;
- this.damage = damage;
- this.duration = duration;
- Game.onTurnEnd += Run;
- }
- public override void Consume() {
- actor.PlaySound("relieved_sigh.wav");
- Cleanup();
- }
- public override void Run() {
- duration--;
- actor.TakeDamage(damage);
- if (duration == 0) { Consume(); }
- }
- // The trouble with this pattern of event subscription is that objects can't be
- // garbage collected until they unsubscribe from any events that are still in scope.
- // So before disposing of any actor, you'd have to take care to run through the
- // actor's statuses and call Cleanup to unsubscribe from any global events.
- public override void Cleanup() {
- Game.onTurnEnd -= Run;
- actor.statuses.Remove(this);
- }
- }
- // e.g. To poison an actor for 30 damage over 3 ticks:
- // haplessActor.statuses.Add(new Poisoned(haplessActor, 10, 3));
- public class PaladinBlock : Status {
- public int duration;
- public PaladinBlock(Actor actor, int duration) {
- this.actor = blocker;
- this.duration = duration;
- Game.onTurnEnd += Tick;
- actor.onBlock += Run;
- }
- public override void Tick() {
- duration--;
- if (duration == 0) { Consume(); }
- }
- public override void Consume() {
- // Do whatever happens when the duration runs out.
- Cleanup();
- }
- public override void Run() {
- // Do cool block-y smite-y things.
- }
- public override void Cleanup() {
- actor.onBlock -= Run;
- actor.statuses.Remove(this);
- }
- }
- // e.g. To have a paladin prepare a counter-attack that triggers on block for a tick:
- // somePaladin.statuses.Add(new PaladinBlock(somePaladin, 1));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement