infinite_ammo

Conversation

Apr 4th, 2012
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.34 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public enum Class
  6. {
  7.     None,
  8.     Fighter,
  9.     Mage,
  10.     Asshole,
  11.     Max
  12. }
  13.  
  14. public class Conversation : MonoBehaviour
  15. {
  16.     public enum State
  17.     {
  18.         Idle,
  19.         Run,
  20.         Max
  21.     }
  22.  
  23.     public State state;
  24.     public Block currentBlock;
  25.     public string startBlock;
  26.     public string notes;
  27.  
  28.     [System.Serializable]
  29.     public class Block
  30.     {
  31.         public Block()
  32.         {
  33.             isOpen = true;
  34.             name = "newBlock";
  35.             dialog = "";
  36.             branches = new List<Branch>();
  37.         }
  38.  
  39.         public void AddNewBranch()
  40.         {
  41.             branches.Add(new Branch());
  42.         }
  43.  
  44.         public void RemoveBranch(Branch branch)
  45.         {
  46.             branches.Remove(branch);
  47.         }
  48.  
  49.         public string name;
  50.         public string dialog;
  51.         public bool isOpen;
  52.         public List<Branch> branches;
  53.     }
  54.  
  55.     [System.Serializable]
  56.     public class Branch
  57.     {
  58.         public Branch()
  59.         {
  60.             dialog = "";
  61.             gotoBlock = "";
  62.             condition = new Condition();
  63.         }
  64.  
  65.         public string dialog;
  66.         public string gotoBlock;
  67.         public Condition condition;
  68.     }
  69.  
  70.     [System.Serializable]
  71.     public class Condition
  72.     {
  73.         public enum Comparison
  74.         {
  75.             Ignore,
  76.             EqualTo,
  77.             NotEqualTo,
  78.             GreaterThan,
  79.             LessThan
  80.         }
  81.  
  82.         public Condition()
  83.         {
  84.             isOpen = false;
  85.         }
  86.  
  87.         public bool IsMet()
  88.         {
  89.             bool isMet = true;
  90.  
  91.             // evaluate disposition
  92.             switch (dispositionComparison)
  93.             {
  94.                 case Comparison.Ignore:
  95.                 break;
  96.                 case Comparison.EqualTo:
  97.                 break;
  98.                 case Comparison.GreaterThan:
  99.                 break;
  100.                 case Comparison.LessThan:
  101.                 break;
  102.                 case Comparison.NotEqualTo:
  103.                 break;
  104.             }
  105.  
  106.             return isMet;
  107.         }
  108.  
  109.         public bool isOpen;
  110.  
  111.         public int dispositionValue;
  112.         public Comparison dispositionComparison;
  113.  
  114.         public Comparison classComparison;
  115.         public Class classValue;
  116.     }
  117.  
  118.     public List<Block> blocks;
  119.  
  120.     public void AddNewBlock()
  121.     {
  122.         blocks.Add(new Block());
  123.     }
  124.  
  125.     public void RemoveBlock(Block block)
  126.     {
  127.         blocks.Remove(block);
  128.     }
  129.  
  130.     public void MoveBlockUp(Block block)
  131.     {
  132.         int index = blocks.IndexOf(block);
  133.         index --;
  134.         if (index < 0)
  135.         {
  136.             index = 0;
  137.         }
  138.         blocks.Remove(block);
  139.         blocks.Insert(index, block);
  140.     }
  141.  
  142.     public void MoveBlockDown(Block block)
  143.     {
  144.         int index = blocks.IndexOf(block);
  145.         index ++;
  146.         if (index >= blocks.Count)
  147.         {
  148.             index = blocks.Count-1;
  149.         }
  150.         blocks.Remove(block);
  151.         blocks.Insert(index, block);
  152.     }
  153.  
  154.     public void Run()
  155.     {
  156.         state = State.Run;
  157.         if (startBlock == "")
  158.         {
  159.             currentBlock = blocks[0];
  160.         }
  161.         else
  162.         {
  163.             GotoBlockNamed(startBlock);
  164.         }
  165.     }
  166.  
  167.     void Awake()
  168.     {
  169.         state = State.Idle;
  170.     }
  171.  
  172.     void OnGUI()
  173.     {
  174.         switch (state)
  175.         {
  176.             case State.Run:
  177.                 GUILayout.BeginVertical();
  178.                     GUILayout.Label(currentBlock.dialog);
  179.                     foreach (Branch branch in currentBlock.branches)
  180.                     {
  181.                         if (branch.condition.IsMet())
  182.                         {
  183.                             if (GUILayout.Button(branch.dialog))
  184.                             {
  185.                                 GotoBlockNamed(branch.gotoBlock);
  186.                             }
  187.                         }
  188.                     }
  189.                 GUILayout.EndVertical();
  190.             break;
  191.         }
  192.     }
  193.  
  194.     void GotoBlockNamed(string blockName)
  195.     {
  196.         currentBlock = GetBlockNamed(blockName);
  197.         if (currentBlock == null)
  198.         {
  199.             state = State.Idle;
  200.         }
  201.         else
  202.         {
  203.             state = State.Run;
  204.         }
  205.     }
  206.  
  207.     public Block GetBlockNamed(string blockName)
  208.     {
  209.         foreach (Block block in blocks)
  210.         {
  211.             if (block.name == blockName)
  212.             {
  213.                 return block;
  214.             }
  215.         }
  216.         return null;
  217.     }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment