-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
430 lines (373 loc) · 13.5 KB
/
app.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import { Entity } from "./Entity.js";
import { Game } from "./Game.js";
import { Renderer } from "./Renderer.js";
import { Vector2 } from "./Vectors.js";
import EnumeratedValue from "./EnumeratedValue.js";
// New supabase session
const supabaseUrl = "https://inyelmyxiphvbfgmhmrk.supabase.co";
const supabaseKey = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImlueWVsbXl4aXBodmJmZ21obXJrIiwicm9sZSI6ImFub24iLCJpYXQiOjE2OTc0Nzg3NzEsImV4cCI6MjAxMzA1NDc3MX0.9ByIuA4tv1oMmEr2UPAbCvNQYSvH-wY8aU-4Y8JSprg";
const s = supabase.createClient(supabaseUrl, supabaseKey);
// Creating the game window
const canvasWidth = 640;
const canvasHeight = 512;
const canvas = document.getElementById("canvas");
const app = new PIXI.Application(
{
view : canvas,
width : canvasWidth,
height : canvasHeight,
backgroundColor : 0xFFFFFF
});
app.stage.sortableChildren = true;
document.getElementById("Gam").appendChild(app.view);
// Stores the background image as a new sprite
const bkgTexture = PIXI.Texture.from("./images/tiles/default_bg.png");
let bkg = new PIXI.Sprite(bkgTexture);
bkg.texture.baseTexture.scaleMode = PIXI.SCALE_MODES.NEAREST;
bkg.setTransform(0, 0, 2, 2);
bkg.zIndex = -10;
app.stage.addChild(bkg);
// Load prefabs from .json file
const prefabs = await fetch('./prefabs.json')
.then((response) => response.json()).catch(error => console.error(error));
// Load our weapons from .json file
const masterList = await fetch('./Items/Weapons.json')
.then((response) => response.json()).catch(error => console.error(error));
// Load sidebar & assets, revised
const sheet = await PIXI.Assets.load('./images/sb/sidebar.json');
const sidebar = new PIXI.Sprite(sheet.textures['sidebar']);
sidebar.setTransform(512, 0, 2, 2);
sidebar.texture.baseTexture.scaleMode = PIXI.SCALE_MODES.NEAREST;
app.stage.addChild(sidebar);
const battleBkg = new PIXI.Sprite(PIXI.Texture.from('./images/sb/frutiger.png'));
battleBkg.setTransform(514, 2, 2, 2);
battleBkg.texture.baseTexture.scaleMode = PIXI.SCALE_MODES.NEAREST;
app.stage.addChild(battleBkg);
const invTxt = new PIXI.Sprite(sheet.textures['inv_text']);
invTxt.setTransform(512, 176, 2, 2);
app.stage.addChild(invTxt);
const invColumns = 3, invRows = 2, slotSize = 42;
for (let i = 0; i < invColumns; i++)
{
for (let j = 0; j < invRows; j++)
{
const slot = new PIXI.Sprite(sheet.textures['circle_slot']);
slot.setTransform(518 + i*slotSize, 212 + j*slotSize, 2, 2);
app.stage.addChild(slot);
}
}
const armorTxt = new PIXI.Sprite(sheet.textures['armor_text']);
armorTxt.setTransform(528, 292, 2, 2);
app.stage.addChild(armorTxt);
const armorSlot = new PIXI.Sprite(sheet.textures['square_slot_big']);
armorSlot.setTransform(544, 326, 2, 2);
app.stage.addChild(armorSlot);
const weaponTxt = new PIXI.Sprite(sheet.textures['weapon_text']);
weaponTxt.setTransform(512, 394, 2, 2);
app.stage.addChild(weaponTxt);
const weaponSlot = new PIXI.Sprite(sheet.textures['circle_slot_big']);
weaponSlot.setTransform(544, 432, 2, 2);
app.stage.addChild(weaponSlot);
// Pull all levels from the database
const roomData = [[]];
for (let i = 1; i < 16; i++)
{
const set = [];
const { data, error } = await s
.from('levels')
.select('*')
.eq('index', i);
data.forEach(item => {
set.push(JSON.parse(String(item.level_file)));
});
roomData.push(set);
}
const game = new Game(app.stage, roomData);
game.loadRoom();
game.deserializeEntity(prefabs.Player);
// game.deserializeEntity(prefabs.Wall);
const gameWindowTab = document.querySelector("#GameWindow");
const levelBuildTab = document.querySelector("#LevelBuilder");
document.addEventListener('keydown', function(input) {
if (gameWindowTab.classList.contains("active"))
{
game.broadcastToEntities({type:'keydown', key:input.key});
}
else if (levelBuildTab.classList.contains('active'))
{
if (selectedEntity != null && input.key == 'Delete')
{
deleteEntity(selectedEntity);
}
}
});
app.ticker.add((delta) => {
if (!gameWindowTab.classList.contains("active")) return;
game.updateEntities(delta);
});
/**
* Level Builder Stuff
*/
const lbcanvas = document.getElementById("levelBuilderCanvas");
const lbapp = new PIXI.Application(
{
view: lbcanvas,
width: canvasHeight,
height: canvasHeight,
backgroundColor: 0x000000,
}
)
lbapp.stage.sortableChildren = true;
document.getElementById("lbCanvasAnchor").appendChild(lbapp.view);
bkg = new PIXI.Sprite(bkgTexture);
bkg.texture.baseTexture.scaleMode = PIXI.SCALE_MODES.NEAREST;
bkg.setTransform(0, 0, 2, 2);
bkg.zIndex = -10;
lbapp.stage.addChild(bkg);
const levelBuilder = new Game(lbapp.stage);
const lbui = new PIXI.Graphics();
lbui.eventMode = 'static';
lbui.cursor = 'pointer';
lbui.zIndex = 1024;
lbapp.stage.addChild(lbui);
let selectedEntity = null;
lbapp.ticker.add((delta) => {
levelBuilder.renderEntities(delta);
lbui.clear();
if (selectedEntity != null)
{
lbapp.stage.on('pointerdown', onDragStart, lbui);
lbui.lineStyle(1, 0xFF0000, 1);
const t = selectedEntity.transform
// Draw bounding box
lbui.drawRect(t.position.x, t.position.y, 16 * t.scale.x, 16 * t.scale.y);
}
});
lbapp.stage.eventMode = 'static';
lbapp.stage.hitArea = lbapp.screen;
lbapp.stage.on('pointerup', onDragEnd);
lbapp.stage.on('pointerupoutside', onDragEnd);
const prefabButtons = document.querySelectorAll('.prefab-button');
for (const e of prefabButtons)
{
e.addEventListener('click', () => addToLevelBuilder(prefabs[e.textContent]));
}
function addToLevelBuilder(obj, rawAdd = false)
{
const entity = levelBuilder.deserializeEntity(obj);
if (selectedEntity != null && !rawAdd)
{
entity.transform.position = Vector2.add(selectedEntity.transform.position, {x:32, y:0});
if (entity.transform.position.x >= 512)
{
entity.transform.position.y += 32;
entity.transform.position.x = 0;
}
}
const eList = document.querySelector('#entityList');
const button = document.createElement('button');
button.textContent = entity.name;
button.setAttribute('id','id'+entity.getID());
button.setAttribute('class','entity-select dropbtn');
button.addEventListener('click', highlightEntity);
if (!rawAdd)
button.click();
eList.appendChild(button);
}
function highlightEntity()
{
clearEditorUI();
const newEntity = levelBuilder.getEntity((e) => e.getID() == this.id.substring(2));
if (selectedEntity != null)
{
if (newEntity.getID() == selectedEntity.getID())
{
deselectCurrentEntity();
return;
}
const previouslySelected = document.querySelector(`#id${selectedEntity.getID()}`);
previouslySelected.setAttribute('class','entity-select dropbtn');
}
this.setAttribute('class','entity-select dropbtn selected-entity');
selectedEntity = newEntity;
loadEditorUI(document.querySelector('.vars'), selectedEntity);
}
function deselectCurrentEntity()
{
document.querySelector(`#id${selectedEntity.getID()}`).setAttribute('class','entity-select dropbtn');
selectedEntity = null;
onDragEnd();
lbapp.stage.off('pointerdown', onDragStart, lbui);
}
function onDragMove(event)
{
let t = selectedEntity.transform;
selectedEntity.transform.position.x = Math.floor((event.global.x - 8 * t.scale.x) / levelBuilder.grid.cellSize) * levelBuilder.grid.cellSize;
selectedEntity.transform.position.y = Math.floor((event.global.y - 8 * t.scale.y) / levelBuilder.grid.cellSize) * levelBuilder.grid.cellSize;
}
function onDragStart()
{
lbapp.stage.on('pointermove', onDragMove);
clearEditorUI();
}
function onDragEnd()
{
lbapp.stage.off('pointermove', onDragMove);
clearEditorUI();
loadEditorUI(document.querySelector('.vars'), selectedEntity);
}
function loadEditorUI(parent, entity, indent=0)
{
for (let prop in entity)
{
if (prop.charAt(0) != '_')
continue;
const newElement = document.createElement(`h${indent+3}`);
newElement.textContent = prop.substring(1);
const val = entity[prop];
if (val instanceof EnumeratedValue)
{
const dropdown = document.createElement('div');
dropdown.setAttribute('class', 'dropdown');
const dropbtn = document.createElement('button');
dropbtn.style.width = "100%";
dropbtn.textContent = val.selected;
dropbtn.setAttribute('class', 'dropbtn');
const dropcontent = document.createElement('div');
dropcontent.setAttribute('class', 'dropdown-content-down');
for (const v in val.map)
{
const option = document.createElement('button');
option.setAttribute('class', 'dropbtn');
option.textContent = v;
option.addEventListener('click', () => {
val.value = option.textContent;
dropbtn.textContent = option.textContent;
const e = selectedEntity;
deselectCurrentEntity();
document.querySelector(`#id${e.getID()}`).click();
});
dropcontent.appendChild(option);
}
parent.appendChild(newElement);
dropdown.appendChild(dropbtn);
dropdown.appendChild(dropcontent);
parent.appendChild(dropdown);
}
else if (val instanceof Object)
{
loadEditorUI(newElement, entity[prop], indent+1);
parent.appendChild(newElement);
}
else
{
const div = document.createElement('div');
const form = document.createElement('input');
form.setAttribute('type', val instanceof Number ? 'number' : 'text');
form.value = val;
form.addEventListener('input', () => {updateEntityValues(entity, prop, form);});
div.appendChild(newElement);
div.appendChild(form);
parent.appendChild(div);
}
}
}
function updateEntityValues(object, propertyName, inputSource)
{
object[propertyName] = inputSource.value;
if (object instanceof Entity && propertyName == '_Name')
{
document.querySelector(`#id${selectedEntity.getID()}`).textContent = inputSource.value;
}
if (object instanceof Renderer && propertyName == '_CurrentAnimation')
{
selectedEntity.renderer.setAnimation(inputSource.value, 1/4, true);
}
}
function clearEditorUI()
{
document.querySelector('.vars').replaceChildren();
}
function deleteEntity(entity)
{
if (entity == selectedEntity)
deselectCurrentEntity();
document.querySelector(`#id${entity.getID()}`).remove();
entity.destroy();
}
document.getElementById("Bui").addEventListener('loadLevel', (e) => {
let entities = levelBuilder.getAllEntities();
while (entities.length > 0)
{
for (const entity of entities)
{
deleteEntity(entity);
}
entities = levelBuilder.getAllEntities();
}
for (const obj of e.detail.level_obj)
{
addToLevelBuilder(obj, true);
}
document.getElementById("levelName").value = e.detail.level_name;
});
/* Event listener for saving levels to supabase */
document.getElementById("saveLevel").addEventListener("click", async (e) => {
e.preventDefault();
// Integer that represents max length allowed for a level name
let maxNameSize = 15;
// Get the level name text box
const levelNameTextBox = document.getElementById("levelName");
// Check to see if there is a name in the text box
if (levelNameTextBox.value == 0) {
// Textbox is empty, make user enter a name
alert("Error: No name provided for the level.")
levelNameTextBox.style.backgroundColor = "#E3963E";
return;
}
// Check to see if name exceeds maximum length
if (levelNameTextBox.value.trim().length > maxNameSize) {
// Name is too long. Make user enter a new name
alert("Error: Level name cannot be longer than 15 characters. Please enter a new name.");
levelNameTextBox.style.backgroundColor = "#E3963E";
return;
}
// Get the user's level name
const level_name = levelNameTextBox.value.trim();
// Get the user's username
const user = await s.auth.getUser();
var username = JSON.stringify(user.data.user.user_metadata.username);
await removeLevel(username, level_name);
// Call the saveRoom() function to get the level file and index
const levelObject = levelBuilder.saveRoom();
// Insert data into Supabase
const { data, error } = await s.from('levels').insert([
{
username: username,
level_file: levelObject.level_file,
index: levelObject.index,
level_name: level_name,
published: false,
},
])
if (error) {
// Error saving to database
alert("Error: There was an error saving your level. Please retry.");
return;
}
else {
// Level was successfully added
alert("Level saved!");
// Change text box color back to original (if necessary)
levelNameTextBox.style.backgroundColor = '';
return;
}
});
async function removeLevel(username, levelname)
{
const { data, error } = await s
.from('levels')
.delete()
.eq('username', username)
.eq('level_name', levelname);
}