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.

228 lines
6.2 KiB

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