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.

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