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.

229 lines
6.2 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. public Sprite icon;
  16. [SerializeField]
  17. [Tooltip("Name which will appear in the UI")]
  18. protected string _DisplayName;
  19. [SerializeField]
  20. [Header("Base Settings")]
  21. [Tooltip("Wait until this block is resolved before moving to next")]
  22. protected bool _WaitUntilFinished = false;
  23. [SerializeField]
  24. [Tooltip("Amount of times to run this Block before moving to next")]
  25. protected int RepeatAmount = 1;
  26. [SerializeField]
  27. [Tooltip("Speed this block will be played back at. (Default = 1)")]
  28. protected float SpeedMultiplier = 1;
  29. #endregion Inspector Fields
  30. #region ReadOnly Variables
  31. public string DisplayName { get { return (string.IsNullOrEmpty(_DisplayName)) ? name : _DisplayName; } }
  32. [HideInInspector]
  33. public bool hasBeenRemoved = false;
  34. public virtual bool WaitUntilFinished { get { return _WaitUntilFinished; } }
  35. #endregion
  36. #region private variables
  37. /// <summary>
  38. /// Amount of times this block has run
  39. /// </summary>
  40. protected int RepeatCount = 0;
  41. #endregion private variables
  42. #region Class Functions
  43. /// <summary>
  44. /// Runs the block
  45. /// </summary>
  46. /// <param name="player">Player which will be affected by the block</param>
  47. /// <returns>returns true if block is finished</returns>
  48. public virtual IEnumerator Run(Character player, float animationSpeed)
  49. {
  50. float TotalAnimationTime = animationSpeed / SpeedMultiplier;
  51. if(GlobalVariables.playerMoves == true){
  52. player.DisplayBlock(this);
  53. }
  54. if (WaitUntilFinished)
  55. {
  56. for (int i = 0; i < RepeatAmount; i++)
  57. {
  58. RepeatCount++;
  59. yield return player.StartCoroutine(BlockLogic(player, TotalAnimationTime));
  60. }
  61. }
  62. else
  63. {
  64. RepeatCount++;
  65. yield return player.StartCoroutine(BlockLogic(player, TotalAnimationTime));
  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);
  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. //Debug.Log("type: " + retVal.GetType());
  177. retVal.CopyToken(this);
  178. return retVal;
  179. }
  180. }