Skip to content

Commit a077abb

Browse files
committed
Testing with tween
1 parent d86a6d2 commit a077abb

1 file changed

Lines changed: 51 additions & 60 deletions

File tree

v3/src/gameobjects/pathfollower/PathFollower.js

Lines changed: 51 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
var Class = require('../../utils/Class');
2-
var Linear = require('../../math/easing/Linear');
32
var Sprite = require('../sprite/Sprite');
43
var Vector2 = require('../../math/Vector2');
5-
// GetEaseFunction
64

75
var PathFollower = new Class({
86

@@ -17,90 +15,83 @@ var PathFollower = new Class({
1715
this.path = path;
1816

1917
this.pathOffset = new Vector2(x, y);
18+
2019
this.pathVector = new Vector2();
2120

22-
// Path
23-
// Direction (forwards, backwards)
24-
// Speed (or is that controlled by the path?)
25-
// Rotate to face direction
26-
// Speed function (ease, default Linear)
27-
// Start from T along the path
28-
// Start from path x/y, or current x/y
29-
30-
this.isFollowing = false;
31-
this.pathT = 0;
32-
this.timeScale = 1;
33-
this.elapsed = 0;
34-
this.progress = 0;
35-
this.duration = 0;
36-
this.ease = Linear;
21+
this.pathTween;
22+
23+
this.pathData = {
24+
t: 0
25+
};
3726
},
3827

39-
start: function (duration)
28+
start: function (config)
4029
{
41-
this.duration = duration;
42-
this.isFollowing = true;
43-
this.pathT = 0;
44-
this.timeScale = 1;
45-
this.elapsed = 0;
46-
this.progress = 0;
30+
if (config === undefined) { config = {}; }
4731

48-
// path.startPoint
32+
if (typeof config === 'number')
33+
{
34+
config = { duration: config };
35+
}
36+
37+
// Add the targets and property into the mix
38+
config.targets = this.pathData;
39+
config.t = 1;
40+
41+
this.pathTween = this.scene.sys.tweens.add(config);
4942

5043
// The starting point of the path, relative to this follower
5144

45+
// this.path.getStartPoint(this.pathOffset);
5246
var start = this.path.getStartPoint();
53-
var diffX = this.pathOffset.x - start.x;
54-
var diffY = this.pathOffset.y - start.y;
5547

56-
this.pathOffset.set(diffX, diffY);
57-
},
58-
59-
preUpdate: function (time, delta)
60-
{
61-
this.anims.update(time, delta);
48+
// console.log('getStartPoint', this.pathOffset.x, this.pathOffset.y);
49+
console.log('getStartPoint2', this.x, this.y);
6250

63-
if (!this.isFollowing)
64-
{
65-
return;
66-
}
51+
var diffX = this.x - start.x;
52+
var diffY = this.y - start.y;
6753

68-
// Apply ease to pathT
69-
delta *= this.timeScale;
54+
console.log('diffX', diffX, 'diffY', diffY);
7055

71-
this.elapsed += delta;
72-
this.progress = Math.min(this.elapsed / this.duration, 1);
56+
this.pathOffset.set(diffX, diffY);
7357

74-
if (this.elapsed > this.duration)
75-
{
76-
var diff = this.elapsed - this.duration;
77-
this.elapsed = this.duration;
78-
}
7958

80-
var forward = true;
59+
},
8160

82-
this.progress = this.elapsed / this.duration;
61+
/*
62+
playing: {
8363
84-
if (forward)
64+
get: function ()
8565
{
86-
this.pathT = this.ease(this.progress);
87-
}
88-
else
66+
return this.pathData.playing;
67+
},
68+
69+
set: function (value)
8970
{
90-
this.pathT = this.ease(1 - this.progress);
71+
if (!value)
72+
{
73+
this.stop();
74+
}
75+
else
76+
{
77+
this.start();
78+
}
9179
}
9280
93-
this.path.getPoint(this.pathT, this.pathVector);
81+
}
82+
*/
9483

95-
this.pathVector.add(this.pathOffset);
84+
preUpdate: function (time, delta)
85+
{
86+
this.anims.update(time, delta);
9687

97-
this.setPosition(this.pathVector.x, this.pathVector.y);
88+
if (this.pathTween && this.pathTween.isPlaying())
89+
{
90+
this.path.getPoint(this.pathData.t, this.pathVector);
9891

99-
// this.setDepth(this.y);
92+
this.pathVector.add(this.pathOffset);
10093

101-
if (this.progress === 1)
102-
{
103-
this.isFollowing = false;
94+
this.setPosition(this.pathVector.x, this.pathVector.y);
10495
}
10596
}
10697

0 commit comments

Comments
 (0)