WaitGameAction.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Collections;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Sirenix.OdinInspector;
  6. using KairoEngine.Core;
  7. namespace KairoEngine.Core.GameActions
  8. {
  9. [System.Serializable, HideReferenceObjectPicker]
  10. public class WaitGameAction : GameActionBase
  11. {
  12. #region DefaultVariables
  13. public override string name { get => $"Wait {time}s"; }
  14. public override GameActionsController controller {
  15. get => _controller;
  16. set
  17. {
  18. _controller = value;
  19. typeName = "WaitGameAction";
  20. }
  21. }
  22. public override string GetTypeName() => "WaitGameAction";
  23. public override string GetActionName() => "Core/Wait";
  24. #endregion
  25. #region ActionVariables
  26. private string GetIconPath
  27. {
  28. get
  29. {
  30. if(_iconPath == "") _iconPath = Utilities.FindFileByName("WaitGameActionIcon.png");
  31. return _iconPath;
  32. }
  33. }
  34. [SerializeField, HideInInspector] private string _iconPath = "";
  35. [IconFoldoutGroup("@name", "@GetIconPath")] public float time = 1f;
  36. [IconFoldoutGroup("@name", "@GetIconPath")] public bool realtime = true;
  37. private float elapsedTime = 0f;
  38. #endregion
  39. public WaitGameAction(GameActionsController controller) : base(controller)
  40. {
  41. this.controller = controller;
  42. className = this.GetType().AssemblyQualifiedName;
  43. }
  44. #region Flow
  45. public override void Start()
  46. {
  47. elapsedTime = 0f;
  48. _done = false;
  49. _started = true;
  50. if(realtime)
  51. {
  52. Timer.ExecuteRealTime(time * 1000, () => {
  53. _done = true;
  54. });
  55. // Timer.StartRealtime(time, () => {
  56. // _done = true;
  57. // });
  58. }
  59. }
  60. public override void Update()
  61. {
  62. if(!realtime)
  63. {
  64. elapsedTime += Time.deltaTime;
  65. if(elapsedTime > time) _done = true;
  66. }
  67. }
  68. public override void Restart()
  69. {
  70. _done = false;
  71. _started = false;
  72. elapsedTime = 0;
  73. }
  74. #endregion
  75. #region Utilities
  76. private WaitGameAction Duplicate(GameActionsController controller = null)
  77. {
  78. WaitGameAction action = new WaitGameAction(controller == null ? this.controller : controller);
  79. action.controller = controller;
  80. action.time = time;
  81. return action;
  82. }
  83. #endregion
  84. #region Serialization
  85. public static WaitGameAction JSONToWaitGameAction(string data)
  86. {
  87. return JsonUtility.FromJson<WaitGameAction>(data);
  88. }
  89. #endregion
  90. }
  91. }