Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Target enums
- public enum CardTarget {
- None,
- Caster,
- Card,
- Point,
- Area
- }
- // Effect enums
- public enum CardEffect {
- None,
- Fire,
- Water,
- Air,
- Earth
- }
- // Definitions for behaviour cues that trigger behaviours during state changes.
- public enum CardState {
- None,
- Throw,
- Flight,
- Hit,
- Stuck,
- Death
- }
- // For when we need a single value that describes a target/effect pair.
- public struct CardBehaviourTuple {
- CardTarget target;
- CardEffect effect;
- public CardBehaviourTuple(CardTarget t, CardEffect e) {
- target = t;
- effect = e;
- }
- }
- // Delegate pattern definition for our behaviour delegates.
- public delegate void CardBehaviourDelegate(Card card);
- // The static CardBehaviourManager class for managing all of our behaviour gubbins.
- public static class CardBehaviourManager {
- // This is a Dictionary of Dictionaries containing all of the Behaviours we expect to see.
- // The outer dictionary is keyed by behaviour pairs
- // The inner dictionary is keyed by behaviour cues (like on card throw, hit, etc)
- static Dictionary<CardBehaviourTuple, Dictionary<CardState, CardBehaviourDelegate>> Behaviours = new Dictionary<CardBehaviourTuple, Dictionary<CardState, CardBehaviourDelegate>>();
- // This method is for setting up a card's behaviours.
- public static void UpdateCard(Card card) {
- CardBehaviourDelegate behaviour = null;
- CardBehaviourTuple tuple = new CardBehaviourTuple(card.Target, card.Effect);
- if (Behaviours.ContainsKey(tuple) && Behaviours[tuple].ContainsKey(card.CurrentState)) {
- behaviour = Behaviours[tuple][card.CurrentState];
- }
- behaviour?.Invoke(card);
- }
- public static void PopulateBehaviours() {
- Dictionary<CardState, CardBehaviourDelegate> dict = new Dictionary<CardState, CardBehaviourDelegate>();
- dict.Add(CardState.Throw, (Card c) => {
- // Inline delegate example.
- });
- dict.Add(CardState.Flight, Behaviour_Flight_Basic);
- Behaviours.Add(new CardBehaviourTuple(CardTarget.Card, CardEffect.Fire), dict);
- }
- private static void Behaviour_Flight_Basic(Card card) {
- // Predefined delegate method example.
- }
- }
- public class Card : MonoBehaviour {
- public CardState CurrentState { get; set; }
- public CardTarget Target { get; set; }
- public CardEffect Effect { get; set; }
- // Life of the card left over.
- public float Life { get; set; }
- public Card(CardTarget target, CardEffect effect) {
- // Card setup constructor.
- }
- public void Update() {
- // Send this card to the behaviour manager to process the current state's behaviour(s).
- CardBehaviourManager.UpdateCard(this);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement