-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame-world-helpers.js
More file actions
165 lines (148 loc) · 3.68 KB
/
game-world-helpers.js
File metadata and controls
165 lines (148 loc) · 3.68 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
// Owns: house-route helpers, map/tile queries, and small world-coordinate
// utilities shared by scenes, progression, and overworld logic.
export function createGameWorldHelpers(deps) {
const {
state,
maps,
species,
cellKey,
screenWidth,
tileSize,
viewTop,
viewHeight,
} = deps;
function shortSeed() {
return String(state.runSeed).slice(-6);
}
function houseStageIndex() {
if (!state.flags.runeH) {
return 0;
}
if (!state.flags.runeJ) {
return 1;
}
if (!state.flags.runeK) {
return 2;
}
if (!state.flags.runeL) {
return 3;
}
return 4;
}
function currentHouseTargetLabel() {
return ["H rune", "J rune", "K rune", "L rune", "exit door"][houseStageIndex()];
}
function houseRouteKeys(stage) {
const keys = new Set();
for (let index = 0; index <= stage; index += 1) {
((state.houseLesson && state.houseLesson.segments[index]) || []).forEach((key) => {
keys.add(key);
});
}
return keys;
}
function houseCurrentSegmentKeys(stage) {
return new Set(
((state.houseLesson && state.houseLesson.segments[Math.min(stage, state.houseLesson.segments.length - 1)]) || [])
);
}
function housePathTone(x, y) {
if (state.map !== "house") {
return "normal";
}
const key = cellKey(x, y);
const stage = houseStageIndex();
const active = houseCurrentSegmentKeys(stage);
const cleared = houseRouteKeys(stage - 1);
if (active.has(key) && !state.houseTrailVisited.includes(key)) {
return "active";
}
if (active.has(key) || cleared.has(key) || state.houseTrailVisited.includes(key)) {
return "cleared";
}
return "future";
}
function markHouseTrailPosition(x, y) {
if (state.map !== "house") {
return;
}
const key = cellKey(x, y);
const allowed = houseRouteKeys(houseStageIndex());
if (!allowed.has(key) && !state.houseTrailVisited.includes(key)) {
return;
}
if (!state.houseTrailVisited.includes(key)) {
state.houseTrailVisited.push(key);
}
}
function mapRows(mapName) {
if (state.randomizedMaps && state.randomizedMaps[mapName]) {
return state.randomizedMaps[mapName];
}
return maps[mapName].rows;
}
function tileAt(mapName, x, y) {
const rows = mapRows(mapName);
if (y < 0 || y >= rows.length || x < 0 || x >= rows[0].length) {
return "#";
}
return rows[y][x];
}
function isWalkable(tile) {
return ".,=HJKLEDRT".includes(tile);
}
function isGateTile(tile) {
return "EDRT".includes(tile);
}
function mapOffset() {
const rows = mapRows(state.map);
return {
x: Math.floor((screenWidth - rows[0].length * tileSize) / 2),
y: viewTop + Math.floor((viewHeight - rows.length * tileSize) / 2),
};
}
function directionVector(direction) {
if (direction === "left") {
return { dx: -1, dy: 0 };
}
if (direction === "right") {
return { dx: 1, dy: 0 };
}
if (direction === "up") {
return { dx: 0, dy: -1 };
}
return { dx: 0, dy: 1 };
}
function reverseDirection(direction) {
if (direction === "left") {
return "right";
}
if (direction === "right") {
return "left";
}
if (direction === "up") {
return "down";
}
return "up";
}
function monsterPalette(id) {
return species[id].palette;
}
return {
shortSeed,
houseStageIndex,
currentHouseTargetLabel,
houseRouteKeys,
houseCurrentSegmentKeys,
housePathTone,
markHouseTrailPosition,
mapRows,
tileAt,
isWalkable,
isGateTile,
mapOffset,
directionVector,
reverseDirection,
monsterPalette,
};
}