Skip to content

Commit d8724ed

Browse files
committed
Fixed Array cloning.
New build process for Tween.
1 parent 70802ce commit d8724ed

3 files changed

Lines changed: 170 additions & 23 deletions

File tree

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: '52ed8ac0-3529-11e7-a5a4-694da76b1298'
2+
build: '6e27ac20-3665-11e7-8a29-950390afce19'
33
};
44
module.exports = CHECKSUM;

v3/src/tween/Tween.js

Lines changed: 159 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var GetEaseFunction = require('./GetEaseFunction');
33
var CloneObject = require('../utils/object/Clone');
44
var MergeRight = require('../utils/object/MergeRight');
55

6-
var RESERVED = [ 'targets', 'ease', 'duration', 'yoyo', 'repeat', 'loop', 'paused', 'useFrames', 'offset' ];
6+
// var RESERVED = [ 'targets', 'ease', 'duration', 'yoyo', 'repeat', 'loop', 'paused', 'useFrames', 'offset' ];
77

88
/*
99
The following are all the same
@@ -93,12 +93,15 @@ var RESERVED = [ 'targets', 'ease', 'duration', 'yoyo', 'repeat', 'loop', 'pause
9393
var tween = this.tweens.add({
9494
targets: [ alien1, alien2, alien3, alienBoss ],
9595
props: {
96-
x: [ 200, 300, 400 ]
96+
x: [ 200, 300, 400 ],
97+
y: [ '+100', '-100', '+100' ]
9798
},
9899
duration: 1000,
99100
ease: 'Sine'
100101
});
101102
103+
// Timeline concept
104+
102105
var tween = this.tweens.add({
103106
targets: player,
104107
timeline: [
@@ -121,12 +124,52 @@ var Tween = function (manager, config)
121124
// and properties. However if you've got a target that has a property that matches one of the
122125
// reserved words, i.e. Target.duration - that you want to tween, then pass it inside a property
123126
// called `props`. If present it will use the contents of the `props` object instead.
127+
// If you have a Target property you want to tween called 'props' then you're SOL I'm afraid!
128+
129+
// Array of Targets
130+
131+
// [
132+
// {
133+
// target: targetRef,
134+
// props: {
135+
// x: {
136+
// start: 0,
137+
// current: 0,
138+
// end: 0
139+
// },
140+
// y: {
141+
// start: 0,
142+
// current: 0,
143+
// end: 0
144+
// }
145+
// }
146+
// }
147+
// ]
148+
149+
this.targets = [];
150+
151+
this.props = [];
152+
153+
// One of these for every property being tweened
154+
this.defaultTweenProp = {
155+
key: '',
156+
value: 0, // target value (as defined in the original tween, could be a number, string or function)
157+
current: 0, // index to which TweenData in the queue it's processing
158+
queue: [] // array of TweenData objects (always at least 1, but can be more)
159+
};
124160

125-
this.targets = this.setTargets(GetValue(config, 'targets', null));
161+
// Keep start and end values to ensure we never go over them when ending
162+
this.defaultTargetProp = {
163+
start: 0,
164+
current: 0,
165+
end: 0
166+
};
126167

127-
// 'Default' Tween properties:
168+
// 'Default' Tween properties, duplicated for each property
169+
// Swap for local getValue for speed
170+
// TODO: Add local getters (getLoop, getProgress, etc)
128171

129-
this.tweenData = {
172+
this.defaultTweenData = {
130173
ease: GetEaseFunction(GetValue(config, 'ease', 'Power0')),
131174
duration: GetValue(config, 'duration', 1000),
132175
yoyo: GetValue(config, 'yoyo', false),
@@ -136,22 +179,10 @@ var Tween = function (manager, config)
136179
startAt: null,
137180
progress: 0,
138181
startTime: 0,
139-
elapsed: 0
182+
elapsed: 0,
183+
direction: 0 // 0 = forward, 1 = reverse
140184
};
141185

142-
// One of these for every property being tweened (min 1)
143-
this.tweenProps = {
144-
key: '',
145-
endValue: 0,
146-
current: 0,
147-
dataQueue: [] // array of TweenData objects
148-
};
149-
150-
// One of these per Target, per Property (min 1)
151-
this.tweenTarget = {
152-
target: undefined,
153-
currentValue: 0
154-
};
155186

156187
// this.ease = GetEaseFunction(GetValue(config, 'ease', 'Power0'));
157188
// this.duration = GetValue(config, 'duration', 1000);
@@ -197,13 +228,106 @@ var Tween = function (manager, config)
197228
// this.progress = 0;
198229
// this.totalDuration = 0;
199230

200-
this.buildTweenData(config);
231+
this.build(config);
201232
};
202233

203234
Tween.prototype.constructor = Tween;
204235

205236
Tween.prototype = {
206237

238+
// Build Process:
239+
240+
// For Each Prop
241+
// Create TweenProp object
242+
// Populate queue with TweenData objects (at least 1)
243+
// Add to props array
244+
// For Each Target
245+
// Create Target object
246+
// For Each Prop
247+
// Create TargetProp object in props object
248+
249+
build: function (config)
250+
{
251+
// For now let's just assume `config.props` is being used:
252+
253+
var propKeys = [];
254+
255+
for (var key in config.props)
256+
{
257+
var prop = CloneObject(this.defaultTweenProp);
258+
259+
var data;
260+
var value = config.props[key];
261+
262+
if (typeof value === 'number')
263+
{
264+
// props: {
265+
// x: 400,
266+
// y: 300
267+
// }
268+
data = CloneObject(this.defaultTweenData);
269+
270+
prop.value = parseFloat(value);
271+
}
272+
else if (typeof value === 'string')
273+
{
274+
// props: {
275+
// x: '+400',
276+
// y: '-300'
277+
// }
278+
}
279+
else if (typeof value === 'function')
280+
{
281+
// props: {
282+
// x: function () { return Math.random() * 10 },
283+
// y: someOtherCallback
284+
// }
285+
data = CloneObject(this.defaultTweenData);
286+
287+
prop.value = parseFloat(value.call());
288+
}
289+
else
290+
{
291+
// props: {
292+
// x: { value: 400, ... },
293+
// y: { value: 300, ... }
294+
// }
295+
296+
data = MergeRight(this.defaultTweenData, value);
297+
298+
// Value may still be a string, function or number though
299+
300+
prop.value = parseFloat(data.value);
301+
}
302+
303+
prop.key = key;
304+
prop.queue.push(data);
305+
306+
this.props.push(prop);
307+
308+
propKeys.push(key);
309+
}
310+
311+
var targets = this.getTargets(config);
312+
313+
for (var i = 0; i < targets.length; i++)
314+
{
315+
var target = {
316+
ref: targets[i],
317+
props: {}
318+
};
319+
320+
for (var k = 0; k < propKeys.length; k++)
321+
{
322+
target.props[propKeys[k]] = CloneObject(this.defaultTargetProp);
323+
}
324+
325+
this.targets.push(target);
326+
}
327+
},
328+
329+
// getPropValue
330+
207331
// Move to own functions
208332

209333
getV: function (obj, key)
@@ -283,6 +407,18 @@ Tween.prototype = {
283407
}
284408
},
285409

410+
// Update Loop:
411+
412+
// For Each Prop
413+
// Get current TweenData
414+
// Update elapsed time
415+
// Get ease value
416+
// For Each Target
417+
// Apply ease value to Target value
418+
// Has the tween finished?
419+
// No: Wait for next update
420+
// Yes: Advance queue index, or complete tween
421+
286422
update: function (timestep, delta)
287423
{
288424
if (!this.running)
@@ -351,8 +487,10 @@ Tween.prototype = {
351487

352488
},
353489

354-
setTargets: function (targets)
490+
getTargets: function (config)
355491
{
492+
var targets = GetValue(config, 'targets', null);
493+
356494
if (typeof targets === 'function')
357495
{
358496
targets = targets.call();

v3/src/utils/object/Clone.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1+
// Shallow Clone
2+
13
var Clone = function (obj)
24
{
35
var clone = {};
46

57
for (var key in obj)
68
{
7-
clone[key] = obj[key];
9+
if (Array.isArray(obj[key]))
10+
{
11+
clone[key] = obj[key].slice(0);
12+
}
13+
else
14+
{
15+
clone[key] = obj[key];
16+
}
817
}
918

1019
return clone;

0 commit comments

Comments
 (0)