Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
Ce dépôt est archivé. Vous pouvez voir les fichiers et le cloner, mais vous ne pouvez pas pousser ni ouvrir de ticket/demande d'ajout.

195 lignes
5.0KB

  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using coa4u;
  6. /// <summary>
  7. /// The default Actor class for the Action system.
  8. /// </summary>
  9. public class Actor : MonoBehaviour
  10. {
  11. protected Dictionary<string, MethodHolder> methodsCache = new Dictionary<string, MethodHolder>();
  12. protected List<ActionInstant> actions = new List<ActionInstant>();
  13. protected List<ActionInstant> actionsToDelete = new List<ActionInstant>();
  14. protected List<ActionInstant> actionsToAdd = new List<ActionInstant>();
  15. private bool paused = false;
  16. private bool running = false;
  17. protected const float coeff = Mathf.PI / 180;
  18. [HideInInspector]
  19. public Transform transformCached;
  20. [HideInInspector]
  21. public Renderer rendererCached;
  22. /// <summary>
  23. /// This method is called when the script instance is being loaded.
  24. /// </summary>
  25. protected void Awake()
  26. {
  27. transformCached = gameObject.transform;
  28. rendererCached = gameObject.renderer;
  29. }
  30. /// <summary>
  31. /// This method is called at every frame update.
  32. /// </summary>
  33. protected void Update()
  34. {
  35. if (paused)
  36. return;
  37. foreach (ActionInstant action in actions)
  38. {
  39. if (action.running)
  40. action.StepTimer(Time.deltaTime);
  41. if (!action.running)
  42. actionsToDelete.Add(action);
  43. }
  44. }
  45. /// <summary>
  46. /// This method is called after the every frame update.
  47. /// </summary>
  48. protected void LateUpdate()
  49. {
  50. foreach (ActionInstant action in actionsToAdd)
  51. {
  52. if (action.running)
  53. actions.Add(action);
  54. }
  55. foreach (ActionInstant action in actionsToDelete)
  56. {
  57. if (!action.running)
  58. actions.Remove(action);
  59. }
  60. actionsToDelete.Clear();
  61. actionsToAdd.Clear();
  62. running = actions.Count > 0;
  63. }
  64. /// <summary>
  65. /// Attaches and starts the action.
  66. /// </summary>
  67. public void AttachAction(ActionInstant targetAction)
  68. {
  69. targetAction.SetActor(this);
  70. targetAction.Start();
  71. if (targetAction.running)
  72. {
  73. actionsToAdd.Add(targetAction);
  74. }
  75. }
  76. /// <summary>
  77. /// Stopes and removes the given action.
  78. /// </summary>
  79. public void RemoveAction(ActionInstant targetAction)
  80. {
  81. if (targetAction.running)
  82. targetAction.Stop();
  83. }
  84. /// <summary>
  85. /// Stops all running actions for this actor.
  86. /// </summary>
  87. public void StopAllActions()
  88. {
  89. foreach (ActionInstant action in actions)
  90. {
  91. if (action.running &! action.unstopable)
  92. action.Stop();
  93. }
  94. }
  95. /// <summary>
  96. /// Pauses actions for this actor.
  97. /// </summary>
  98. public void PauseActions()
  99. {
  100. paused = true;
  101. }
  102. /// <summary>
  103. /// Unpauses actions for this actor.
  104. /// </summary>
  105. public void UnpauseActions()
  106. {
  107. paused = false;
  108. }
  109. /// <summary>
  110. /// Sets the timescale for current action.
  111. /// </summary>
  112. public void SetTimeScale(float ts)
  113. {
  114. for (int i = 0; i < actions.Count; i++)
  115. {
  116. ActionInstant action = actions[i];
  117. if (action is ActionInterval)
  118. {
  119. ((ActionInterval)action).SetTimeScale(ts);
  120. }
  121. }
  122. }
  123. /// <summary>
  124. /// Adds method to cache to speed-up the ActionSendMessage.
  125. /// </summary>
  126. public void AddMethodToCache(Action method)
  127. {
  128. methodsCache.Add(method.Method.Name, new MethodHolder(method));
  129. }
  130. /// <summary>
  131. /// Adds method to cache to speed-up the ActionSendMessage.
  132. /// </summary>
  133. public void AddMethodToCache<T>(Action<T> method)
  134. {
  135. methodsCache.Add(method.Method.Name, new MethodHolder<T>(method));
  136. }
  137. /// <summary>
  138. /// Adds method from cache.
  139. /// </summary>
  140. public void RemoveMethodFromCache(string key)
  141. {
  142. if (methodsCache.ContainsKey(key))
  143. {
  144. methodsCache.Remove(key);
  145. }
  146. else
  147. {
  148. throw new Exception("Method " + key + " not found in cache.");
  149. }
  150. }
  151. /// <summary>
  152. /// Proxy method to receive messages. WARNING: If the message can be handled by the actor, it will be handled and will not be passed to the Unity3D gameobject.
  153. /// </summary>
  154. public void ReceiveMessage(string key, object param = null, SendMessageOptions options = SendMessageOptions.DontRequireReceiver)
  155. {
  156. if (methodsCache.ContainsKey(key))
  157. {
  158. if (param == null)
  159. methodsCache[key].Run();
  160. else
  161. methodsCache[key].Run(param);
  162. }
  163. else
  164. {
  165. gameObject.SendMessage(key, param, options);
  166. }
  167. }
  168. /// <summary>
  169. /// Shows if the actor has any running actions.
  170. /// </summary>
  171. public bool hasRunningActions
  172. {
  173. get
  174. {
  175. return running;
  176. }
  177. }
  178. }