You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

168 lines
4.3KB

  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 ActionInstant action;
  13. private bool paused = false;
  14. protected Vector3 angles1prev = Vector3.zero;
  15. protected Vector3 angles2prev = Vector3.zero;
  16. protected const float coeff = Mathf.PI / 180;
  17. protected Vector3[] initialState;
  18. public Transform transformCached;
  19. public Renderer rendererCached;
  20. public Mesh meshCached;
  21. public Vector3 skewAngles1;
  22. public Vector3 skewAngles2;
  23. /// <summary>
  24. /// This method is called when the script instance is being loaded.
  25. /// </summary>
  26. protected void Awake()
  27. {
  28. transformCached = gameObject.transform;
  29. rendererCached = gameObject.renderer;
  30. meshCached = gameObject.GetComponent<MeshFilter>().mesh;
  31. initialState = meshCached.vertices;
  32. }
  33. /// <summary>
  34. /// This method is called at every frame update.
  35. /// </summary>
  36. protected void Update()
  37. {
  38. if (paused || action == null)
  39. return;
  40. if (action.running)
  41. action.step(Time.deltaTime);
  42. if (skewAngles1 != angles1prev || skewAngles2 != angles2prev)
  43. updateSkew();
  44. }
  45. /// <summary>
  46. /// Updates the skew for the mesh.
  47. /// </summary>
  48. void updateSkew()
  49. {
  50. Matrix4x4 m = Matrix4x4.zero;
  51. m[0, 0] = 1;
  52. m[1, 1] = 1;
  53. m[2, 2] = 1;
  54. m[3, 3] = 1;
  55. m[0, 1] = Mathf.Tan(skewAngles1.x * coeff); // skewing in xy
  56. m[0, 2] = Mathf.Tan(skewAngles2.x * coeff); // skewing in xz
  57. m[1, 0] = Mathf.Tan(skewAngles1.y * coeff); // skewing in yx
  58. m[1, 2] = Mathf.Tan(skewAngles2.y * coeff); // skewing in yz
  59. m[2, 0] = Mathf.Tan(skewAngles1.z * coeff); // skewing in zx
  60. m[2, 1] = Mathf.Tan(skewAngles2.z * coeff); // skewing in zy
  61. Vector3[] verts = new Vector3[initialState.Length];
  62. int i = 0;
  63. while (i < verts.Length)
  64. {
  65. verts[i] = m.MultiplyPoint3x4(initialState[i]);
  66. i++;
  67. }
  68. meshCached.vertices = verts;
  69. angles1prev = skewAngles1;
  70. angles2prev = skewAngles2;
  71. }
  72. /// <summary>
  73. /// Attaches and starts the action.
  74. /// </summary>
  75. public void AttachAction(ActionInstant tgtAction)
  76. {
  77. if (action != null)
  78. {
  79. action.stop();
  80. }
  81. action = tgtAction;
  82. action.setActor(this);
  83. action.start();
  84. }
  85. /// <summary>
  86. /// Stops all running actions for this actor.
  87. /// </summary>
  88. public void StopAction()
  89. {
  90. if (action == null)
  91. return;
  92. if (action.running)
  93. action.stop();
  94. action = null;
  95. }
  96. /// <summary>
  97. /// Pauses actions for this actor.
  98. /// </summary>
  99. public void PauseAction()
  100. {
  101. paused = true;
  102. }
  103. /// <summary>
  104. /// Unpauses actions for this actor.
  105. /// </summary>
  106. public void UnpauseAction()
  107. {
  108. paused = false;
  109. }
  110. /// <summary>
  111. /// Sets the timescale for current action.
  112. /// </summary>
  113. public void SetTimeScale(float ts)
  114. {
  115. if (action is ActionInterval)
  116. {
  117. ((ActionInterval)action).setTimeScale(ts);
  118. }
  119. }
  120. /// <summary>
  121. /// Adds method to cache to speed-up
  122. /// </summary>
  123. public void AddMethodToCache(MethodHolder method)
  124. {
  125. methodsCache.Add(method.name, method);
  126. }
  127. public void RemoveMethodFromCache(string key)
  128. {
  129. if (methodsCache.ContainsKey(key))
  130. {
  131. methodsCache.Remove(key);
  132. }
  133. else
  134. {
  135. throw new Exception("Method " + key + " not found in cache.");
  136. }
  137. }
  138. public void ReceiveMessage(string key, object param = null, SendMessageOptions options = SendMessageOptions.DontRequireReceiver)
  139. {
  140. if (methodsCache.ContainsKey(key))
  141. {
  142. if (param == null)
  143. methodsCache[key].run();
  144. else
  145. methodsCache[key].run(param);
  146. }
  147. else
  148. {
  149. gameObject.SendMessage(key, param, options);
  150. }
  151. }
  152. }