forked from mozilla/BrowserQuest
-
Notifications
You must be signed in to change notification settings - Fork 219
/
camera.js
83 lines (66 loc) · 2.32 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
define(function() {
var Camera = Class.extend({
init: function(renderer) {
this.renderer = renderer;
this.x = 0;
this.y = 0;
this.gridX = 0;
this.gridY = 0;
this.offset = 0.5;
this.rescale();
},
rescale: function() {
var factor = this.renderer.mobile ? 1 : 2;
this.gridW = 15 * factor;
this.gridH = 7 * factor;
log.debug("---------");
log.debug("Factor:"+factor);
log.debug("W:"+this.gridW + " H:" + this.gridH);
},
setPosition: function(x, y) {
this.x = x;
this.y = y;
this.gridX = Math.floor( x / 16 );
this.gridY = Math.floor( y / 16 );
},
setGridPosition: function(x, y) {
this.gridX = x;
this.gridY = y;
this.x = this.gridX * 16;
this.y = this.gridY * 16;
},
lookAt: function(entity) {
var r = this.renderer,
x = Math.round( entity.x - (Math.floor(this.gridW / 2) * r.tilesize) ),
y = Math.round( entity.y - (Math.floor(this.gridH / 2) * r.tilesize) );
this.setPosition(x, y);
},
forEachVisiblePosition: function(callback, extra) {
var extra = extra || 0;
for(var y=this.gridY-extra, maxY=this.gridY+this.gridH+(extra*2); y < maxY; y += 1) {
for(var x=this.gridX-extra, maxX=this.gridX+this.gridW+(extra*2); x < maxX; x += 1) {
callback(x, y);
}
}
},
isVisible: function(entity) {
return this.isVisiblePosition(entity.gridX, entity.gridY);
},
isVisiblePosition: function(x, y) {
if(y >= this.gridY && y < this.gridY + this.gridH
&& x >= this.gridX && x < this.gridX + this.gridW) {
return true;
} else {
return false;
}
},
focusEntity: function(entity) {
var w = this.gridW - 2,
h = this.gridH - 2,
x = Math.floor((entity.gridX - 1) / w) * w,
y = Math.floor((entity.gridY - 1) / h) * h;
this.setGridPosition(x, y);
}
});
return Camera;
});