Advertisement
Guest User

Untitled

a guest
Jan 24th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1. // Target enums
  2. public enum CardTarget {
  3.     None,
  4.     Caster,
  5.     Card,
  6.     Point,
  7.     Area
  8. }
  9. // Effect enums
  10. public enum CardEffect {
  11.     None,
  12.     Fire,
  13.     Water,
  14.     Air,
  15.     Earth
  16. }
  17. // Definitions for behaviour cues that trigger behaviours during state changes.
  18. public enum CardState {
  19.     None,
  20.     Throw,
  21.     Flight,
  22.     Hit,
  23.     Stuck,
  24.     Death
  25. }
  26.  
  27. // For when we need a single value that describes a target/effect pair.
  28. public struct CardBehaviourTuple {
  29.     CardTarget target;
  30.     CardEffect effect;
  31.  
  32.     public CardBehaviourTuple(CardTarget t, CardEffect e) {
  33.         target = t;
  34.         effect = e;
  35.     }
  36. }
  37.    
  38. // Delegate pattern definition for our behaviour delegates.
  39. public delegate void CardBehaviourDelegate(Card card);
  40.  
  41. // The static CardBehaviourManager class for managing all of our behaviour gubbins.
  42. public static class CardBehaviourManager {
  43.     // This is a Dictionary of Dictionaries containing all of the Behaviours we expect to see.
  44.     // The outer dictionary is keyed by behaviour pairs
  45.     // The inner dictionary is keyed by behaviour cues (like on card throw, hit, etc)
  46.     static Dictionary<CardBehaviourTuple, Dictionary<CardState, CardBehaviourDelegate>> Behaviours = new Dictionary<CardBehaviourTuple, Dictionary<CardState, CardBehaviourDelegate>>();
  47.  
  48.     // This method is for setting up a card's behaviours.
  49.     public static void UpdateCard(Card card) {
  50.         CardBehaviourDelegate behaviour = null;
  51.         CardBehaviourTuple tuple = new CardBehaviourTuple(card.Target, card.Effect);
  52.         if (Behaviours.ContainsKey(tuple) && Behaviours[tuple].ContainsKey(card.CurrentState)) {
  53.             behaviour = Behaviours[tuple][card.CurrentState];
  54.         }
  55.         behaviour?.Invoke(card);
  56.     }
  57.  
  58.     public static void PopulateBehaviours() {
  59.         Dictionary<CardState, CardBehaviourDelegate> dict = new Dictionary<CardState, CardBehaviourDelegate>();
  60.         dict.Add(CardState.Throw, (Card c) => {
  61.             // Inline delegate example.
  62.         });
  63.         dict.Add(CardState.Flight, Behaviour_Flight_Basic);
  64.         Behaviours.Add(new CardBehaviourTuple(CardTarget.Card, CardEffect.Fire), dict);
  65.     }
  66.  
  67.     private static void Behaviour_Flight_Basic(Card card) {
  68.         // Predefined delegate method example.
  69.     }
  70. }
  71.  
  72. public class Card : MonoBehaviour {
  73.     public CardState CurrentState { get; set; }
  74.     public CardTarget Target { get; set; }
  75.     public CardEffect Effect { get; set; }
  76.  
  77.     // Life of the card left over.
  78.     public float Life { get; set; }
  79.  
  80.     public Card(CardTarget target, CardEffect effect) {
  81.         // Card setup constructor.
  82.     }
  83.  
  84.     public void Update() {
  85.         // Send this card to the behaviour manager to process the current state's behaviour(s).
  86.         CardBehaviourManager.UpdateCard(this);
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement