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.

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