-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.js
67 lines (55 loc) · 1.67 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
var collisionChecker = require('./collisionChecker.js');
var gameArea = require('./canvas.js');
function camera(x, y, c, canvas){
this.x = x,
this.y = y,
this.width = 1280,
this.height = 720,
this.gameArea = canvas;
this.collisionChecker = new collisionChecker();
//Update the cameras position
this.update = function(dt, focus, map){
//focus the camera on the player
if(focus.x > 4 * 64 && focus.x < map[0].length * 64 - this.width + 4 * 64){
this.x = focus.x - 4 * 64;
}
if(this.y + this.height < map.length * 64){
this.y = focus.y - this.height / 2;
}
}
//Render the game with list of objects to render
this.render = function(arr, background){
var ctx = this.gameArea.context;
//draw background
ctx.drawImage(background, 0, 0);
for(var i = 0; i < arr.length; i++){
var item = arr[i];
//Check if the object is in view
if(this.collisionChecker.check(this, item)){
var img = item.img;
var sx = item.imgSrcX;
var sy = item.imgSrcY;
var sw = item.imgWidth;
var sh = item.imgHeight;
var dx = item.imgX - this.x;
var dy = item.imgY - this.y;
var dw = item.imgWidth;
var dh = item.imgHeight;
//draw to the canvas
if(item.img){
ctx.drawImage(img, sx, sy, sw, sh, dx, dy, dw, dh);
}
else{
ctx.fillStyle = item.col;
ctx.fillRect(item.x - this.x, item.y - this.y,item.width, item.height);
}
//uncomment the belowto see bounding boxes
//ctx.fillStyle = item.col;
//ctx.fillRect(item.x-this.x, item.y-this.y, item.width, item.height);
//if(item.img){ctx.drawImage(img,sx,sy,sw,sh,dx,dy,dw,dh);}
//else{console.log('no img');}
}
}
}
}
module.exports = camera;