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.

160 lines
4.2 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. public virtual void CopyToken(BlockToken token)
  85. {
  86. Color = token.Color;
  87. _DisplayName = token._DisplayName;
  88. WaitUntilFinished = token.WaitUntilFinished;
  89. RepeatAmount = token.RepeatAmount;
  90. name = token.ObjectName;
  91. }
  92. public virtual BlockToken ToToken(BlockToken token = null)
  93. {
  94. if (token == null)
  95. token = new BlockToken(this);
  96. token.Color = Color;
  97. token._DisplayName = _DisplayName;
  98. token.WaitUntilFinished = WaitUntilFinished;
  99. token.RepeatAmount = RepeatAmount;
  100. token.ObjectName = name;
  101. return token;
  102. }
  103. #endregion Class Functions
  104. }
  105. [System.Serializable]
  106. public class BlockToken
  107. {
  108. public System.Type blockType;
  109. public Color Color;
  110. public string _DisplayName;
  111. public string ObjectName;
  112. public bool WaitUntilFinished;
  113. public int RepeatAmount;
  114. public BlockToken(LogicBlock block)
  115. {
  116. blockType = block.GetType();
  117. }
  118. public LogicBlock ToLogicBlock()
  119. {
  120. LogicBlock retVal = (LogicBlock) ScriptableObject.CreateInstance(blockType);
  121. Debug.Log("type: " + retVal.GetType());
  122. retVal.CopyToken(this);
  123. return retVal;
  124. }
  125. }