Skip to content

Commit d86a6d2

Browse files
committed
Added PathFollower Game Object
1 parent bd1154c commit d86a6d2

3 files changed

Lines changed: 132 additions & 1 deletion

File tree

v3/src/gameobjects/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ var GameObjects = {
1212
Image: require('./image/Image'),
1313
ObjectPool: require('./pool/ObjectPool.js'),
1414
ParticleEmitter: require('./emitter/ParticleEmitter'),
15-
Sprite: require('./sprite/Sprite'),
15+
PathFollower: require('./pathfollower/PathFollower'),
1616
Sprite3D: require('./sprite3d/Sprite3D'),
17+
Sprite: require('./sprite/Sprite'),
1718
StaticTilemap: require('./tilemap/static/StaticTilemap'),
1819
Text: require('./text/static/Text'),
1920
Tilemap: require('./tilemap/dynamic/Tilemap'),
@@ -30,6 +31,7 @@ var GameObjects = {
3031
Group: require('./group/GroupFactory'),
3132
Image: require('./image/ImageFactory'),
3233
ParticleEmitter: require('./emitter/ParticleEmitterFactory'),
34+
PathFollower: require('./pathfollower/PathFollowerFactory'),
3335
Sprite: require('./sprite/SpriteFactory'),
3436
Sprite3D: require('./sprite3d/Sprite3DFactory'),
3537
StaticBitmapText: require('./bitmaptext/static/BitmapTextFactory'),
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
var Class = require('../../utils/Class');
2+
var Linear = require('../../math/easing/Linear');
3+
var Sprite = require('../sprite/Sprite');
4+
var Vector2 = require('../../math/Vector2');
5+
// GetEaseFunction
6+
7+
var PathFollower = new Class({
8+
9+
Extends: Sprite,
10+
11+
initialize:
12+
13+
function PathFollower (scene, path, x, y, texture, frame)
14+
{
15+
Sprite.call(this, scene, x, y, texture, frame);
16+
17+
this.path = path;
18+
19+
this.pathOffset = new Vector2(x, y);
20+
this.pathVector = new Vector2();
21+
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;
37+
},
38+
39+
start: function (duration)
40+
{
41+
this.duration = duration;
42+
this.isFollowing = true;
43+
this.pathT = 0;
44+
this.timeScale = 1;
45+
this.elapsed = 0;
46+
this.progress = 0;
47+
48+
// path.startPoint
49+
50+
// The starting point of the path, relative to this follower
51+
52+
var start = this.path.getStartPoint();
53+
var diffX = this.pathOffset.x - start.x;
54+
var diffY = this.pathOffset.y - start.y;
55+
56+
this.pathOffset.set(diffX, diffY);
57+
},
58+
59+
preUpdate: function (time, delta)
60+
{
61+
this.anims.update(time, delta);
62+
63+
if (!this.isFollowing)
64+
{
65+
return;
66+
}
67+
68+
// Apply ease to pathT
69+
delta *= this.timeScale;
70+
71+
this.elapsed += delta;
72+
this.progress = Math.min(this.elapsed / this.duration, 1);
73+
74+
if (this.elapsed > this.duration)
75+
{
76+
var diff = this.elapsed - this.duration;
77+
this.elapsed = this.duration;
78+
}
79+
80+
var forward = true;
81+
82+
this.progress = this.elapsed / this.duration;
83+
84+
if (forward)
85+
{
86+
this.pathT = this.ease(this.progress);
87+
}
88+
else
89+
{
90+
this.pathT = this.ease(1 - this.progress);
91+
}
92+
93+
this.path.getPoint(this.pathT, this.pathVector);
94+
95+
this.pathVector.add(this.pathOffset);
96+
97+
this.setPosition(this.pathVector.x, this.pathVector.y);
98+
99+
// this.setDepth(this.y);
100+
101+
if (this.progress === 1)
102+
{
103+
this.isFollowing = false;
104+
}
105+
}
106+
107+
});
108+
109+
module.exports = PathFollower;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var PathFollower = require('./PathFollower');
2+
var GameObjectFactory = require('../../scene/plugins/GameObjectFactory');
3+
4+
// When registering a factory function 'this' refers to the GameObjectFactory context.
5+
//
6+
// There are several properties available to use:
7+
//
8+
// this.scene - a reference to the Scene that owns the GameObjectFactory
9+
// this.displayList - a reference to the Display List the Scene owns
10+
// this.updateList - a reference to the Update List the Scene owns
11+
12+
GameObjectFactory.register('follower', function (path, x, y, key, frame)
13+
{
14+
var sprite = new PathFollower(this.scene, path, x, y, key, frame);
15+
16+
this.displayList.add(sprite);
17+
this.updateList.add(sprite);
18+
19+
return sprite;
20+
});

0 commit comments

Comments
 (0)