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.

208 lines
6.2 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. using System.Collections;
  2. using UnityEngine;
  3. /// <summary>
  4. /// Base class all logic blocks are derived from
  5. /// </summary>
  6. [System.Serializable]
  7. public abstract class LogicBlock : ScriptableObject
  8. {
  9. #region Inspector Fields
  10. [SerializeField]
  11. [Header("UI Settings")]
  12. [Tooltip("Color which will identify this element")]
  13. public Color Color;
  14. [SerializeField]
  15. [Tooltip("Name which will appear in the UI")]
  16. protected string _DisplayName;
  17. [SerializeField]
  18. [Header("Base Settings")]
  19. [Tooltip("Wait until this block is resolved before moving to next")]
  20. protected bool _WaitUntilFinished = false;
  21. [SerializeField]
  22. [Tooltip("Amount of times to run this Block before moving to next")]
  23. protected int RepeatAmount = 1;
  24. [SerializeField]
  25. [Tooltip("Speed this block will be played back at. (Default = 1)")]
  26. protected float SpeedMultiplier = 1;
  27. #endregion Inspector Fields
  28. #region ReadOnly Variables
  29. public string DisplayName { get { return (string.IsNullOrEmpty(_DisplayName)) ? name : _DisplayName; } }
  30. [HideInInspector]
  31. public bool hasBeenRemoved = false;
  32. public virtual bool WaitUntilFinished { get { return _WaitUntilFinished; } }
  33. #endregion
  34. #region private variables
  35. /// <summary>
  36. /// Amount of times this block has run
  37. /// </summary>
  38. protected int RepeatCount = 0;
  39. #endregion private variables
  40. #region Class Functions
  41. /// <summary>
  42. /// Runs the block
  43. /// </summary>
  44. /// <param name="player">Player which will be affected by the block</param>
  45. /// <returns>returns true if block is finished</returns>
  46. public virtual IEnumerator Run(Character player, float animationSpeed,bool useBlockDirection = false)
  47. {
  48. float TotalAnimationTime = animationSpeed / SpeedMultiplier;
  49. if(GlobalVariables.playerMoves == true){
  50. player.DisplayBlock(this);
  51. }
  52. if (WaitUntilFinished)
  53. {
  54. for (int i = 0; i < RepeatAmount; i++)
  55. {
  56. if (player.stuck)
  57. yield break;
  58. RepeatCount++;
  59. yield return player.StartCoroutine(BlockLogic(player, TotalAnimationTime, useBlockDirection));
  60. }
  61. }
  62. else
  63. {
  64. RepeatCount++;
  65. yield return player.StartCoroutine(BlockLogic(player, TotalAnimationTime, useBlockDirection));
  66. }
  67. }
  68. /// <summary>
  69. /// Returns the amount of space this logic block takes up
  70. /// </summary>
  71. /// <returns>Int which controlls how much space this takes up</returns>
  72. public virtual int Size()
  73. {
  74. return 1;
  75. }
  76. /// <summary>
  77. /// Where derived callses should implement the logic for their classes
  78. /// </summary>
  79. /// <param name="player">Player which will be affected by the block</param>
  80. /// <returns>returns true if block is finished</returns>
  81. protected abstract IEnumerator BlockLogic(Character player, float animationTime,bool useBlockDirection = false);
  82. /// <summary>
  83. /// Returns the block that the character will endUp on after they use this logic element
  84. /// </summary>
  85. /// <param name="startBlock">block character is on</param>
  86. /// <param name="transform">transform function will be based off</param>
  87. /// <param name="layerMask">layers to ignore</param>
  88. /// <returns>block which character will finish on after performing this function </returns>
  89. public abstract Block GetEndBlock(Block startBlock, Transform transform, LayerMask layerMask);
  90. /// <summary>
  91. /// Resets the block to be ready to used again
  92. /// </summary>
  93. public virtual void Reset()
  94. {
  95. RepeatCount = 0;
  96. }
  97. /// <summary>
  98. /// False if this block needs to be run again
  99. /// </summary>
  100. /// <returns>bool false if block needs to be run again</returns>
  101. public virtual bool isFinished()
  102. {
  103. return (RepeatCount == RepeatAmount);
  104. }
  105. #region Serialisation Functions
  106. /// <summary>
  107. /// Copies data from BlockToken to this Block
  108. /// </summary>
  109. /// <param name="token">Token to Copy</param>
  110. public virtual void CopyToken(BlockToken token)
  111. {
  112. Color = token.Color;
  113. _DisplayName = token._DisplayName;
  114. _WaitUntilFinished = token.WaitUntilFinished;
  115. RepeatAmount = token.RepeatAmount;
  116. name = token.ObjectName;
  117. SpeedMultiplier = token.SpeedMultiplier;
  118. }
  119. /// <summary>
  120. /// Copies Block data to supplied token, if token is null creates new token
  121. /// </summary>
  122. /// <param name="token">token to copy data to</param>
  123. /// <returns></returns>
  124. public virtual BlockToken ToToken(BlockToken token = null)
  125. {
  126. if (token == null)
  127. token = new BlockToken(this);
  128. token.Color = Color;
  129. token._DisplayName = _DisplayName;
  130. token.WaitUntilFinished = WaitUntilFinished;
  131. token.RepeatAmount = RepeatAmount;
  132. token.ObjectName = name;
  133. token.SpeedMultiplier = SpeedMultiplier;
  134. return token;
  135. }
  136. #endregion Serialisation Functions
  137. public bool isSameType(object other)
  138. {
  139. if (Object.ReferenceEquals(null, other))
  140. return false;
  141. if (Object.ReferenceEquals(other.GetType(), other))
  142. return false;
  143. LogicBlock otherLogic = other as LogicBlock;
  144. return !Object.ReferenceEquals(null, otherLogic)
  145. && string.Equals(name, otherLogic.name);
  146. }
  147. public virtual LogicBlock Clone()
  148. {
  149. LogicBlock retVal = Instantiate(this);
  150. retVal.name = this.name;
  151. return retVal;
  152. }
  153. public virtual LogicBlock[] GetAllBlocks()
  154. {
  155. return new LogicBlock[] { this };
  156. }
  157. #endregion Class Functions
  158. }
  159. [System.Serializable]
  160. public class BlockToken
  161. {
  162. public System.Type blockType;
  163. public Color Color;
  164. public string _DisplayName;
  165. public string ObjectName;
  166. public bool WaitUntilFinished;
  167. public int RepeatAmount;
  168. public float SpeedMultiplier;
  169. public BlockToken(LogicBlock block)
  170. {
  171. blockType = block.GetType();
  172. }
  173. public LogicBlock ToLogicBlock()
  174. {
  175. LogicBlock retVal = (LogicBlock)ScriptableObject.CreateInstance(blockType);
  176. retVal.CopyToken(this);
  177. return retVal;
  178. }
  179. }