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.

179 lines
4.9KB

  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace coa4u
  5. {
  6. public enum Axises
  7. {
  8. none,
  9. x,
  10. y,
  11. z,
  12. xy,
  13. xz,
  14. yz,
  15. xyz
  16. }
  17. /// <summary>
  18. /// Basic action class for subclassing. To inherit interval actions, consider using the ActionInterval as the base class.
  19. /// </summary>
  20. public class ActionInstant
  21. {
  22. public Axises locks = Axises.none;
  23. protected Actor target;
  24. protected Transform transform;
  25. protected Renderer renderer;
  26. protected bool is2d = false;
  27. private float durationValue = 0;
  28. private float dtrValue = 0;
  29. private bool isRunning = false;
  30. public ActionInstant()
  31. {
  32. }
  33. /// <summary>
  34. /// Returns a copy of the action.
  35. /// </summary>
  36. public virtual ActionInstant Clone()
  37. {
  38. return new ActionInstant();
  39. }
  40. /// <summary>
  41. /// Returns the reversed version of the action, if it is possible.
  42. /// </summary>
  43. public virtual ActionInstant Reverse()
  44. {
  45. throw new Exception("Can reverse only the reversable interval actions");
  46. }
  47. /// <summary>
  48. /// This method is called at the action start. Usable for instant and interval actions.
  49. /// </summary>
  50. public virtual void Start()
  51. {
  52. if (target == null)
  53. throw new Exception("Can start the action only after it's atached to the actor");
  54. transform = target.transformCached;
  55. renderer = target.rendererCached;
  56. }
  57. /// <summary>
  58. /// This method is called at every frame update. Not usable for instant actions.
  59. /// </summary>
  60. public virtual void StepTimer(float dt)
  61. {
  62. if (target == null)
  63. throw new Exception("Can update the action only after it's atached to the actor");
  64. }
  65. /// <summary>
  66. /// This method is called after the interval action is stopped. Not usable for instant actions.
  67. /// </summary>
  68. public virtual void Stop()
  69. {
  70. if (target == null)
  71. throw new Exception("Can stop the action only after it's atached to the actor");
  72. }
  73. /// <summary>
  74. /// This method sets the actor for the action.
  75. /// </summary>
  76. public void SetActor(Actor actor)
  77. {
  78. target = actor;
  79. }
  80. /// <summary>
  81. /// This method modifies the given vector to keep the locked axises untouched.
  82. /// </summary>
  83. protected void LockAxises(ref Vector3 point)
  84. {
  85. {
  86. if (target == null)
  87. throw new Exception("Can calculate axises only after the action is atached to the actor");
  88. }
  89. switch (locks)
  90. {
  91. case Axises.x:
  92. point.x = transform.position.x;
  93. break;
  94. case Axises.y:
  95. point.y = transform.position.y;
  96. break;
  97. case Axises.z:
  98. point.z = transform.position.z;
  99. break;
  100. case Axises.xy:
  101. point.x = transform.position.x;
  102. point.y = transform.position.y;
  103. break;
  104. case Axises.xz:
  105. point.x = transform.position.x;
  106. point.z = transform.position.z;
  107. break;
  108. case Axises.yz:
  109. point.y = transform.position.y;
  110. point.z = transform.position.z;
  111. break;
  112. case Axises.xyz:
  113. point.x = transform.position.x;
  114. point.y = transform.position.y;
  115. point.z = transform.position.z;
  116. break;
  117. }
  118. }
  119. /// <summary>
  120. /// Readonly property. Shows the remaining time of the action.
  121. /// </summary>
  122. public float duration
  123. {
  124. get
  125. {
  126. return durationValue;
  127. }
  128. protected set
  129. {
  130. durationValue = value;
  131. }
  132. }
  133. /// <summary>
  134. /// Readonly property. Shows if the action is running or not.
  135. /// </summary>
  136. public bool running
  137. {
  138. get
  139. {
  140. return isRunning;
  141. }
  142. protected set
  143. {
  144. isRunning = value;
  145. }
  146. }
  147. /// <summary>
  148. /// Readonly property. Contains the remaining tick time after action finished.
  149. /// </summary>
  150. public float dtr
  151. {
  152. get
  153. {
  154. return dtrValue;
  155. }
  156. protected set
  157. {
  158. dtrValue = value;
  159. }
  160. }
  161. }
  162. }