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.

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