ActionController.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. namespace KairoEngine.Core
  6. {
  7. public class ActionController : MonoBehaviour
  8. {
  9. [ShowInInspector]
  10. private List<IAction> actionList;
  11. void Start()
  12. {
  13. actionList = new List<IAction>();
  14. }
  15. void Update()
  16. {
  17. // Remove actions that are done
  18. for (int i = 0; i < actionList.Count; i++)
  19. {
  20. if(actionList[i].IsDone())
  21. {
  22. actionList.RemoveAt(i);
  23. i -= 1;
  24. }
  25. }
  26. // Run Update on all actions
  27. for (int i = 0; i < actionList.Count; i++)
  28. {
  29. actionList[i].Update();
  30. }
  31. }
  32. public void AddAction(IAction action)
  33. {
  34. if(actionList == null || action == null) return;
  35. action.Start();
  36. actionList.Add(action);
  37. }
  38. public bool HasAction(IAction action)
  39. {
  40. if(actionList == null) return false;
  41. for (int i = 0; i < actionList.Count; i++)
  42. {
  43. if(actionList[i].GetType().ToString() == action.GetType().ToString())
  44. {
  45. return true;
  46. }
  47. }
  48. return false;
  49. }
  50. public bool IsActionDone(IAction action)
  51. {
  52. for (int i = 0; i < actionList.Count; i++)
  53. {
  54. if(actionList[i] == action)
  55. {
  56. return actionList[i].IsDone();
  57. }
  58. }
  59. return false;
  60. }
  61. public void ChangeAction(IAction action)
  62. {
  63. for (int i = 0; i < actionList.Count; i++)
  64. {
  65. if(actionList[i].GetType() == action.GetType())
  66. {
  67. actionList[i] = action;
  68. //actionList[i].Start();
  69. }
  70. }
  71. }
  72. public void CancelAction(string actionType)
  73. {
  74. for (int i = 0; i < actionList.Count; i++)
  75. {
  76. if(actionList[i].GetType().ToString() == "kairoEngine." + actionType)
  77. {
  78. actionList[i].End();
  79. }
  80. }
  81. }
  82. public void CancelActions()
  83. {
  84. if(actionList == null) return;
  85. for (int i = 0; i < actionList.Count; i++)
  86. {
  87. actionList[i].End();
  88. }
  89. actionList = new List<IAction>();
  90. }
  91. }
  92. }