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.

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