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.

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