-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.js
76 lines (64 loc) · 1.63 KB
/
main.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
import { makeMenu } from "./scenes/menu.js";
import { debugMode } from "./entities/debugMode.js";
import { makeWorld } from "./scenes/world.js";
import { makeBattle } from "./scenes/battle.js";
new p5((p) => {
let font;
const scenes = ["menu", "world", "battle"];
let currentScene = "menu";
function setScene(name) {
if (scenes.includes(name)) {
currentScene = name;
}
}
const menu = makeMenu(p);
const world = makeWorld(p, setScene);
const battle = makeBattle(p);
p.preload = () => {
font = p.loadFont("./assets/power-clear.ttf");
world.load();
menu.load();
battle.load();
};
p.setup = () => {
const canvasEl = p.createCanvas(512, 384, document.getElementById("game"));
// make canvas sharper temporarly
p.pixelDensity(3);
canvasEl.canvas.style = "";
p.textFont(font);
p.noSmooth(); // for pixels to not become blurry
world.setup();
battle.setup();
};
p.draw = () => {
switch (currentScene) {
case "menu":
menu.update();
menu.draw();
break;
case "world":
world.update();
world.draw();
break;
case "battle":
battle.update();
battle.draw();
break;
default:
}
debugMode.drawFpsCounter(p);
};
p.keyPressed = (keyEvent) => {
if (keyEvent.key === "Shift") {
debugMode.toggle();
}
if (keyEvent.keyCode === p.ENTER && currentScene === "menu")
setScene("world");
if (currentScene === "battle") battle.onKeyPressed(keyEvent);
};
p.keyReleased = () => {
if (currentScene === "world") {
world.keyReleased();
}
};
});