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.

134 lines
3.9 KiB

5 years ago
5 years ago
  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 IEnumerator Read(Character character, float speedMultiplier)
  50. {
  51. LogicBlock currentBlock = LogicChain[currentBlockIndex];
  52. //If the character has become stuck, they forfeit their remaining moves - we skip directly to their end of their chain
  53. if (character.stuck == true)
  54. {
  55. currentBlockIndex = LogicChain.Count;
  56. currentBlock.Reset();
  57. yield break;
  58. }
  59. //apply blocks to character
  60. yield return character.StartCoroutine(currentBlock.Run(character, speedMultiplier));
  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. if (currentBlock.isFinished())
  64. {
  65. currentBlock.Reset();
  66. currentBlockIndex++;
  67. }
  68. //Should other readers wait for this one
  69. //return (runAgainNot || !currentBlock.WaitUntilFinished);
  70. }
  71. /// <summary>
  72. /// Clears all elements out of the Block Reader
  73. /// </summary>
  74. public void Clear()
  75. {
  76. LogicChain.Clear();
  77. if (OnUpdate != null)
  78. OnUpdate.Invoke();
  79. }
  80. /// <summary>
  81. /// Removes the first instance found in the Block Reader
  82. /// </summary>
  83. /// <param name="item">LogicElement to remove</param>
  84. public void Remove(LogicBlock item)
  85. {
  86. LogicChain.Remove(item);
  87. if (OnUpdate != null)
  88. OnUpdate.Invoke();
  89. }
  90. /// <summary>
  91. /// Addes item to the end of the BlockReader
  92. /// </summary>
  93. /// <param name="item">item to add</param>
  94. public void Add(LogicBlock item)
  95. {
  96. LogicChain.Add(item);
  97. if (OnUpdate != null)
  98. OnUpdate.Invoke();
  99. }
  100. /// <summary>
  101. /// Inserts item at index of the Block Reader
  102. /// </summary>
  103. /// <param name="index">index to insert at</param>
  104. /// <param name="item">item to insert</param>
  105. public void Insert(int index, LogicBlock item)
  106. {
  107. LogicChain.Insert(index, item);
  108. if (OnUpdate != null)
  109. OnUpdate.Invoke();
  110. }
  111. #endregion
  112. }