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.

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