-
Notifications
You must be signed in to change notification settings - Fork 18
/
Camera.js
98 lines (67 loc) · 1.43 KB
/
Camera.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
(function (Ω) {
"use strict";
var Camera = Ω.Class.extend({
x: 0,
y: 0,
w: 0,
h: 0,
debug: false,
init: function (x, y, w, h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.zoom = 1;
},
tick: function () {},
moveTo: function (x, y) {
this.x = x;
this.y = y;
},
moveBy: function (x, y) {
this.x += x;
this.y += y;
},
renderPre: function (gfx) {
var c = gfx.ctx;
c.save();
c.scale(this.zoom, this.zoom);
c.translate(-(Math.round(this.x)), -(Math.round(this.y)));
},
renderPost: function (gfx) {
var c = gfx.ctx;
if (this.debug) {
c.strokeStyle = "red";
c.strokeRect(this.x, this.y, this.w / this.zoom, this.h / this.zoom);
}
c.restore();
},
render: function (gfx, renderables, noPrePost) {
var self = this;
!noPrePost && this.renderPre(gfx);
renderables
// Flatten to an array
.reduce(function (ac, e) {
if (Array.isArray(e)) {
return ac.concat(e);
}
ac.push(e);
return ac;
}, [])
// Remove out-of-view entites
.filter(function (r) {
return r.repeat || !(
r.x + r.w < self.x ||
r.y + r.h < self.y ||
r.x > self.x + (self.w / self.zoom) ||
r.y > self.y + (self.h / self.zoom));
})
// Draw 'em
.forEach(function (r) {
r.render(gfx, self);
});
!noPrePost && this.renderPost(gfx);
}
});
Ω.Camera = Camera;
}(window.Ω));