forked from amyjko/Gidget
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscenes.js
140 lines (84 loc) · 2.78 KB
/
scenes.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var SCENES = {
Sprite: function(url, width, height) {
this.image = GIDGET.ui.media.getImage(url);
this.width = width;
this.height = height;
},
Scene: function(length) {
this.length = length;
this.sprites = [];
this.sounds = [];
this.addSprite = function(sprite, time, coord) {
// time == (begin, end); coord = (x-pos, y-pos)
this.sprites.push({ sprite: sprite, time: time, coord: coord });
};
this.addSound = function(url, begin) {
this.sounds.push({ url: url, begin: begin});
},
this.draw = function(canvas, time) {
var ctx = canvas.getContext('2d');
var i;
var sprite, begin, end;
ctx.fillStyle = 'rgb(0,0,0)';
ctx.clearRect(0, 0, canvas.width, canvas.height);
for(i = 0; i < this.sounds.length; i++) {
if(this.sounds[i].begin <= time) {
GIDGET.ui.media.playSound(this.sounds[i].url);
this.sounds.splice(i, 1);
i--;
}
}
for(i = 0; i < this.sprites.length; i++) {
sprite = this.sprites[i].sprite;
begin = this.sprites[i].time[0];
end = this.sprites[i].time[1];
xpos = this.sprites[i].coord[0];
ypos = this.sprites[i].coord[1];
// If this is visible, draw the sprite.
if(begin < time && time < end) {
if(sprite.width > 0 && sprite.height > 0 && isDef(sprite.image))
ctx.drawImage(sprite.image, xpos, ypos, sprite.width, sprite.height);
}
}
};
},
Movie: function() {
this.scenes = [];
this.sceneStartTime = 0;
this.sceneTimeElapsed = undefined;
this.intervalID = undefined;
this.currentScene = undefined;
this.addScene = function(scene) {
this.scenes.push(scene);
};
this.step = function(canvas, whenDone) {
// What scene are we on?
var scene = this.scenes[this.currentScene];
// If this scene is over, move to the next scene
if(this.currentScene === 0 && this.sceneTimeElapsed === undefined) {
this.sceneStartTime = (new Date()).getTime();
this.sceneTimeElapsed = 0;
}
if(this.sceneTimeElapsed > scene.length) {
this.sceneStartTime = (new Date()).getTime();
this.sceneTimeElapsed = 0;
this.currentScene++;
}
// Update times
this.sceneTimeElapsed = (new Date()).getTime() - this.sceneStartTime;
// Draw the current scene.
scene.draw(canvas, this.sceneTimeElapsed);
// If this movie is over, end the animation
if(this.currentScene >= this.scenes.length) {
clearInterval(this.intervalID);
whenDone.call();
}
};
this.play = function(canvas, whenDone) {
this.sceneTimeElapsed = undefined;
this.currentScene = 0;
var thisMovie = this;
this.intervalID = setInterval(function() { thisMovie.step(canvas, whenDone); }, 40);
};
}
}