GameActionsComponent.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. using KairoEngine.Core;
  6. namespace KairoEngine.Core.GameActions
  7. {
  8. [HideMonoScript, AddComponentMenu("KairoEngine/Actions Controller")]
  9. public class GameActionsComponent : MonoBehaviour
  10. {
  11. [InlineProperty, HideLabel]
  12. [PropertySpace(0, 3)]
  13. public GameActionTriggersController triggersController = new GameActionTriggersController();
  14. [InlineProperty, HideLabel]
  15. [OnInspectorInit("Initialize")]
  16. public GameActionsController controller;
  17. private void OnEnable()
  18. {
  19. Initialize();
  20. if(triggersController != null) triggersController.OnEnable();
  21. }
  22. private void OnDisable()
  23. {
  24. if(controller != null) controller.Stop();
  25. if(triggersController != null) triggersController.OnDisable();
  26. }
  27. private void Start()
  28. {
  29. Initialize();
  30. if(controller == null)
  31. {
  32. controller = new GameActionsController();
  33. controller.actions = new List<GameAction>();
  34. }
  35. }
  36. private void Update()
  37. {
  38. if(triggersController != null) triggersController.Update();
  39. }
  40. private void Initialize()
  41. {
  42. if(controller == null) return;
  43. controller.showContext = true;
  44. triggersController.actionsController = controller;
  45. if(!controller.context.HasVariable("GameObject"))
  46. {
  47. var variable = new GameActionContextGameObject();
  48. variable.name = "GameObject";
  49. variable.value = this.gameObject;
  50. variable.canEdit = false;
  51. controller.context.AddVariable(variable);
  52. }
  53. }
  54. public void StartGameActionsController()
  55. {
  56. if(controller != null)
  57. {
  58. controller.Start();
  59. }
  60. }
  61. public void ResetGameActionsController()
  62. {
  63. if(controller != null)
  64. {
  65. controller.Restart();
  66. }
  67. }
  68. }
  69. }