-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathrpg.js
207 lines (190 loc) · 4.75 KB
/
rpg.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
// @ts-check
// simple rpg style walk and talk
kaplay({
background: [74, 48, 82],
});
loadSprite("bag", "/sprites/bag.png");
loadSprite("ghosty", "/sprites/ghosty.png");
loadSprite("grass", "/sprites/grass.png");
loadSprite("steel", "/sprites/steel.png");
loadSprite("door", "/sprites/door.png");
loadSprite("key", "/sprites/key.png");
loadSprite("bean", "/sprites/bean.png");
scene("main", (levelIdx) => {
const SPEED = 320;
// character dialog data
const characters = {
"a": {
sprite: "bag",
msg: "Hi Bean! You should get that key!",
},
"b": {
sprite: "ghosty",
msg: "Who are you? You can see me??",
},
};
// level layouts
const levels = [
[
"===|====",
"= =",
"= $ =",
"= a =",
"= =",
"= @ =",
"========",
],
[
"--------",
"- -",
"- $ -",
"| -",
"- b -",
"- @ -",
"--------",
],
];
const level = addLevel(levels[levelIdx], {
tileWidth: 64,
tileHeight: 64,
pos: vec2(64, 64),
tiles: {
"=": () => [
sprite("grass"),
area(),
body({ isStatic: true }),
anchor("center"),
],
"-": () => [
sprite("steel"),
area(),
body({ isStatic: true }),
anchor("center"),
],
"$": () => [
sprite("key"),
area(),
anchor("center"),
"key",
],
"@": () => [
sprite("bean"),
area(),
body(),
anchor("center"),
"player",
],
"|": () => [
sprite("door"),
area(),
body({ isStatic: true }),
anchor("center"),
"door",
],
},
// any() is a special function that gets called everytime there's a
// symbole not defined above and is supposed to return what that symbol
// means
wildcardTile(ch) {
const char = characters[ch];
if (char) {
return [
sprite(char.sprite),
area(),
body({ isStatic: true }),
anchor("center"),
"character",
{ msg: char.msg },
];
}
},
});
// get the player game obj by tag
const player = level.get("player")[0];
function addDialog() {
const h = 160;
const pad = 16;
const bg = add([
pos(0, height() - h),
rect(width(), h),
color(0, 0, 0),
z(100),
]);
const txt = add([
text("", {
width: width(),
}),
pos(0 + pad, height() - h + pad),
z(100),
]);
bg.hidden = true;
txt.hidden = true;
return {
say(t) {
txt.text = t;
bg.hidden = false;
txt.hidden = false;
},
dismiss() {
if (!this.active()) {
return;
}
txt.text = "";
bg.hidden = true;
txt.hidden = true;
},
active() {
return !bg.hidden;
},
destroy() {
bg.destroy();
txt.destroy();
},
};
}
let hasKey = false;
const dialog = addDialog();
player.onCollide("key", (key) => {
destroy(key);
hasKey = true;
});
player.onCollide("door", () => {
if (hasKey) {
if (levelIdx + 1 < levels.length) {
go("main", levelIdx + 1);
}
else {
go("win");
}
}
else {
dialog.say("you got no key!");
}
});
// talk on touch
player.onCollide("character", (ch) => {
dialog.say(ch.msg);
});
const dirs = {
"left": LEFT,
"right": RIGHT,
"up": UP,
"down": DOWN,
};
for (const dir in dirs) {
onKeyPress(dir, () => {
dialog.dismiss();
});
onKeyDown(dir, () => {
player.move(dirs[dir].scale(SPEED));
});
}
});
scene("win", () => {
add([
text("You Win!"),
pos(width() / 2, height() / 2),
anchor("center"),
]);
});
go("main", 0);