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.

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