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.

151 lines
3.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. /// Resets the block to be ready to used again
  63. /// </summary>
  64. public virtual void Reset()
  65. {
  66. RepeatCount = 0;
  67. }
  68. /// <summary>
  69. /// False if this block needs to be run again
  70. /// </summary>
  71. /// <returns>bool false if block needs to be run again</returns>
  72. public virtual bool isFinished()
  73. {
  74. return (RepeatCount == RepeatAmount);
  75. }
  76. public virtual void CopyToken(BlockToken token)
  77. {
  78. Color = token.Color;
  79. _DisplayName = token._DisplayName;
  80. WaitUntilFinished = token.WaitUntilFinished;
  81. RepeatAmount = token.RepeatAmount;
  82. name = token.ObjectName;
  83. }
  84. public virtual BlockToken ToToken(BlockToken token = null)
  85. {
  86. if (token == null)
  87. token = new BlockToken(this);
  88. token.Color = Color;
  89. token._DisplayName = _DisplayName;
  90. token.WaitUntilFinished = WaitUntilFinished;
  91. token.RepeatAmount = RepeatAmount;
  92. token.ObjectName = name;
  93. return token;
  94. }
  95. #endregion Class Functions
  96. }
  97. [System.Serializable]
  98. public class BlockToken
  99. {
  100. public System.Type blockType;
  101. public Color Color;
  102. public string _DisplayName;
  103. public string ObjectName;
  104. public bool WaitUntilFinished;
  105. public int RepeatAmount;
  106. public BlockToken(LogicBlock block)
  107. {
  108. blockType = block.GetType();
  109. }
  110. public LogicBlock ToLogicBlock()
  111. {
  112. LogicBlock retVal = (LogicBlock) ScriptableObject.CreateInstance(blockType);
  113. Debug.Log("type: " + retVal.GetType());
  114. retVal.CopyToken(this);
  115. return retVal;
  116. }
  117. }