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.

231 lines
6.3 KiB

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