ExitApplicationGameAction.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 ExitApplicationGameAction : GameActionBase
  11. {
  12. #region DefaultVariables
  13. public override string name { get => $"Exit application in {time}s"; }
  14. public override GameActionsController controller {
  15. get => _controller;
  16. set
  17. {
  18. _controller = value;
  19. typeName = "ExitApplicationGameAction";
  20. }
  21. }
  22. public override string GetTypeName() => "ExitApplicationGameAction";
  23. public override string GetActionName() => "Core/Exit Application";
  24. #endregion
  25. #region ActionVariables
  26. [FoldoutGroup("@name")] public float time = 1f;
  27. private float elapsedTime = 0f;
  28. #endregion
  29. public ExitApplicationGameAction(GameActionsController controller) : base(controller)
  30. {
  31. this.controller = controller;
  32. className = this.GetType().AssemblyQualifiedName;
  33. }
  34. #region Flow
  35. public override void Start()
  36. {
  37. elapsedTime = 0f;
  38. _done = false;
  39. _started = true;
  40. }
  41. public override void Update()
  42. {
  43. elapsedTime += Time.deltaTime;
  44. if(elapsedTime > time)
  45. {
  46. _done = true;
  47. #if UNITY_EDITOR
  48. UnityEditor.EditorApplication.isPlaying = false;
  49. #else
  50. Application.Quit();
  51. #endif
  52. }
  53. }
  54. public override void Restart()
  55. {
  56. _done = false;
  57. _started = false;
  58. elapsedTime = 0;
  59. }
  60. #endregion
  61. #region Utilities
  62. private ExitApplicationGameAction Duplicate(GameActionsController controller = null)
  63. {
  64. ExitApplicationGameAction action = new ExitApplicationGameAction(controller == null ? this.controller : controller);
  65. action.controller = controller;
  66. action.time = time;
  67. return action;
  68. }
  69. #endregion
  70. #region Serialization
  71. public static ExitApplicationGameAction JSONToExitApplicationGameAction(string data)
  72. {
  73. return JsonUtility.FromJson<ExitApplicationGameAction>(data);
  74. }
  75. #endregion
  76. }
  77. }