You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.0 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// Class which reads a a list of logic blocks and applies to a character
  6. /// </summary>
  7. public class BlockReader : MonoBehaviour
  8. {
  9. #region Inspector Variables
  10. [SerializeField]
  11. [Tooltip("Character to be controllered by the Logic Chain")]
  12. protected Character character;
  13. [SerializeField]
  14. [Tooltip("List of LogicBlocks which will affected this character")]
  15. protected List<LogicBlock> LogicChain;
  16. #endregion Inspector Variables
  17. #region Read-only Variables
  18. /// <summary>
  19. /// returns true if all LogicBlocks in LogicChain have been completed
  20. /// </summary>
  21. public bool Finished { get { return currentBlockIndex >= LogicChain.Count; } }
  22. #endregion Read-only Variables
  23. #region Private Variables
  24. private int currentBlockIndex;//Block currently on
  25. #endregion Private Variables
  26. #region Class Implementation
  27. /// <summary>
  28. /// Resets Block reader;
  29. /// </summary>
  30. public void Reset()
  31. {
  32. currentBlockIndex = 0;
  33. }
  34. /// <summary>
  35. /// Reads the current block in the logic chain
  36. /// </summary>
  37. /// <returns>Returns false if other readers should wait for this one to run again</returns>
  38. [ContextMenu("Do Read")]
  39. public bool Read()
  40. {
  41. //return that this is done if no more blocks left in chain
  42. if (LogicChain.Count <= currentBlockIndex)
  43. return true;
  44. //get current Block
  45. LogicBlock currentBlock = LogicChain[currentBlockIndex];
  46. //apply blocks to character
  47. currentBlock.Run(character);
  48. //if the block is finished reset it and move on to next one
  49. //(it might not be finished if it is a for loop or something)
  50. if (currentBlock.isFinished())
  51. {
  52. currentBlock.Reset();
  53. currentBlockIndex++;
  54. }
  55. //Should other readers wait for this one
  56. return (currentBlock.isFinished() || !currentBlock.WaitUntilFinished);
  57. }
  58. #endregion
  59. }