Skip to content

Commit aa78bf7

Browse files
committed
Added rotate option.
1 parent e3bd25a commit aa78bf7

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

v3/src/gameobjects/pathfollower/PathFollower.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
var Class = require('../../utils/Class');
2+
var GetBoolean = require('../../tween/builder/GetBoolean');
3+
var GetValue = require('../../utils/object/getValue');
24
var Sprite = require('../sprite/Sprite');
35
var Vector2 = require('../../math/Vector2');
46

@@ -14,13 +16,26 @@ var PathFollower = new Class({
1416

1517
this.path = path;
1618

19+
this.rotateToPath = false;
20+
this.pathRotationOffset = 0;
21+
1722
this.pathOffset = new Vector2(x, y);
1823

1924
this.pathVector = new Vector2();
2025

2126
this.pathTween;
2227
},
2328

29+
setRotateToPath: function (value, offset)
30+
{
31+
if (offset === undefined) { offset = 0; }
32+
33+
this.rotateToPath = value;
34+
this.pathRotationOffset = offset;
35+
36+
return this;
37+
},
38+
2439
start: function (config)
2540
{
2641
if (config === undefined) { config = {}; }
@@ -39,7 +54,10 @@ var PathFollower = new Class({
3954
config.from = 0;
4055
config.to = 1;
4156

42-
// Can also read extra values out of the config, like autoRotate
57+
// Can also read extra values out of the config:
58+
59+
this.rotateToPath = GetBoolean(config, 'rotateToPath', false);
60+
this.pathRotationOffset = GetValue(config, 'rotationOffset', 0);
4361

4462
this.pathTween = this.scene.sys.tweens.addCounter(config);
4563

@@ -95,7 +113,15 @@ var PathFollower = new Class({
95113

96114
this.pathVector.add(this.pathOffset);
97115

116+
var oldX = this.x;
117+
var oldY = this.y;
118+
98119
this.setPosition(this.pathVector.x, this.pathVector.y);
120+
121+
if (this.rotateToPath)
122+
{
123+
this.rotation = Math.atan2(this.y - oldY, this.x - oldX) + this.pathRotationOffset;
124+
}
99125
}
100126
}
101127

0 commit comments

Comments
 (0)