-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
map.js
104 lines (86 loc) · 2.43 KB
/
map.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
99
100
101
102
103
104
var randomRGBA = require('./util/math').randomRGBA;
var locations = require('./locations.json');
var Location = require('./locations/location');
var AssetLoader = require('./asset_loader');
module.exports = Map;
function Map(game, width, height){
this.game = game;
this.width = width;
this.height = height;
this.image = null;
}
Map.prototype.generate = function(ticks){
var context = document.createElement('canvas').getContext('2d');
context.canvas.width = this.width;
context.canvas.height = this.height;
var size = 30;
var columns = this.width / size;
var rows = this.height / size;
/*
for (var x = 0, i = 0; i < columns; x+=size, i++){
for (var y = 0, j=0; j < rows; y+=size, j++){
context.fillStyle = randomRGBA(0, 255, 0, 255, 0, 255, 1);
context.fillRect(x, y, size, size);
}
}
*/
this.image = new Image();
this.image.src = context.canvas.toDataURL("image/png");
context = null;
}
// draw the map adjusted to camera
Map.prototype.draw = function(context, camera) {
context.drawImage(this.image, 0, 0, this.image.width, this.image.height, -camera.position.x, -camera.position.y, this.image.width, this.image.height);
}
Map.prototype.load = function(game, camera, filename) {
var map = this;
var data = {
images: [ "assets/setPiecesTSR.PNG" ],
frames: [
[3, 170, 81, 64, 0],
[86, 170, 81, 64, 0],
[168, 170, 81, 64, 0],
[250, 170, 81, 64, 0],
[250, 170, 81, 64, 0]
],
animations: {
Shop: 0,
Shelter: 1,
"Food Bank": 2,
Office: 3,
Jail: 4
}
};
this.assetLoader = new AssetLoader();
this.spritesheet = this.assetLoader.load(data);
map.locations = [];
map.spritesheet = this.spritesheet;
var assetLoader = new AssetLoader();
var data = {
images: [ "assets/setPiecesTSR.PNG" ],
frames: [
[3, 170, 81, 64, 0],
[86, 170, 81, 64, 0],
[168, 170, 81, 64, 0],
[250, 170, 81, 64, 0],
[250, 170, 81, 64, 0]
],
animations: {
Shop: 0,
Shelter: 1,
"Food Bank": 2,
Office: 3,
Jail: 4
}
};
spritesheet = assetLoader.load("assets/setPiecesTSR.PNG", data);
locations.forEach(function(location, index, array) {
location.game = game;
location.camera = camera;
location.map = map;
location.spritesheet = map.spritesheet;
var building = new Location(location);
building.addTo(game);
map.locations.push(building);
});
}