|
| 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; |
0 commit comments