-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
174 lines (147 loc) · 3.74 KB
/
game.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
import kaboom from "./node_modules/kaboom/dist/kaboom.mjs";
import { LEVELS } from "./levels/levelsDef";
import { createLevelsConfig } from "./levels/levelsConf";
import loadAllSprites from "./utils/loadAllSprites.js";
kaboom({
background: [134, 135, 247],
fullscreen: true,
global: true,
scale: 1.5,
debug: true,
});
loadAllSprites();
const SPEED = 120;
const levelConf = createLevelsConfig();
scene("game", (levelNumber = 0) => {
layers(["bg", "game", "ui"], "game");
const gameLevel = addLevel(LEVELS[levelNumber], levelConf);
// Add background elements
let cloudX = 20;
while (cloudX < 1000) {
add([sprite("cloud"), pos(cloudX, 50), layer("bg")]);
cloudX += 200;
}
// add([sprite("cloud"), pos(20, 50), layer("bg")]);
add([sprite("hill"), pos(32, 208), layer("bg"), origin("bot")]);
add([sprite("shrubbery"), pos(200, 208), layer("bg"), origin("bot")]);
// Add UI Layer
// add([
// text("Level " + 1, { size: 24 }),
// pos(vec2(160, 30)),
// color(255, 255, 255),
// origin("center"),
// layer("ui"),
// lifespan(1, { fade: 0.5 }),
// ]);
// const score = 0;
// const scoreLabel = add([
// text("Score: " + score, { size: 20 }),
// pos(500, 6),
// layer("ui"),
// { value: score },
// ]);
// Add Mario
const player = gameLevel.spawn("p", 1, 5);
let canSquash = false;
// #region Player key press functions
onKeyDown("right", () => {
if (player.isFrozen) return;
player.flipX(false);
player.move(SPEED, 0);
});
onKeyDown("left", () => {
if (player.isFrozen) return;
player.flipX(true);
if (toScreen(player.pos).x > 20) {
player.move(-SPEED, 0);
}
});
onKeyPress("space", () => {
if (player.isAlive && player.isGrounded()) {
player.jump();
canSquash = true;
}
});
// #endregion
// #region Player action functions
player.onUpdate(() => {
// center camera to player
const currCam = camPos();
if (currCam.x < player.pos.x) {
camPos(player.pos.x, currCam.y);
}
if (player.isAlive && player.isGrounded()) {
canSquash = false;
}
if (player.pos.y > height() - 16) {
killed();
}
});
player.onCollide("badGuy", (baddy) => {
if (baddy.isAlive === false || player.isAlive === false) {
return;
}
if (canSquash) {
// Mario has jumped on the bad guy:
baddy.squash();
} else {
if (player.isBig) {
player.smaller();
baddy.changeDir();
} else {
killed();
}
}
});
player.onHeadbutt((obj) => {
if (obj.is("questionBox")) {
if (obj.is("coinBox")) {
let coin = gameLevel.spawn("c", obj.gridPos.sub(0, 1));
coin.bump();
} else if (obj.is("mushyBox")) {
gameLevel.spawn("M", obj.gridPos.sub(0, 1));
}
var pos = obj.gridPos;
destroy(obj);
var box = gameLevel.spawn("!", pos);
box.bump();
}
});
player.onCollide("bigMushy", (mushy) => {
destroy(mushy);
player.bigger();
});
player.onCollide("castle", (castle, side) => {
player.freeze();
add([
text("Well Done!", { size: 24 }),
pos(toWorld(vec2(160, 120))),
color(255, 255, 255),
origin("center"),
layer("ui"),
]);
wait(1, () => {
let nextLevel = levelNumber + 1;
if (nextLevel >= LEVELS.length) {
nextLevel = 0;
}
go("game", nextLevel);
});
});
function killed() {
if (player.isAlive === false) {
return;
}
player.die();
add([
text("Game Over :(", { size: 24 }),
pos(toWorld(vec2(160, 120))),
color(255, 255, 255),
origin("center"),
layer("ui"),
]);
}
// #endregion
});
go("game");
/* HELPER FUNCTIONS */