forked from RatoGBM/Laserpointer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel.js
More file actions
99 lines (95 loc) · 2.75 KB
/
Copy pathlevel.js
File metadata and controls
99 lines (95 loc) · 2.75 KB
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
let wall_textures = [];
class Level {
static textures = [];
static scale = 2;
constructor(level_str) {
let rows = level_str.trim().split("\n");
// load level map
this.level = new Array();
for (let r of rows) {
this.level.push(Array.from(r));
}
this.level.reverse();
//
this.width = this.level[0].length;
this.height = this.level.length;
this.x_offset = Math.round(this.width / 2);
this.y_offset = Math.round(this.height / 2);
this.generateWalls();
this.positionCharacters();
}
getTile(x, y) {
if (x >= this.width || y >= this.height || x < 0 || y < 0) {
return "+"
}
// x = x >= this.width ? this.width - 1 : x;
// x = x < 0 ? 0 : x;
// y = y >= this.height ? this.height - 1 : y;
// y = y < 0 ? 0 : y;
if (x < 0) {
x = this.width - x;
};
if (y < 0) {
y = this.height - y;
};
return this.level[y][x];
}
isWall(x, y) {
let r = this.getTile(x, y) == "W";
return r;
}
getTexture(x, y) {
return Level.getTexture(this, x, y);
}
generateWalls() {
for (let y = 0; y < this.height; y++) {
for (let x = 0; x < this.width; x++) {
if (this.isWall(x, y)) {
new Wall(vec2(x - this.x_offset, y - this.y_offset).scale(Level.scale), vec2(1, 1).scale(Level.scale), this.getTexture(x, y))
}
}
}
}
positionCharacters() {
for (let y = 0; y < this.height; y++) {
for (let x = 0; x < this.width; x++) {
let pos = vec2(x - this.x_offset, y - this.y_offset).scale(Level.scale);
if (this.getTile(x, y) == "C") {
cat.pos = pos;
}
if (this.getTile(x, y) == "M") {
mouse.pos = pos;
}
if (this.getTile(x, y) == "G") {
cheese.pos = pos;
}
}
}
}
static getTexture(level, x, y) {
let index = 0;
index += (1 * level.isWall(x, y + 1));
index += (1 * level.isWall(x + 1, y) << 1);
index += (1 * level.isWall(x, y - 1) << 2);
index += (1 * level.isWall(x - 1, y) << 3);
return Level.textures[index];
}
static loadWallTextures() {
for (let y = 0.2; y < 64; y += 16) {
for (let x = 192.2; x < 256; x += 16) {
Level.textures.push(new TileInfo(vec2(x, y), vec2(15.6, 15.6)));
}
}
}
}
///////////////LEVELS///////////////
const levels = [
`
WWWWWWWWW
W++W++C+W
W+++++++W
WM++WW++W
W+++WW+GW
WWWWWWWWW
`,
]