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.

227 lines
6.1 KiB

5 years ago
5 years ago
5 years ago
5 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)
  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. RepeatCount++;
  58. yield return player.StartCoroutine(BlockLogic(player, TotalAnimationTime));
  59. }
  60. }
  61. else
  62. {
  63. RepeatCount++;
  64. yield return player.StartCoroutine(BlockLogic(player, TotalAnimationTime));
  65. }
  66. }
  67. /// <summary>
  68. /// Returns the amount of space this logic block takes up
  69. /// </summary>
  70. /// <returns>Int which controlls how much space this takes up</returns>
  71. public virtual int Size()
  72. {
  73. return 1;
  74. }
  75. /// <summary>
  76. /// Where derived callses should implement the logic for their classes
  77. /// </summary>
  78. /// <param name="player">Player which will be affected by the block</param>
  79. /// <returns>returns true if block is finished</returns>
  80. protected abstract IEnumerator BlockLogic(Character player, float animationTime);
  81. /// <summary>
  82. /// Returns the block that the character will endUp on after they use this logic element
  83. /// </summary>
  84. /// <param name="startBlock">block character is on</param>
  85. /// <param name="transform">transform function will be based off</param>
  86. /// <param name="layerMask">layers to ignore</param>
  87. /// <returns>block which character will finish on after performing this function </returns>
  88. public abstract Block GetEndBlock(Block startBlock, Transform transform, LayerMask layerMask);
  89. /// <summary>
  90. /// Resets the block to be ready to used again
  91. /// </summary>
  92. public virtual void Reset()
  93. {
  94. RepeatCount = 0;
  95. }
  96. /// <summary>
  97. /// False if this block needs to be run again
  98. /// </summary>
  99. /// <returns>bool false if block needs to be run again</returns>
  100. public virtual bool isFinished()
  101. {
  102. return (RepeatCount == RepeatAmount);
  103. }
  104. #region Serialisation Functions
  105. /// <summary>
  106. /// Copies data from BlockToken to this Block
  107. /// </summary>
  108. /// <param name="token">Token to Copy</param>
  109. public virtual void CopyToken(BlockToken token)
  110. {
  111. Color = token.Color;
  112. _DisplayName = token._DisplayName;
  113. _WaitUntilFinished = token.WaitUntilFinished;
  114. RepeatAmount = token.RepeatAmount;
  115. name = token.ObjectName;
  116. SpeedMultiplier = token.SpeedMultiplier;
  117. }
  118. /// <summary>
  119. /// Copies Block data to supplied token, if token is null creates new token
  120. /// </summary>
  121. /// <param name="token">token to copy data to</param>
  122. /// <returns></returns>
  123. public virtual BlockToken ToToken(BlockToken token = null)
  124. {
  125. if (token == null)
  126. token = new BlockToken(this);
  127. token.Color = Color;
  128. token._DisplayName = _DisplayName;
  129. token.WaitUntilFinished = WaitUntilFinished;
  130. token.RepeatAmount = RepeatAmount;
  131. token.ObjectName = name;
  132. token.SpeedMultiplier = SpeedMultiplier;
  133. return token;
  134. }
  135. #endregion Serialisation Functions
  136. public bool isSameType(object other)
  137. {
  138. if (Object.ReferenceEquals(null, other))
  139. return false;
  140. if (Object.ReferenceEquals(other.GetType(), other))
  141. return false;
  142. LogicBlock otherLogic = other as LogicBlock;
  143. return !Object.ReferenceEquals(null, otherLogic)
  144. && string.Equals(name, otherLogic.name);
  145. }
  146. public virtual LogicBlock Clone()
  147. {
  148. LogicBlock retVal = Instantiate(this);
  149. retVal.name = this.name;
  150. return retVal;
  151. }
  152. public virtual LogicBlock[] GetAllBlocks()
  153. {
  154. return new LogicBlock[] { this };
  155. }
  156. #endregion Class Functions
  157. }
  158. [System.Serializable]
  159. public class BlockToken
  160. {
  161. public System.Type blockType;
  162. public Color Color;
  163. public string _DisplayName;
  164. public string ObjectName;
  165. public bool WaitUntilFinished;
  166. public int RepeatAmount;
  167. public float SpeedMultiplier;
  168. public BlockToken(LogicBlock block)
  169. {
  170. blockType = block.GetType();
  171. }
  172. public LogicBlock ToLogicBlock()
  173. {
  174. LogicBlock retVal = (LogicBlock)ScriptableObject.CreateInstance(blockType);
  175. //Debug.Log("type: " + retVal.GetType());
  176. retVal.CopyToken(this);
  177. return retVal;
  178. }
  179. }