|
@@ -19,13 +19,40 @@
|
|
|
* along with Pandora. If not, see <https:
|
|
|
*************************************************************************/
|
|
|
|
|
|
+
|
|
|
+ * The {@code TweenData} class represents the data that a Tween GameObject needs to
|
|
|
+ * interpolate a given property.
|
|
|
+ *
|
|
|
+ * @author Pedro Schneider
|
|
|
+ *
|
|
|
+ * @class
|
|
|
+ */
|
|
|
class TweenData
|
|
|
{
|
|
|
+
|
|
|
+ * @constructor
|
|
|
+ * Creates a TweenData Object with the specified parameters.
|
|
|
+ *
|
|
|
+ * @param {Object} target Object that has the property to be interpolated.
|
|
|
+ * @param {String} property name of the property of target to be interpolated.
|
|
|
+ * target[property] should be number, Vector2 or Color.
|
|
|
+ * @param {PROPERTY_TYPE} propertyType type of the property to be interpolated.
|
|
|
+ * @param {number, Vector2, Color} initVal initial value for the interpolation.
|
|
|
+ * Should be the same type as target[property].
|
|
|
+ * @param {number, Vector2, Color} finalVal final value for the interpolation.
|
|
|
+ * Should be the same type as target[property].
|
|
|
+ * @param {number} duration duration in seconds of the interpolation.
|
|
|
+ * @param {TRANS_TYPE} transType transition type of the interpolation.
|
|
|
+ * @param {EASE_TYPE} easeType easing type of the interpolation.
|
|
|
+ * @param {number} delay delay in seconds for the interpolation to start.
|
|
|
+ */
|
|
|
constructor(target, property, propertyType, initVal, finalVal, duration, transType, easeType, delay)
|
|
|
{
|
|
|
- this.target = target;
|
|
|
- this.property = property;
|
|
|
- this.propertyType = propertyType;
|
|
|
+ this.target = target;
|
|
|
+ this.property = property;
|
|
|
+ this.propertyType = propertyType;
|
|
|
+
|
|
|
+
|
|
|
switch (this.propertyType)
|
|
|
{
|
|
|
case PROPERTY_TYPE.COLOR:
|
|
@@ -41,15 +68,16 @@ class TweenData
|
|
|
this.finalVal = finalVal;
|
|
|
break;
|
|
|
}
|
|
|
- this.duration = duration;
|
|
|
- this.transType = transType;
|
|
|
- this.easeType = easeType;
|
|
|
|
|
|
- this.t = -delay;
|
|
|
- this.playing = false;
|
|
|
- this.done = false;
|
|
|
+ this.duration = duration;
|
|
|
+ this.transType = transType;
|
|
|
+ this.easeType = easeType;
|
|
|
|
|
|
- this.p = [];
|
|
|
+ this.t = -delay;
|
|
|
+ this.playing = false;
|
|
|
+ this.done = false;
|
|
|
+
|
|
|
+ this.p = [];
|
|
|
switch (this.propertyType)
|
|
|
{
|
|
|
case PROPERTY_TYPE.COLOR:
|
|
@@ -65,7 +93,7 @@ class TweenData
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
- this.trans = "";
|
|
|
+ this.trans = "";
|
|
|
switch (this.transType)
|
|
|
{
|
|
|
case TRANS_TYPE.LINEAR:
|
|
@@ -103,7 +131,7 @@ class TweenData
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
- this.ease = "";
|
|
|
+ this.ease = "";
|
|
|
if (this.transType == TRANS_TYPE.LINEAR) this.ease = "ease";
|
|
|
else
|
|
|
{
|
|
@@ -123,8 +151,23 @@ class TweenData
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * The {@code Tween} class represents a Tween GameObject that has functionality to
|
|
|
+ * interpolate any property of another GameObject if the properties are of the type
|
|
|
+ * number, Vector2 or Color.
|
|
|
+ *
|
|
|
+ * @author Pedro Schneider
|
|
|
+ *
|
|
|
+ * @class
|
|
|
+ */
|
|
|
class Tween extends GameObject
|
|
|
{
|
|
|
+
|
|
|
+ * @constructor
|
|
|
+ * Creates an empty Tween GameObject.
|
|
|
+ *
|
|
|
+ * @param {String} name name of the Tween GameObject.
|
|
|
+ */
|
|
|
constructor(name)
|
|
|
{
|
|
|
super(name);
|
|
@@ -134,12 +177,40 @@ class Tween extends GameObject
|
|
|
this.done = false;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * Add a new TweenData Object to this Tween with the necessary information to interpolate
|
|
|
+ * the target's property.
|
|
|
+ *
|
|
|
+ * @param {Object} target Object that has the property to be interpolated.
|
|
|
+ * @param {String} property name of the property of target to be interpolated.
|
|
|
+ * target[property] should be number, Vector2 or Color.
|
|
|
+ * @param {PROPERTY_TYPE} propertyType type of the property to be interpolated.
|
|
|
+ * @param {number, Vector2, Color} initVal initial value for the interpolation.
|
|
|
+ * Should be the same type as target[property].
|
|
|
+ * @param {number, Vector2, Color} finalVal final value for the interpolation.
|
|
|
+ * Should be the same type as target[property].
|
|
|
+ * @param {number} duration duration in seconds of the interpolation.
|
|
|
+ * @param {TRANS_TYPE} transType transition type of the interpolation.
|
|
|
+ * Default is TRANS_TYPE.LINEAR.
|
|
|
+ * @param {EASE_TYPE} easeType easing type of the interpolation.
|
|
|
+ * Default is EASY_TYPE.IN_OUT.
|
|
|
+ * @param {number} delay delay in seconds for the interpolation to start.
|
|
|
+ * Default is 0.
|
|
|
+ */
|
|
|
interpolateProperty(target, property, propertyType, initVal, finalVal, duration, transType = 1, easeType = 3, delay = 0)
|
|
|
{
|
|
|
- this.done = false;
|
|
|
+ this.done = false;
|
|
|
+
|
|
|
+
|
|
|
this.tweenData.push(new TweenData(target, property, propertyType, initVal, finalVal, duration, transType, easeType, delay));
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * Given a TweenData, sets its interpolation's target's property to the appropriate value for
|
|
|
+ * the current time of the interpolation.
|
|
|
+ *
|
|
|
+ * @param {TweenData} td reference to the TweenData Object.
|
|
|
+ */
|
|
|
interpolate(td)
|
|
|
{
|
|
|
if (td.propertyType == PROPERTY_TYPE.NUMBER)
|
|
@@ -151,42 +222,81 @@ class Tween extends GameObject
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- start()
|
|
|
+
|
|
|
+ * Starts interpolating all TweenData Objectcs currently added to this Tween.
|
|
|
+ */
|
|
|
+ startAll()
|
|
|
{
|
|
|
for (let i = 0; i < this.tweenData.length; i++)
|
|
|
this.tweenData[i].playing = true;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * Starts interpolating a specific TweenData Object based on its index.
|
|
|
+ *
|
|
|
+ * ! Since TwennData are not GameObjects, this is the only way to query
|
|
|
+ * ! for them. The index refera to the order you added the TweenData to
|
|
|
+ * ! this Tween, starting at 0.
|
|
|
+ *
|
|
|
+ * @param {number} idx index of the desired TweenData to start.
|
|
|
+ */
|
|
|
startByIndex(idx)
|
|
|
{
|
|
|
if (idx < 0 && idx >= this.tweenData.length) return;
|
|
|
this.tweenData[idx].playing = true;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * Stops interpolating all TweenData Objects currently added to this Tween.
|
|
|
+ */
|
|
|
stopAll()
|
|
|
{
|
|
|
for (let i = 0; i < this.tweenData.length; i++)
|
|
|
this.tweenData[i].playing = false;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * Stops interpolating a specific TweenData Object based on its index.
|
|
|
+ *
|
|
|
+ * ! Since TwennData are not GameObjects, this is the only way to query
|
|
|
+ * ! for them. The index refera to the order you added the TweenData to
|
|
|
+ * ! this Tween, starting at 0.
|
|
|
+ *
|
|
|
+ * @param {number} idx index of the desired TweenData to stop.
|
|
|
+ */
|
|
|
stopByIndex(idx)
|
|
|
{
|
|
|
if (idx < 0 && idx >= this.tweenData.length) return;
|
|
|
this.tweenData[idx].playing = false;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * Resumes interpolating all TweenData currently added to this Tween.
|
|
|
+ */
|
|
|
resumeAll()
|
|
|
{
|
|
|
for (let i = 0; i < this.tweenData.length; i++)
|
|
|
this.tweenData[i].playing = true;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * Resumes interpolating a specific TweenData Object based on its index.
|
|
|
+ *
|
|
|
+ * ! Since TwennData are not GameObjects, this is the only way to query
|
|
|
+ * ! for them. The index refera to the order you added the TweenData to
|
|
|
+ * ! this Tween, starting at 0.
|
|
|
+ *
|
|
|
+ * @param {number} idx index of the desired TweenData to resume.
|
|
|
+ */
|
|
|
resumeByIndex(idx)
|
|
|
{
|
|
|
if (idx < 0 && idx >= this.tweenData.length) return;
|
|
|
this.tweenData[idx].playing = true;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * Resets all TweenData currently added to this Tween.
|
|
|
+ */
|
|
|
resetAll()
|
|
|
{
|
|
|
this.doneTweens = 0;
|
|
@@ -198,6 +308,15 @@ class Tween extends GameObject
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * Resets a specific TweenData Object based on its index.
|
|
|
+ *
|
|
|
+ * ! Since TwennData are not GameObjects, this is the only way to query
|
|
|
+ * ! for them. The index refera to the order you added the TweenData to
|
|
|
+ * ! this Tween, starting at 0.
|
|
|
+ *
|
|
|
+ * @param {number} idx index of the desired TweenData to reset.
|
|
|
+ */
|
|
|
resetByIndex(idx)
|
|
|
{
|
|
|
if (idx < 0 && idx >= this.tweenData.length) return;
|
|
@@ -207,18 +326,36 @@ class Tween extends GameObject
|
|
|
this.tweenData[idx].done = false;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * Removes all TweenData currently added to this Tween.
|
|
|
+ */
|
|
|
removeAll()
|
|
|
{
|
|
|
while (this.tweenData.length > 0)
|
|
|
this.tweenData.pop();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * Removes a specific TweenData Object based on its index.
|
|
|
+ *
|
|
|
+ * ! Since TwennData are not GameObjects, this is the only way to query
|
|
|
+ * ! for them. The index refera to the order you added the TweenData to
|
|
|
+ * ! this Tween, starting at 0.
|
|
|
+ *
|
|
|
+ * @param {number} idx index of the desired TweenData to remove.
|
|
|
+ */
|
|
|
removeByIndex(idx)
|
|
|
{
|
|
|
if (idx < 0 && idx >= this.tweenData.length) return;
|
|
|
this.tweenData.splice(idx, 1);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * Sets the current time of all TweenData currently added to this Tween
|
|
|
+ * to the specified time.
|
|
|
+ *
|
|
|
+ * @param {number} time time in seconds to seek all TweenData on this Tween.
|
|
|
+ */
|
|
|
seekAll(time)
|
|
|
{
|
|
|
if (time < 0) return;
|
|
@@ -226,18 +363,47 @@ class Tween extends GameObject
|
|
|
this.tweenData[i].t = min(time, this.tweenData[i].duration);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * Sets the current time of a specific TweenData Object, based on its index,
|
|
|
+ * to the specified time.
|
|
|
+ *
|
|
|
+ * ! Since TwennData are not GameObjects, this is the only way to query
|
|
|
+ * ! for them. The index refera to the order you added the TweenData to
|
|
|
+ * ! this Tween, starting at 0.
|
|
|
+ *
|
|
|
+ * @param {number} idx index of the TweenData to seek to the time.
|
|
|
+ * @param {number} time time in seconds to seek the specified TweenData
|
|
|
+ */
|
|
|
seekByIndex(idx, time)
|
|
|
{
|
|
|
if (idx < 0 && idx >= this.tweenData.length) return;
|
|
|
this.tweenData[idx].t = min(time, this.tweenData[idx].duration);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * Called once every time all TweenData on this Tween are completed.
|
|
|
+ * Emits the tweenDataAllCompleted signal.
|
|
|
+ */
|
|
|
allDone()
|
|
|
{
|
|
|
this.emitSignal("tweenAllCompleted");
|
|
|
this.done = true;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * @override
|
|
|
+ * Adds default signals for the Tween GameObject and serves as a caller
|
|
|
+ * to the _initSignals() callback.
|
|
|
+ *
|
|
|
+ * @signal tweenAllCompleted Emited once when all TweenData on this Tween
|
|
|
+ * are done.
|
|
|
+ * @signal tweenCompleted Emited once when one TweenData on this Tween
|
|
|
+ * is done. Passes the completed TweenData as a
|
|
|
+ * parameter.
|
|
|
+ * @signal tweenStarted Emited once when one TweenData on this Tween
|
|
|
+ * starts. Passes the started TweenData as a
|
|
|
+ * parameter.
|
|
|
+ */
|
|
|
initSignals()
|
|
|
{
|
|
|
this.addSignal("tweenAllCompleted");
|
|
@@ -246,23 +412,34 @@ class Tween extends GameObject
|
|
|
this._initSignals();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * @override
|
|
|
+ * Updates all TweenData added to this Tween and recursively calls the _update(delta)
|
|
|
+ * callback for this GameObject and all of it's children.
|
|
|
+ * @param {*} delta
|
|
|
+ */
|
|
|
update(delta)
|
|
|
{
|
|
|
+
|
|
|
if (!this.done && this.doneTweens == this.tweenData.length) this.allDone();
|
|
|
|
|
|
for (let i = 0; i < this.tweenData.length; i++)
|
|
|
{
|
|
|
+
|
|
|
if (!this.tweenData[i].playing) continue;
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
if (this.tweenData[i].t >= 0)
|
|
|
- {
|
|
|
this.interpolate(this.tweenData[i]);
|
|
|
- }
|
|
|
|
|
|
- if (this.tweenData[i].t < 0 && this.tweenData[i].t + delta >= 0)
|
|
|
+
|
|
|
+ if (this.tweenData[i].t <= 0 && this.tweenData[i].t + delta >= 0)
|
|
|
this.emitSignal("tweenStarted", this.tweenData[i]);
|
|
|
+
|
|
|
+
|
|
|
this.tweenData[i].t = min(this.tweenData[i].t + delta, this.tweenData[i].duration);
|
|
|
|
|
|
+
|
|
|
if (!this.tweenData[i].done && this.tweenData[i].t == this.tweenData[i].duration)
|
|
|
{
|
|
|
this.emitSignal("tweenDone", this.tweenData[i]);
|