/
sprite.js
90 lines (72 loc) · 2.29 KB
/
sprite.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
define(function(require) {
var resources = require('./resources');
function Sprite(url, pos, size, speed, frames, dir, once) {
this.pos = pos;
this.size = size;
this.speed = typeof speed === 'number' ? speed : 0;
this.frames = frames;
this._index = 0;
this.url = url;
this.dir = dir || 'horizontal';
this.once = once;
};
Sprite.prototype = {
flipHorizontal: function(val) {
this.flipHoriz = val;
},
setOffset: function(offset) {
this.offset = offset;
},
randomize: function() {
if(this.speed > 0) {
this._index += Math.random() * this.frames.length * .2;
}
return this;
},
clone: function() {
return new Sprite(this.url,
[this.pos[0], this.pos[1]],
[this.size[0], this.size[1]],
this.speed,
this.frames && this.frames.slice(),
this.dir,
this.once);
},
update: function(dt) {
this._index += this.speed*dt;
},
render: function(ctx) {
var frame;
if(this.speed > 0) {
var max = this.frames.length;
var idx = Math.floor(this._index);
frame = this.frames[idx % max];
if(this.once && idx >= max) {
this.done = true;
return;
}
}
else {
frame = 0;
}
var x = this.pos[0];
var y = this.pos[1];
if(this.dir == 'vertical') {
y += frame * this.size[1];
}
else {
x += frame * this.size[0];
}
var dest = this.offset || [0, 0];
if(this.flipHoriz) {
ctx.scale(-1, 1);
}
ctx.drawImage(resources.get(this.url),
x, y,
this.size[0], this.size[1],
dest[0], dest[1],
this.size[0], this.size[1]);
}
};
return Sprite;
});