-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTileFeild.js
95 lines (85 loc) · 2.41 KB
/
TileFeild.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
function TileField(level) {
powerupjs.GameObjectGrid.call(this, 14, 8, ID.layer_objects_1, ID.tiles);
this.cellWidth = 64;
this.cellHeight = 64;
this.levelIndex = level;
this.rows = levelLayout.length;
this.columns = levelLayout[0].length;
this.loadTiles();
}
TileField.prototype = Object.create(powerupjs.GameObjectGrid.prototype);
TileField.prototype.update = function (delta) {
powerupjs.GameObjectGrid.prototype.update.call(this, delta);
};
TileField.prototype.loadTiles = function () {
var data;
if (localStorage.tileInfo === undefined) {
var tiles = levelLayout;
for (var y = 0; y < tiles.length; y++) {
for (var x = 0; x < tiles[0].length; x++) {
var t = this.addTile(tiles[y][x], x, y);
this.addAt(t, x, y);
}
}
var infoArray = "";
var string = ''
for (var l=0; l < 2; l++) {
for (var i = 0; i < this.gridLength; i++) {
var tile = this.gameObjects[i];
var str =
tile.blockType +
"/" +
tile.color +
"/" +
tile.type +
"/" +
tile.index.x +
"/" +
tile.index.y +
"/" +
tile.sheetIndex;
infoArray += str + ",";
}
string += l + ":" + infoArray + ":";
localStorage.tileInfo = string
}
}
data = localStorage.tileInfo.split(":");
var tileInfo = data[(this.levelIndex + 1) * 2 - 1].split(',');
for (var i = 0; i < this.gridLength; i++) {
var info = tileInfo[i].split("/");
if (info[0] !== "undefined") {
var t = new Tile(
info[2],
new powerupjs.Vector2(parseInt(info[3]), parseInt(info[4])),
sprites.tiles[info[0].replace(/["]/g, '')][info[1].replace(/["]/g, '')],
parseInt(info[5]),
info[0].replace(/["]/g, ''),
info[1].replace(/["]/g, '')
);
} else {
var t = new Tile(
info[2],
new powerupjs.Vector2(parseInt(info[3]), parseInt(info[4]))
);
}
this.addAt(t, parseInt(info[3]), parseInt(info[4]));
}
};
TileField.prototype.addTile = function (type, x, y) {
switch (type) {
case "#":
return new Tile(
TileType.normal,
new powerupjs.Vector2(x, y),
sprites.tiles["blocks"].grey,
4,
"blocks",
"grey"
);
case ".":
return new Tile(TileType.background, new powerupjs.Vector2(x, y));
default:
return new Tile(TileType.background, new powerupjs.Vector2(x, y));
}
};