-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoverworld.js
More file actions
597 lines (560 loc) · 17.9 KB
/
overworld.js
File metadata and controls
597 lines (560 loc) · 17.9 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
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
// Owns: overworld movement, gate transitions, NPC/sign interactions, and
// lesson-specific world events. Does not own: top-level keyboard routing,
// battle rules, or rendering.
export function createOverworldRuntime(deps) {
const {
state,
maps,
cellKey,
houseStageIndex,
houseCurrentSegmentKeys,
houseRouteKeys,
currentHouseTargetLabel,
createMonster,
setMessage,
playSound,
setFx,
checkMilestones,
gateInspectMessage,
gateBlockedMessage,
maybeEncounter,
startEncounterTransition,
healParty,
tileAt,
isWalkable,
isGateTile,
directionVector,
reverseDirection,
openStarterSelect,
syncFollowerTrail,
pushFollowerStep,
startLessonDrill,
houseComplete,
meadowComplete,
ridgeComplete,
groveComplete,
fenComplete,
studioComplete,
markHouseTrailPosition,
} = deps;
function signText() {
const facing = directionVector(state.facing);
const x = state.x + facing.dx;
const y = state.y + facing.dy;
const key = `${state.map}:${x},${y}`;
const table = {
"house:16,2": "Home Row sign: Walk over H, J, K, and L. They are the four core motion keys.",
"meadow:4,1": "Mentor note: w and b land on word starts. e lands on the current word end. ge lands on the previous word end.",
"meadow:13,9": "Field note: Wild VimMonsters live in tall grass. Use a or x to weaken them and f to throw a VimOrb.",
"ridge:6,2": "Cliff note: 0 and $ jump to the far left and far right of the current row. x deletes the stray character under the cursor.",
"ridge:6,10": "Coach note: Press [ or ] once after the drill to switch your active VimMonster and clear Lesson 3. x is Quick Jab in battle.",
"grove:7,4": "Count note: Prefix a motion with a number. Try 3w, then 2j. dd deletes a full line.",
"grove:9,9": "Reward note: Lesson clears grant score and extra VimOrbs. cw powers the next VimOrb in battle.",
"fen:6,4": "Scout note: f and t move forward to a character. F and T search backward on the same line.",
"fen:10,9": "Fen note: The cache is optional. It pays score and VimOrbs, but wakes a rare wild encounter.",
"studio:3,2": "Scribe note: dw deletes a word from the cursor. ciw changes the word under the cursor and drops you into insert.",
"studio:5,9": "Studio note: Break Word strips enemy guard and evade. Inner Word heals and primes a Focus Ball.",
"tower:10,5": "Tower note: gg jumps to the top. G jumps to the bottom. / searches, and :s repairs the file.",
};
return table[key] || "The sign is blank.";
}
function mentorTalk() {
if (!state.flags.metMentor) {
state.flags.metMentor = true;
state.flags.commandUnlocked = true;
state.vimOrbs += 6;
state.score += 90;
setFx("reward", "hud", 1500);
}
if (!state.flags.usedW || !state.flags.usedB || !state.flags.usedE || !state.flags.usedGe) {
startLessonDrill("meadow");
return;
}
setMessage("Mentor W: Think in words. w and b land on starts. e and ge land on word endings.", "mentor");
}
function coachTalk() {
if (!state.flags.metCoach) {
state.flags.metCoach = true;
if (state.party.length < 2) {
state.party.push(createMonster("tabbit", 6));
}
healParty();
state.score += 110;
setFx("reward", "hud", 1500);
}
if (!state.flags.usedZero || !state.flags.usedDollar || !state.flags.usedX) {
startLessonDrill("ridge");
return;
}
if (!state.flags.switchedParty) {
setMessage("Coach Buffer: Good. Now press [ or ] once to switch your active VimMonster. That finishes Lesson 3.", "coach");
return;
}
setMessage("Coach Buffer: x is your Quick Jab now. Use [ and ] whenever a different party member is a better fit.", "coach");
}
function sageTalk() {
if (!state.flags.metSage) {
state.flags.metSage = true;
state.vimOrbs += 3;
state.score += 60;
setFx("reward", "hud", 1500);
}
if (!state.flags.usedCountMove || !state.flags.usedDd || !state.flags.usedCw) {
startLessonDrill("grove");
return;
}
setMessage("Sage Count: Counts multiply the next motion. dd and cw now carry into battle too.", "sage");
}
function scoutTalk() {
if (!state.flags.metScout) {
state.flags.metScout = true;
if (state.party.length < 3) {
state.party.push(createMonster("glyphowl", 7));
}
state.vimOrbs += 2;
state.score += 80;
healParty();
setFx("reward", "hud", 1500);
}
if (!state.flags.usedFindForward || !state.flags.usedTillForward || !state.flags.usedFindBackward || !state.flags.usedTillBackward) {
startLessonDrill("fen");
return;
}
setMessage("Scout Find: Character-find motions win space without overmoving. Use them when a line has a precise target.", "scout");
}
function scribeTalk() {
if (!state.flags.metScribe) {
state.flags.metScribe = true;
state.vimOrbs += 3;
state.score += 90;
healParty();
setFx("reward", "hud", 1500);
}
if (!state.flags.usedDw || !state.flags.usedCiw) {
startLessonDrill("studio");
return;
}
setMessage("Scribe Edit: Operators shape lines fast. dw tears space open. ciw fixes the inner word without touching the rest.", "scribe");
}
function inspectCache(mapName) {
if (mapName === "fen") {
if (state.flags.fenCacheClaimed) {
setMessage("The fen cache is empty. The rare encounter already sprang loose.", "sign");
return;
}
state.flags.fenCacheClaimed = true;
state.score += 90;
state.vimOrbs += 2;
setFx("reward", "hud", 1400);
setMessage("Fen cache opened. +90 score and +2 VimOrbs. A Glyphowl bursts out of the reeds.", "glyphowl");
startEncounterTransition(createMonster("glyphowl", 8), false);
return;
}
if (mapName === "studio") {
if (state.flags.studioCacheClaimed) {
setMessage("The studio cache is empty. The hard optional fight is already gone.", "sign");
return;
}
state.flags.studioCacheClaimed = true;
state.score += 110;
state.vimOrbs += 2;
setFx("reward", "hud", 1400);
setMessage("Studio cache opened. +110 score and +2 VimOrbs. Slashram charges out of the frame room.", "slashram");
startEncounterTransition(createMonster("slashram", 9), false);
}
}
function altarInteract() {
if (!state.flags.towerDrillCleared) {
startLessonDrill("tower");
return;
}
if (state.flags.finalWon) {
setMessage("The altar is quiet. You already beat the academy.", "macrobat");
return;
}
if (!state.flags.finalStarted) {
state.flags.finalStarted = true;
startEncounterTransition(createMonster("macrobat", 9), true);
return;
}
startEncounterTransition(createMonster("macrobat", 9), true);
}
function interactAhead() {
const vector = directionVector(state.facing);
const targetX = state.x + vector.dx;
const targetY = state.y + vector.dy;
const tile = tileAt(state.map, targetX, targetY);
if (tile === "B") {
healParty();
setMessage("You rest at home. The whole party is fully healed.", "player");
return;
}
if (tile === "S") {
setMessage(signText(), "sign");
return;
}
if (tile === "M") {
mentorTalk();
return;
}
if (tile === "C") {
coachTalk();
return;
}
if (tile === "V") {
sageTalk();
return;
}
if (tile === "N") {
scoutTalk();
return;
}
if (tile === "Q") {
scribeTalk();
return;
}
if (tile === "Y") {
inspectCache(state.map);
return;
}
if (tile === "X") {
altarInteract();
return;
}
if (tile === "E" || tile === "R" || tile === "D" || tile === "T") {
setMessage(gateInspectMessage(tile), "sign");
return;
}
setMessage("Nothing important is in front of you.", "player");
}
function postLanding(tile) {
if (state.map === "house") {
const stage = houseStageIndex();
const currentKey = cellKey(state.x, state.y);
const activeSegment = houseCurrentSegmentKeys(stage);
const clearedRoute = houseRouteKeys(stage - 1);
activeSegment.add(cellKey(state.houseLesson.start.x, state.houseLesson.start.y));
["H", "J", "K", "L"].forEach((letter) => {
const point = state.houseLesson.targets[letter];
if (point && (state.flags[`rune${letter}`] || houseStageIndex() === ["H", "J", "K", "L"].indexOf(letter))) {
activeSegment.add(cellKey(point.x, point.y));
}
});
if (!activeSegment.has(currentKey) && !clearedRoute.has(currentKey) && tile !== "S" && tile !== "B") {
state.score = Math.max(0, state.score - 8);
setMessage(`Stay on the lesson path to the ${currentHouseTargetLabel()}. -8 score.`, "player");
return;
}
if (tile === "H" && !state.flags.runeH) {
if (stage !== 0) {
state.score = Math.max(0, state.score - 10);
setMessage("That is not the next rune in the route. -10 score.", "player");
return;
}
state.flags.runeH = true;
state.score += 20;
setMessage("Rune H: h moves left. +20 score. Follow the bright route to J next.", "player");
checkMilestones();
return;
}
if (tile === "J" && !state.flags.runeJ) {
if (stage !== 1) {
state.score = Math.max(0, state.score - 10);
setMessage("The training route wants H first, then J. -10 score.", "player");
return;
}
state.flags.runeJ = true;
state.score += 20;
setMessage("Rune J: j moves down. +20 score. Follow the bright route to K next.", "player");
checkMilestones();
return;
}
if (tile === "K" && !state.flags.runeK) {
if (stage !== 2) {
state.score = Math.max(0, state.score - 10);
setMessage("The lesson route has not reached K yet. -10 score.", "player");
return;
}
state.flags.runeK = true;
state.score += 20;
setMessage("Rune K: k moves up. +20 score. Follow the bright route to L next.", "player");
checkMilestones();
return;
}
if (tile === "L" && !state.flags.runeL) {
if (stage !== 3) {
state.score = Math.max(0, state.score - 10);
setMessage("The lesson route has not reached L yet. -10 score.", "player");
return;
}
state.flags.runeL = true;
state.score += 20;
setMessage("Rune L: l moves right. +20 score. Follow the bright route back to the door.", "player");
checkMilestones();
return;
}
}
maybeEncounter(tile);
if (state.mode === "overworld") {
if (tile === ",") {
setMessage("Tall grass rustles around you.");
} else {
setMessage(`Exploring ${maps[state.map].name}.`);
}
}
}
function transitionIfNeeded(tile) {
if (!isGateTile(tile)) {
return false;
}
if (state.map === "house" && tile === "E") {
if (!houseComplete()) {
setMessage(gateBlockedMessage(tile));
return true;
}
if (!state.flags.starterChosen) {
openStarterSelect();
return true;
}
state.map = "meadow";
state.x = 1;
state.y = 1;
state.facing = "right";
syncFollowerTrail();
setMessage("Welcome to Word Meadow. Find Mentor W and catch your first VimMonster.");
return true;
}
if (state.map === "meadow" && tile === "D") {
state.map = "house";
state.x = 9;
state.y = 10;
state.facing = "down";
syncFollowerTrail();
setMessage("Back to Home Row House.");
return true;
}
if (state.map === "meadow" && tile === "R") {
if (!meadowComplete()) {
setMessage(gateBlockedMessage(tile));
return true;
}
state.map = "ridge";
state.x = 1;
state.y = 1;
state.facing = "right";
syncFollowerTrail();
setMessage("Line Ridge opens. Coach Buffer teaches 0, $, then [ or ] to switch your active VimMonster.");
return true;
}
if (state.map === "ridge" && tile === "R") {
state.map = "meadow";
state.x = 17;
state.y = 10;
state.facing = "left";
syncFollowerTrail();
setMessage("You head back to Word Meadow.");
return true;
}
if (state.map === "ridge" && tile === "T") {
if (!ridgeComplete()) {
setMessage(gateBlockedMessage(tile));
return true;
}
state.map = "grove";
state.x = 1;
state.y = 1;
state.facing = "right";
syncFollowerTrail();
setMessage("Count Grove opens. Lesson 3 is complete and Sage Count is waiting with count motions.");
return true;
}
if (state.map === "grove" && tile === "R") {
state.map = "ridge";
state.x = 17;
state.y = 10;
state.facing = "left";
syncFollowerTrail();
setMessage("You head back to Line Ridge.");
return true;
}
if (state.map === "grove" && tile === "T") {
if (!groveComplete()) {
setMessage("Finder Fen opens after Sage Count teaches counts and you use them.");
return true;
}
state.map = "fen";
state.x = 1;
state.y = 1;
state.facing = "right";
syncFollowerTrail();
setMessage("Finder Fen opens. Scout Find teaches f, t, F, and T, and a hidden cache stirs a rare encounter.");
return true;
}
if (state.map === "fen" && tile === "R") {
state.map = "grove";
state.x = 17;
state.y = 10;
state.facing = "left";
syncFollowerTrail();
setMessage("You head back to Count Grove.");
return true;
}
if (state.map === "fen" && tile === "T") {
if (!fenComplete()) {
setMessage(gateBlockedMessage(tile));
return true;
}
state.map = "studio";
state.x = 1;
state.y = 1;
state.facing = "right";
syncFollowerTrail();
setMessage("Operator Studio opens. Scribe Edit teaches dw and ciw, and the cache holds a harder optional fight.");
return true;
}
if (state.map === "studio" && tile === "R") {
state.map = "fen";
state.x = 17;
state.y = 10;
state.facing = "left";
syncFollowerTrail();
setMessage("You head back to Finder Fen.");
return true;
}
if (state.map === "studio" && tile === "T") {
if (!studioComplete()) {
setMessage(gateBlockedMessage(tile));
return true;
}
state.map = "tower";
state.x = 1;
state.y = 1;
state.facing = "right";
syncFollowerTrail();
setMessage("Macro Tower unlocks. Search, replace, and the final battle wait at the altar.");
return true;
}
if (state.map === "tower" && tile === "T") {
state.map = "studio";
state.x = 17;
state.y = 10;
state.facing = "left";
syncFollowerTrail();
setMessage("You descend back to Operator Studio.");
return true;
}
return false;
}
function moveTo(x, y, facing) {
const previous = {
map: state.map,
x: state.x,
y: state.y,
facing: state.facing,
};
state.x = x;
state.y = y;
state.facing = facing;
state.lastMoveAt = performance.now();
state.stepFrame = (state.stepFrame + 1) % 2;
playSound("step");
markHouseTrailPosition(x, y);
pushFollowerStep(previous);
const tile = tileAt(state.map, x, y);
if (transitionIfNeeded(tile)) {
return true;
}
postLanding(tile);
return true;
}
function tryMove(dx, dy, facing, silentBlock) {
state.facing = facing;
const nextX = state.x + dx;
const nextY = state.y + dy;
const tile = tileAt(state.map, nextX, nextY);
if (!isWalkable(tile)) {
if (!silentBlock) {
state.score = Math.max(0, state.score - 2);
playSound("blocked");
setMessage("That path is blocked. -2 score.", "player");
}
return false;
}
return moveTo(nextX, nextY, facing);
}
function scanMove(dx, dy, limit, facing) {
let steps = 0;
let currentX = state.x;
let currentY = state.y;
let moved = false;
while (steps < limit) {
const nextX = currentX + dx;
const nextY = currentY + dy;
const tile = tileAt(state.map, nextX, nextY);
if (!isWalkable(tile)) {
break;
}
currentX = nextX;
currentY = nextY;
moved = true;
steps += 1;
if (isGateTile(tile)) {
break;
}
}
if (!moved) {
playSound("blocked");
setMessage("No room to move that way.");
return false;
}
return moveTo(currentX, currentY, facing);
}
function lineJump(horizontalDirection) {
const delta = horizontalDirection === "left" ? -1 : 1;
const moved = scanMove(delta, 0, 999, horizontalDirection);
if (moved) {
if (horizontalDirection === "left") {
state.flags.usedZero = true;
} else {
state.flags.usedDollar = true;
}
checkMilestones();
}
}
function fileJump(verticalDirection) {
const delta = verticalDirection === "up" ? -1 : 1;
const moved = scanMove(0, delta, 999, verticalDirection);
if (moved) {
if (verticalDirection === "up") {
state.flags.usedGG = true;
} else {
state.flags.usedBigG = true;
}
checkMilestones();
}
}
function dash(direction, backwards, count) {
const vector = directionVector(backwards ? reverseDirection(direction) : direction);
const facing = backwards ? reverseDirection(direction) : direction;
const moved = scanMove(vector.dx, vector.dy, count, facing);
if (!moved) {
return;
}
if (backwards) {
state.flags.usedB = true;
} else {
state.flags.usedW = true;
}
if (count >= 3 && !backwards) {
state.flags.usedCountWord = true;
}
checkMilestones();
}
return {
tryMove,
lineJump,
fileJump,
dash,
interactAhead,
};
}