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.

180 lines
4.8 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. [SerializeField]
  26. [Tooltip("UI Object which is spawned as object")]
  27. public LogicElementUI UIPrefab;
  28. #endregion Inspector Fields
  29. #region ReadOnly Variables
  30. public string DisplayName { get {return (string.IsNullOrEmpty(_DisplayName)) ? name : _DisplayName; } }
  31. #endregion
  32. #region private variables
  33. /// <summary>
  34. /// Amount of times this block has run
  35. /// </summary>
  36. protected int RepeatCount = 0;
  37. #endregion private variables
  38. #region Class Functions
  39. /// <summary>
  40. /// Runs the block
  41. /// </summary>
  42. /// <param name="player">Player which will be affected by the block</param>
  43. /// <returns>returns true if block is finished</returns>
  44. public virtual bool Run(Character player, float animationTime)
  45. {
  46. RepeatCount++;
  47. BlockLogic(player, animationTime);
  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 virtual void OnDoubleClick()
  118. {
  119. }
  120. #endregion Class Functions
  121. }
  122. [System.Serializable]
  123. public class BlockToken
  124. {
  125. public System.Type blockType;
  126. public Color Color;
  127. public string _DisplayName;
  128. public string ObjectName;
  129. public bool WaitUntilFinished;
  130. public int RepeatAmount;
  131. public BlockToken(LogicBlock block)
  132. {
  133. blockType = block.GetType();
  134. }
  135. public LogicBlock ToLogicBlock()
  136. {
  137. LogicBlock retVal = (LogicBlock) ScriptableObject.CreateInstance(blockType);
  138. Debug.Log("type: " + retVal.GetType());
  139. retVal.CopyToken(this);
  140. return retVal;
  141. }
  142. }