-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.js
91 lines (78 loc) · 2.31 KB
/
game.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
var Game = {};
(function(){
Game.Game = function(images,tileSize) {
this.imgs = images;
this.tileSize = tileSize;
var bspMap = Game.generateMap(60,60,3);
console.log(bspMap);
this.map = {
islands:this.createIslandSprites(bspMap.islands),
bridges:this.createBridgeSprites(bspMap.bridges)
};
};
Game.Game.prototype.createIslandSprites = function(islands) {
var sprites = [];
// FOR EACH ISLAND
for(i in islands) {
var island = islands[i];
// FOR EACH COLUMN AND ROW IN EACH ISLAND
for(var y=0,yL=island.h; y<yL; y++) {
for(var x=0,xL=island.w; x<xL; x++) {
sprites.push(new Game.Sprite(0,0,this.tileSize,this.tileSize,
(island.x+x)*this.tileSize,
(island.y+y)*this.tileSize,
this.tileSize,this.tileSize));
}
}
}
return sprites;
};
Game.Game.prototype.createBridgeSprites = function(bridges) {
var sprites = [];
for(var i=0,iL=bridges.length; i<iL; i++) {
var bridgeEnds = bridges[i];
if(bridgeEnds[0].x === bridgeEnds[1].x) {
// VERTICAL BRIDGE
for(var l=0,lL=bridgeEnds[1].y - bridgeEnds[0].y + 2; l<lL; l++) {
sprites.push(new Game.Sprite(16,0,this.tileSize,this.tileSize,
(bridgeEnds[0].x)*this.tileSize,
(bridgeEnds[0].y+l-1)*this.tileSize,this.tileSize,this.tileSize))
}
} else {
// HORIZONTAL BRIDGE
for(var l=0,lL=bridgeEnds[1].x - bridgeEnds[0].x + 2; l<lL; l++) {
sprites.push(new Game.Sprite(16,0,this.tileSize,this.tileSize,
(bridgeEnds[0].x+l-1)*this.tileSize,
(bridgeEnds[0].y)*this.tileSize,this.tileSize,this.tileSize))
}
}
}
return sprites;
}
Game.Game.prototype.render = function(draw) {
for(i in this.map.islands) {
this.map.islands[i].render(draw,this.imgs.ground);
}
for(i in this.map.bridges) {
this.map.bridges[i].render(draw,this.imgs.ground);
}
};
Game.Sprite = function(sx,sy,sw,sh,x,y,w,h) {
this.visible = true;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.sourceX = sx;
this.sourceY = sy;
this.sourceW = sw;
this.sourceH = sh;
};
Game.Sprite.prototype.render = function(draw,img) {
if(this.visible) {
draw.drawImage(img,this.sourceX,this.sourceY,
this.sourceW,this.sourceH,
this.x,this.y,this.w,this.h);
}
};
})();