-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmap.js
More file actions
218 lines (181 loc) · 7.72 KB
/
map.js
File metadata and controls
218 lines (181 loc) · 7.72 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { GameState } from "./gamestate.js";
import { enemy } from "./enemy.js";
export function map(p) {
return {
mapData: null,
tilesetImage: null,
tileWidth: 16,
tileHeight: 16,
mapWidth: null,
mapHeight: null,
mapPixelWidth: null,
mapPixelHeight: null,
tiles: [],
tilesetsInfo: [],
wallChunks: [],
state: new GameState(p),
btnref: p.createButton("Begin Quest"),
GDLbtn: p.createButton("Greg DeLozier Mode"),
levelLoaded: false,
loadOnce: false,
Enemy: new enemy(p),
notMenu: false,
preload() {
this.Enemy.preload();
this.tilesetsInfo.push(p.loadImage('assets/Maps/Dungeon tileset.png'));
if (this.state.getState() == 'menu') {
this.btnref.style("background-color", "#ff5959");
this.GDLbtn.style("background-color", "#ff5959");
}
},
setup() {
this.Enemy.setup();
this.mapWidth = this.mapData.width;
this.mapHeight = this.mapData.height;
this.mapPixelWidth = this.mapWidth * this.tileWidth;
this.mapPixelHeight = this.mapHeight * this.tileHeight;
if (this.state.getState() == 'level1') {
this.parseLayers();
}
},
isWallTile(tileX, tileY) {
// Iterate through all wall chunks
for (let chunk of this.wallChunks) {
// Calculate the boundaries of this chunk
const chunkStartX = chunk.x;
const chunkEndX = chunk.x + chunk.width;
const chunkStartY = chunk.y;
const chunkEndY = chunk.y + chunk.height;
// Check if the given tile coordinates are within the range of the current chunk
if (tileX >= chunkStartX && tileX < chunkEndX && tileY >= chunkStartY && tileY < chunkEndY) {
// If so, calculate the index of the tile in the chunk data array
const index = (tileY - chunk.y) * chunk.width + (tileX - chunk.x);
// Check if the tile ID at this position is not 0 (assuming ID 0 represents no wall)
if (chunk.data[index] !== 0) {
return true; // It's a wall tile
}
}
}
return false; // not a wall tile
},
preloadLevel1() {
this.mapData = p.loadJSON('assets/Maps/level_1.json');
this.tilesetsInfo.push(p.loadImage('assets/Maps/Dungeon tileset.png'));
},
async draw(w) {
if (this.state.getState() == 'menu') {
w.disable()
p.background("#5cb8ff");
p.textSize(30);
p.textAlign(p.CENTER);
var txtref = p.text("The Wizard's Quest", 50, -50);
// change the size of the text for the text box
p.textSize(10);
// explanation of the game
var txtref2 = p.text("Objective: Collect 50 coins before the enemies get you!", 50, -20);
this.btnref.position(p.width / 2, p.height / 2);
this.GDLbtn.position(p.width / 2 - 22, p.height / 2 + 50);
this.GDLbtn.style("border-radius", "20%");
this.GDLbtn.style("padding-top", "10px");
this.GDLbtn.style("padding-bottom", "10px");
this.btnref.style("border-radius", "20%");
this.btnref.style("padding-top", "10px");
this.btnref.style("padding-bottom", "10px");
this.btnref.mouseOver(() => {
this.btnref.style("background-color", "white");
});
this.btnref.mouseOut(() => {
this.btnref.style("background-color", "#ff5959");
});
this.GDLbtn.mouseOver(() => {
this.GDLbtn.style("background-color", "white");
});
this.GDLbtn.mouseOut(() => {
this.GDLbtn.style("background-color", "#ff5959");
});
this.btnref.mouseClicked(() => {
this.btnref.remove();
this.GDLbtn.remove();
w.enable();
this.state.changeState(1);
this.notMenu = true;
});
this.GDLbtn.mouseClicked(() => {
this.GDLbtn.remove();
this.btnref.remove();
w.enable();
this.state.changeState(1);
w.GDLMode = true;
this.notMenu = true;
});
} else {
if (!this.levelLoaded) {
this.mapData = await this.preloadLevel("assets/Maps/level_1.json");
this.setup();
this.levelLoaded = true;
} else if (!this.loadOnce) {
this.parseLayers();
this.loadOnce = true;
} else {
p.background("#666666");
let scaleAmount = 1;
this.tiles.forEach(tile => {
p.image(
tile.img,
tile.dx * scaleAmount,
tile.dy * scaleAmount,
this.tileWidth * scaleAmount,
this.tileHeight * scaleAmount,
tile.sx,
tile.sy,
this.tileWidth,
this.tileHeight
);
});
}
}
},
preloadLevel(fileRef) {
// returns a 'promise' to resolve loadJSON.
// Due to loadJSON having to parse a large file, it needs to be asynchronous,
// and needs to return a promise to fulfill the variable, rather than just the variable.
return new Promise((resolve) => {
resolve(p.loadJSON(fileRef));
});
},
parseLayers() {
var tmpTileset = this.tilesetsInfo[0];
this.mapData.layers.forEach(layer => {
let isWallLayer = layer.name === 'wall';
if (layer.type === 'tilelayer') {
layer.chunks.forEach(chunk => {
if (isWallLayer) {
this.wallChunks.push({
x: chunk.x,
y: chunk.y,
width: chunk.width,
height: chunk.height,
data: chunk.data,
});
}
for (let y = 0; y < chunk.height; ++y) {
for (let x = 0; x < chunk.width; ++x) {
let tile = chunk.data[y * chunk.width + x];
if (tile !== 0) {
this.tiles.push({
img: tmpTileset,
sx: ((tile - 1) % 24) * this.tileHeight,
sy: (Math.floor((tile - 1) / 24)) * this.tileHeight,
dx: (chunk.x + x) * this.tileWidth,
dy: (chunk.y + y) * this.tileHeight,
});
}
}
}
});
}
}
);
},
}
};