Tween.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. play()
  145. {
  146. for (let i = 0; i < this.tweenData.length; i++)
  147. {
  148. this.tweenData[i].playing = true;
  149. }
  150. }
  151. update(delta)
  152. {
  153. if (!this.done && this.doneTweens == this.tweenData.length) this.allDone();
  154. for (let i = 0; i < this.tweenData.length; i++)
  155. {
  156. if (!this.tweenData[i].playing) continue;
  157. if (this.tweenData[i].t >= 0)
  158. this.interpolate(this.tweenData[i]);
  159. this.tweenData[i].t = min(this.tweenData[i].t + delta, this.tweenData[i].duration);
  160. if (!this.tweenData[i].done && this.tweenData[i].t == this.tweenData[i].duration)
  161. {
  162. this.tweenData[i].done = true;
  163. this.doneTweens += 1;
  164. }
  165. }
  166. this._update(delta);
  167. for (let i = 0; i < this.children.length; i++)
  168. this.children[i].update(delta);
  169. }
  170. allDone()
  171. {
  172. this.done = true;
  173. }
  174. }