WaitGameAction.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. [IconFoldoutGroup("@name", "Assets/Plugins/KairoEngine/Core/Editor/Icons/WaitGameActionIcon.png")] public float time = 1f;
  27. [IconFoldoutGroup("@name", "Assets/Plugins/KairoEngine/Core/Editor/Icons/WaitGameActionIcon.png")] public bool realtime = true;
  28. private float elapsedTime = 0f;
  29. #endregion
  30. public WaitGameAction(GameActionsController controller) : base(controller)
  31. {
  32. this.controller = controller;
  33. className = this.GetType().AssemblyQualifiedName;
  34. }
  35. #region Flow
  36. public override void Start()
  37. {
  38. elapsedTime = 0f;
  39. _done = false;
  40. _started = true;
  41. if(realtime)
  42. {
  43. Timer.ExecuteRealTime(time * 1000, () => {
  44. _done = true;
  45. });
  46. // Timer.StartRealtime(time, () => {
  47. // _done = true;
  48. // });
  49. }
  50. }
  51. public override void Update()
  52. {
  53. if(!realtime)
  54. {
  55. elapsedTime += Time.deltaTime;
  56. if(elapsedTime > time) _done = true;
  57. }
  58. }
  59. public override void Restart()
  60. {
  61. _done = false;
  62. _started = false;
  63. elapsedTime = 0;
  64. }
  65. #endregion
  66. #region Utilities
  67. private WaitGameAction Duplicate(GameActionsController controller = null)
  68. {
  69. WaitGameAction action = new WaitGameAction(controller == null ? this.controller : controller);
  70. action.controller = controller;
  71. action.time = time;
  72. return action;
  73. }
  74. #endregion
  75. #region Serialization
  76. public static WaitGameAction JSONToWaitGameAction(string data)
  77. {
  78. return JsonUtility.FromJson<WaitGameAction>(data);
  79. }
  80. #endregion
  81. }
  82. }