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.

115 lines
3.2 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. OnUpdate.Invoke();
  67. }
  68. /// <summary>
  69. /// Removes the first instance found in the Block Reader
  70. /// </summary>
  71. /// <param name="item">LogicElement to remove</param>
  72. public void Remove(LogicBlock item)
  73. {
  74. LogicChain.Remove(item);
  75. OnUpdate.Invoke();
  76. }
  77. /// <summary>
  78. /// Addes item to the end of the BlockReader
  79. /// </summary>
  80. /// <param name="item">item to add</param>
  81. public void Add(LogicBlock item)
  82. {
  83. LogicChain.Add(item);
  84. OnUpdate.Invoke();
  85. }
  86. /// <summary>
  87. /// Inserts item at index of the Block Reader
  88. /// </summary>
  89. /// <param name="index">index to insert at</param>
  90. /// <param name="item">item to insert</param>
  91. public void Insert(int index, LogicBlock item)
  92. {
  93. LogicChain.Insert(index, item);
  94. OnUpdate.Invoke();
  95. }
  96. #endregion
  97. }