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.

124 lines
3.4 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. [System.Serializable]
  8. public class BlockReader
  9. {
  10. #region Inspector Variables
  11. [SerializeField]
  12. [Tooltip("List of LogicBlocks which will affected this character")]
  13. public List<LogicBlock> LogicChain = new List<LogicBlock>();
  14. #endregion Inspector Variables
  15. #region Read-only Variables
  16. /// <summary>
  17. /// returns true if all LogicBlocks in LogicChain have been completed
  18. /// </summary>
  19. public bool Finished { get { return currentBlockIndex >= LogicChain.Count; } }
  20. /// <summary>
  21. /// Fired whenever the logic chain is updated, useful for UI
  22. /// </summary>
  23. public event System.Action OnUpdate;
  24. /// <summary>
  25. /// Gets the current block in the logic chain
  26. /// </summary>
  27. public LogicBlock CurrentBlock { get { return LogicChain[currentBlockIndex]; } }
  28. #endregion Read-only Variables
  29. #region Private Variables
  30. private int currentBlockIndex;//Block currently on
  31. #endregion Private Variables
  32. #region Class Implementation
  33. /// <summary>
  34. /// Resets Block reader;
  35. /// </summary>
  36. public void Reset()
  37. {
  38. currentBlockIndex = 0;
  39. }
  40. /// <summary>
  41. /// Reads the current block in the logic chain
  42. /// </summary>
  43. /// <param name="character">Character to control</param>
  44. /// <returns>Returns false if other readers should wait for this one to run again</returns>
  45. public bool Read(Character character,float animationTime)
  46. {
  47. //return that this is done if no more blocks left in chain
  48. if (LogicChain.Count <= currentBlockIndex)
  49. return true;
  50. //get current Block
  51. LogicBlock currentBlock = LogicChain[currentBlockIndex];
  52. //apply blocks to character
  53. currentBlock.Run(character, animationTime);
  54. //if the block is finished reset it and move on to next one
  55. //(it might not be finished if it is a for loop or something)
  56. if (currentBlock.isFinished())
  57. {
  58. currentBlock.Reset();
  59. currentBlockIndex++;
  60. }
  61. //Should other readers wait for this one
  62. return (currentBlock.isFinished() || !currentBlock.WaitUntilFinished);
  63. }
  64. /// <summary>
  65. /// Clears all elements out of the Block Reader
  66. /// </summary>
  67. public void Clear()
  68. {
  69. LogicChain.Clear();
  70. if (OnUpdate != null)
  71. OnUpdate.Invoke();
  72. }
  73. /// <summary>
  74. /// Removes the first instance found in the Block Reader
  75. /// </summary>
  76. /// <param name="item">LogicElement to remove</param>
  77. public void Remove(LogicBlock item)
  78. {
  79. LogicChain.Remove(item);
  80. if (OnUpdate != null)
  81. OnUpdate.Invoke();
  82. }
  83. /// <summary>
  84. /// Addes item to the end of the BlockReader
  85. /// </summary>
  86. /// <param name="item">item to add</param>
  87. public void Add(LogicBlock item)
  88. {
  89. LogicChain.Add(item);
  90. if (OnUpdate != null)
  91. OnUpdate.Invoke();
  92. }
  93. /// <summary>
  94. /// Inserts item at index of the Block Reader
  95. /// </summary>
  96. /// <param name="index">index to insert at</param>
  97. /// <param name="item">item to insert</param>
  98. public void Insert(int index, LogicBlock item)
  99. {
  100. LogicChain.Insert(index, item);
  101. if (OnUpdate != null)
  102. OnUpdate.Invoke();
  103. }
  104. #endregion
  105. }