-
Notifications
You must be signed in to change notification settings - Fork 2
/
scroll.js
85 lines (71 loc) · 1.8 KB
/
scroll.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { Script } from 'playcanvas';
export class Scroll extends Script {
/**
* @attribute
*/
startEvent = 'start'
/**
* @attribute
*/
stopEvent = 'stop'
/**
* @attribute
*/
resetEvent = 'reset'
/**
* @attribute
*/
cycleEvent = 'cycle'
/**
* @attribute
*/
startX = 1
/**
* @attribute
*/
endX = 1
/**
* @attribute
*/
speed = 1
/**
* @attribute
*/
frozen = false
initialize() {
var app = this.app;
this.paused = false;
this.initialPos = this.entity.getPosition().clone();
this.initialRot = this.entity.getRotation().clone();
app.on(this.resetEvent, function () {
this.entity.setPosition(this.initialPos);
this.entity.setRotation(this.initialRot);
}, this);
app.on(this.startEvent, function () {
this.frozen = false;
}, this);
app.on(this.stopEvent, function () {
this.frozen = true;
}, this);
app.on('game:pause', function () {
this.paused = true;
}, this);
app.on('game:unpause', function () {
this.paused = false;
}, this);
};
// update code called every frame
update(dt) {
var app = this.app;
if (!this.frozen && !this.paused) {
this.entity.translateLocal(this.speed * dt / (1/60), 0, 0);
// Check to see if we've scrolled beyond the window...
var pos = this.entity.getLocalPosition();
if (pos.x < this.endX) {
// Translate back to the start
this.entity.translateLocal(this.startX - this.endX, 0, 0);
app.fire(this.cycleEvent);
}
}
}
}