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.

217 lines
5.9 KiB

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