Tween.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /************************************************************************
  2. * Tween.js
  3. ************************************************************************
  4. * Copyright (c) 2021 Pedro Tonini Rosenberg Schneider.
  5. *
  6. * This file is part of Pandora.
  7. *
  8. * Pandora is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * Pandora is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with Pandora. If not, see <https://www.gnu.org/licenses/>.
  20. *************************************************************************/
  21. class TweenData
  22. {
  23. constructor(target, property, propertyType, initVal, finalVal, duration, transType, easeType, delay)
  24. {
  25. this.target = target;
  26. this.property = property;
  27. this.propertyType = propertyType;
  28. switch (this.propertyType)
  29. {
  30. case PROPERTY_TYPE.COLOR:
  31. this.initVal = new Color(initVal.r, initVal.g, initVal.b, initVal.a);
  32. this.finalVal = new Color(finalVal.r, finalVal.g, finalVal.b, finalVal.a);
  33. break;
  34. case PROPERTY_TYPE.VECTOR2:
  35. this.initVal = new Vector2(initVal.x, initVal.y);
  36. this.finalVal = new Vector2(finalVal.x, finalVal.y);
  37. break;
  38. case PROPERTY_TYPE.NUMBER:
  39. this.initVal = initVal;
  40. this.finalVal = finalVal;
  41. break;
  42. }
  43. this.duration = duration;
  44. this.transType = transType;
  45. this.easeType = easeType;
  46. this.t = -delay;
  47. this.playing = false;
  48. this.done = false;
  49. this.p = [];
  50. switch (this.propertyType)
  51. {
  52. case PROPERTY_TYPE.COLOR:
  53. this.p.push("r");
  54. this.p.push("g");
  55. this.p.push("b");
  56. break;
  57. case PROPERTY_TYPE.VECTOR2:
  58. this.p.push("x");
  59. this.p.push("y");
  60. break;
  61. case PROPERTY_TYPE.NUMBER:
  62. break;
  63. }
  64. this.trans = "";
  65. switch (this.transType)
  66. {
  67. case TRANS_TYPE.LINEAR:
  68. this.trans = "Linear";
  69. break;
  70. case TRANS_TYPE.QUAD:
  71. this.trans = "Quad";
  72. break;
  73. case TRANS_TYPE.CUBIC:
  74. this.trans = "Cubic";
  75. break;
  76. case TRANS_TYPE.QUART:
  77. this.trans = "Quart";
  78. break;
  79. case TRANS_TYPE.QUINT:
  80. this.trans = "Quint";
  81. break;
  82. case TRANS_TYPE.SINE:
  83. this.trans = "Sine";
  84. break;
  85. case TRANS_TYPE.EXPONENTIAL:
  86. this.trans = "Expo";
  87. break;
  88. case TRANS_TYPE.CIRCULAR:
  89. this.trans = "Circ";
  90. break;
  91. case TRANS_TYPE.ELASTIC:
  92. this.trans = "Elastic";
  93. break;
  94. case TRANS_TYPE.BACK:
  95. this.trans = "Back";
  96. break;
  97. case TRANS_TYPE.BOUNCE:
  98. this.trans = "Bounce";
  99. break;
  100. }
  101. this.ease = "";
  102. if (this.transType == TRANS_TYPE.LINEAR) this.ease = "ease";
  103. else
  104. {
  105. switch (this.easeType)
  106. {
  107. case EASE_TYPE.IN:
  108. this.ease = "easeIn";
  109. break;
  110. case EASE_TYPE.OUT:
  111. this.ease = "easeOut";
  112. break;
  113. case EASE_TYPE.IN_OUT:
  114. this.ease = "easeInOut";
  115. break;
  116. }
  117. }
  118. }
  119. }
  120. class Tween extends GameObject
  121. {
  122. constructor(name)
  123. {
  124. super(name);
  125. this.tweenData = [];
  126. this.doneTweens = 0;
  127. this.done = false;
  128. }
  129. interpolateProperty(target, property, propertyType, initVal, finalVal, duration, transType = 1, easeType = 3, delay = 0)
  130. {
  131. this.done = false;
  132. this.tweenData.push(new TweenData(target, property, propertyType, initVal, finalVal, duration, transType, easeType, delay));
  133. }
  134. interpolate(td)
  135. {
  136. if (td.propertyType == PROPERTY_TYPE.NUMBER)
  137. td.target[td.property] = Easings[td.trans][td.ease](td.t, td.initVal, td.finalVal - td.initVal, td.duration);
  138. else
  139. {
  140. for (let i = 0; i < td.p.length; i++)
  141. td.target[td.property][td.p[i]] = Easings[td.trans][td.ease](td.t, td.initVal[td.p[i]], td.finalVal[td.p[i]] - td.initVal[td.p[i]], td.duration);
  142. }
  143. }
  144. start()
  145. {
  146. for (let i = 0; i < this.tweenData.length; i++)
  147. this.tweenData[i].playing = true;
  148. }
  149. startByIndex(idx)
  150. {
  151. if (idx < 0 && idx >= this.tweenData.length) return;
  152. this.tweenData[idx].playing = true;
  153. }
  154. stopAll()
  155. {
  156. for (let i = 0; i < this.tweenData.length; i++)
  157. this.tweenData[i].playing = false;
  158. }
  159. stopByIndex(idx)
  160. {
  161. if (idx < 0 && idx >= this.tweenData.length) return;
  162. this.tweenData[idx].playing = false;
  163. }
  164. resumeAll()
  165. {
  166. for (let i = 0; i < this.tweenData.length; i++)
  167. this.tweenData[i].playing = true;
  168. }
  169. resumeByIndex(idx)
  170. {
  171. if (idx < 0 && idx >= this.tweenData.length) return;
  172. this.tweenData[idx].playing = true;
  173. }
  174. resetAll()
  175. {
  176. this.doneTweens = 0;
  177. this.done = false;
  178. for (let i = 0; i < this.tweenData.length; i++)
  179. {
  180. this.tweenData[i].t = 0;
  181. this.tweenData[i].done = false;
  182. }
  183. }
  184. resetByIndex(idx)
  185. {
  186. if (idx < 0 && idx >= this.tweenData.length) return;
  187. this.doneTweens--;
  188. this.done = false;
  189. this.tweenData[idx].t = 0;
  190. this.tweenData[idx].done = false;
  191. }
  192. removeAll()
  193. {
  194. while (this.tweenData.length > 0)
  195. this.tweenData.pop();
  196. }
  197. removeByIndex(idx)
  198. {
  199. if (idx < 0 && idx >= this.tweenData.length) return;
  200. this.tweenData.splice(idx, 1);
  201. }
  202. seekAll(time)
  203. {
  204. if (time < 0) return;
  205. for (let i = 0; i < this.tweenData.length; i++)
  206. this.tweenData[i].t = min(time, this.tweenData[i].duration);
  207. }
  208. seekByIndex(idx, time)
  209. {
  210. if (idx < 0 && idx >= this.tweenData.length) return;
  211. this.tweenData[idx].t = min(time, this.tweenData[idx].duration);
  212. }
  213. allDone()
  214. {
  215. this.done = true;
  216. }
  217. update(delta)
  218. {
  219. if (!this.done && this.doneTweens == this.tweenData.length) this.allDone();
  220. for (let i = 0; i < this.tweenData.length; i++)
  221. {
  222. if (!this.tweenData[i].playing) continue;
  223. if (this.tweenData[i].t >= 0)
  224. this.interpolate(this.tweenData[i]);
  225. this.tweenData[i].t = min(this.tweenData[i].t + delta, this.tweenData[i].duration);
  226. if (!this.tweenData[i].done && this.tweenData[i].t == this.tweenData[i].duration)
  227. {
  228. this.tweenData[i].done = true;
  229. this.doneTweens += 1;
  230. }
  231. }
  232. this._update(delta);
  233. for (let i = 0; i < this.children.length; i++)
  234. this.children[i].update(delta);
  235. }
  236. }