mirror of
https://github.com/photonstorm/phaser
synced 2024-11-28 15:41:37 +00:00
Testing with tween
This commit is contained in:
parent
d86a6d21b1
commit
a077abb8ce
1 changed files with 55 additions and 64 deletions
|
@ -1,8 +1,6 @@
|
||||||
var Class = require('../../utils/Class');
|
var Class = require('../../utils/Class');
|
||||||
var Linear = require('../../math/easing/Linear');
|
|
||||||
var Sprite = require('../sprite/Sprite');
|
var Sprite = require('../sprite/Sprite');
|
||||||
var Vector2 = require('../../math/Vector2');
|
var Vector2 = require('../../math/Vector2');
|
||||||
// GetEaseFunction
|
|
||||||
|
|
||||||
var PathFollower = new Class({
|
var PathFollower = new Class({
|
||||||
|
|
||||||
|
@ -17,90 +15,83 @@ var PathFollower = new Class({
|
||||||
this.path = path;
|
this.path = path;
|
||||||
|
|
||||||
this.pathOffset = new Vector2(x, y);
|
this.pathOffset = new Vector2(x, y);
|
||||||
|
|
||||||
this.pathVector = new Vector2();
|
this.pathVector = new Vector2();
|
||||||
|
|
||||||
// Path
|
this.pathTween;
|
||||||
// Direction (forwards, backwards)
|
|
||||||
// Speed (or is that controlled by the path?)
|
|
||||||
// Rotate to face direction
|
|
||||||
// Speed function (ease, default Linear)
|
|
||||||
// Start from T along the path
|
|
||||||
// Start from path x/y, or current x/y
|
|
||||||
|
|
||||||
this.isFollowing = false;
|
this.pathData = {
|
||||||
this.pathT = 0;
|
t: 0
|
||||||
this.timeScale = 1;
|
};
|
||||||
this.elapsed = 0;
|
|
||||||
this.progress = 0;
|
|
||||||
this.duration = 0;
|
|
||||||
this.ease = Linear;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
start: function (duration)
|
start: function (config)
|
||||||
{
|
{
|
||||||
this.duration = duration;
|
if (config === undefined) { config = {}; }
|
||||||
this.isFollowing = true;
|
|
||||||
this.pathT = 0;
|
|
||||||
this.timeScale = 1;
|
|
||||||
this.elapsed = 0;
|
|
||||||
this.progress = 0;
|
|
||||||
|
|
||||||
// path.startPoint
|
if (typeof config === 'number')
|
||||||
|
{
|
||||||
|
config = { duration: config };
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the targets and property into the mix
|
||||||
|
config.targets = this.pathData;
|
||||||
|
config.t = 1;
|
||||||
|
|
||||||
|
this.pathTween = this.scene.sys.tweens.add(config);
|
||||||
|
|
||||||
// The starting point of the path, relative to this follower
|
// The starting point of the path, relative to this follower
|
||||||
|
|
||||||
|
// this.path.getStartPoint(this.pathOffset);
|
||||||
var start = this.path.getStartPoint();
|
var start = this.path.getStartPoint();
|
||||||
var diffX = this.pathOffset.x - start.x;
|
|
||||||
var diffY = this.pathOffset.y - start.y;
|
// console.log('getStartPoint', this.pathOffset.x, this.pathOffset.y);
|
||||||
|
console.log('getStartPoint2', this.x, this.y);
|
||||||
|
|
||||||
|
var diffX = this.x - start.x;
|
||||||
|
var diffY = this.y - start.y;
|
||||||
|
|
||||||
|
console.log('diffX', diffX, 'diffY', diffY);
|
||||||
|
|
||||||
this.pathOffset.set(diffX, diffY);
|
this.pathOffset.set(diffX, diffY);
|
||||||
|
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/*
|
||||||
|
playing: {
|
||||||
|
|
||||||
|
get: function ()
|
||||||
|
{
|
||||||
|
return this.pathData.playing;
|
||||||
|
},
|
||||||
|
|
||||||
|
set: function (value)
|
||||||
|
{
|
||||||
|
if (!value)
|
||||||
|
{
|
||||||
|
this.stop();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
preUpdate: function (time, delta)
|
preUpdate: function (time, delta)
|
||||||
{
|
{
|
||||||
this.anims.update(time, delta);
|
this.anims.update(time, delta);
|
||||||
|
|
||||||
if (!this.isFollowing)
|
if (this.pathTween && this.pathTween.isPlaying())
|
||||||
{
|
{
|
||||||
return;
|
this.path.getPoint(this.pathData.t, this.pathVector);
|
||||||
}
|
|
||||||
|
|
||||||
// Apply ease to pathT
|
this.pathVector.add(this.pathOffset);
|
||||||
delta *= this.timeScale;
|
|
||||||
|
|
||||||
this.elapsed += delta;
|
this.setPosition(this.pathVector.x, this.pathVector.y);
|
||||||
this.progress = Math.min(this.elapsed / this.duration, 1);
|
|
||||||
|
|
||||||
if (this.elapsed > this.duration)
|
|
||||||
{
|
|
||||||
var diff = this.elapsed - this.duration;
|
|
||||||
this.elapsed = this.duration;
|
|
||||||
}
|
|
||||||
|
|
||||||
var forward = true;
|
|
||||||
|
|
||||||
this.progress = this.elapsed / this.duration;
|
|
||||||
|
|
||||||
if (forward)
|
|
||||||
{
|
|
||||||
this.pathT = this.ease(this.progress);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
this.pathT = this.ease(1 - this.progress);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.path.getPoint(this.pathT, this.pathVector);
|
|
||||||
|
|
||||||
this.pathVector.add(this.pathOffset);
|
|
||||||
|
|
||||||
this.setPosition(this.pathVector.x, this.pathVector.y);
|
|
||||||
|
|
||||||
// this.setDepth(this.y);
|
|
||||||
|
|
||||||
if (this.progress === 1)
|
|
||||||
{
|
|
||||||
this.isFollowing = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue