-
Notifications
You must be signed in to change notification settings - Fork 0
/
level.js
104 lines (95 loc) · 2.96 KB
/
level.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
let level;
var Tile = {
None: -1,
Empty: 0,
Wall: 1,
Crate: 2,
Slot: 3,
Start: 4,
SlotCrate: 5,
};
let map = new Array();
tileXSize = 40;
tileYSize = 40;
margin = 5;
map[0] = "~~~~#####~~~~~~~~~~";
map[1] = "~~~~# #~~~~~~~~~~";
map[2] = "~~~~#$ #~~~~~~~~~~";
map[3] = "~~### $##~~~~~~~~~";
map[4] = "~~# $ $ #~~~~~~~~~";
map[5] = "### # ## #~~~######";
map[6] = "# # ## ##### ..#";
map[7] = "# $ $ ..#";
map[8] = "##### ### #@## ..#";
map[9] = "~~~~# #########";
map[10] = "~~~~#######~~~~~~~~";
function createLevelFromMap(minos) {
level = new Array();
levelHeight = map.length;
for (i = 0; i < levelHeight; i++) {
level[i] = new Array();
levelWidth = map[i].length;
for (j = 0; j < levelWidth; j++) {
if (map[i].charAt(j) == "#") {
level[i][j] = Tile.Wall;
} else if(map[i].charAt(j) == "~") {
level[i][j] = Tile.None;
} else if(map[i].charAt(j) == "$") {
level[i][j] = Tile.Crate;
} else if(map[i].charAt(j) == ".") {
level[i][j] = Tile.Slot;
} else if(map[i].charAt(j) == "!") {
level[i][j] = Tile.Sign;
} else if(map[i].charAt(j) == "@") {
level[i][j] = Tile.Start;
minos.x = j;
minos.y = i;
minos.direction = Direction.Up;
} else {
level[i][j] = Tile.Empty;
}
}
}
}
var canvasLevel = document.createElement("canvas"),
levelContext = canvasLevel.getContext("2d");
canvasLevel.width = map[0].length * tileXSize;
canvasLevel.height = map.length * tileYSize;
levelContext.strokeStyle = "#FFFFFF";
function drawMap() {
for (i = 0; i < map.length; i++) {
for (j = 0; j < map[i].length; j++) {
drawTile(j, i);
}
}
canvasContext.drawImage(levelContext.canvas, 0, 0);
}
function drawTile(x, y) {
switch(level[y][x]){
case Tile.None:
drawRectangle(x * tileXSize, y * tileYSize, tileXSize, tileYSize, "#F6F6F6");
break;
case Tile.Empty:
case Tile.Start:
//drawRectangle(x * tileXSize, y * tileYSize, tileXSize, tileYSize, "#FFFFFF");
levelContext.drawImage(tileset, 120, 160, 40, 40, x * tileXSize, y * tileYSize, tileXSize, tileYSize);
break;
case Tile.Wall:
//levelContext.drawImage(tileset, 120, 0, 40, 40, x * tileXSize, y * tileYSize, tileXSize, tileYSize);
drawRectangle(x * tileXSize, y * tileYSize, tileXSize, tileYSize, "#000000");
break;
case Tile.Crate:
levelContext.drawImage(tileset, 120, 40, 40, 40, x * tileXSize, y * tileYSize, tileXSize, tileYSize);
break;
case Tile.Slot:
levelContext.drawImage(tileset, 120, 80, 40, 40, x * tileXSize, y * tileYSize, tileXSize, tileYSize);
break;
case Tile.SlotCrate:
levelContext.drawImage(tileset, 120, 120, 40, 40, x * tileXSize, y * tileYSize, tileXSize, tileYSize);
break;
}
}
function drawRectangle(x, y, w, h, c) {
levelContext.fillStyle = c;
levelContext.fillRect(x, y, w, h);
}