-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnine-key-view.js
211 lines (189 loc) · 4.96 KB
/
nine-key-view.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
208
209
210
211
// @ts-check
import { assert, assertNonZero } from './lib/assert.js';
import { halfOcturn, fullOcturn, octurnVectors } from './lib/geometry2d.js';
import { makeBoxTileMap } from './tile-map-box.js';
const noop = () => {};
/**
* @typedef {[number, number, number, number, number, number, number, number, number]} NineNumbers
*/
/**
* @param {number} x
* @param {number} y
*/
export function locate(x, y) {
return (y + 1) * 5 + x + 1;
}
export const tileMap = makeBoxTileMap({ x: 5, y: 5 }, { x: -1, y: -1 });
const gridCoordinates = [
{ x: 0, y: 2 },
{ x: 1, y: 2 },
{ x: 2, y: 2 },
{ x: 0, y: 1 },
{ x: 1, y: 1 },
{ x: 2, y: 1 },
{ x: 0, y: 0 },
{ x: 1, y: 0 },
{ x: 2, y: 0 },
];
const gridLocations = gridCoordinates.map(({ x, y }) => locate(x, y));
const octurnGridIndexes = [7, 8, 5, 2, 1, 0, 3, 6];
/**
* @param {import('./types.js').MacroViewModelFacetForAdapter} macroViewModel
*/
export function makeNineKeyView(macroViewModel) {
let next = 1; // 0 is a sentinel for absence.
/** @type {NineNumbers} */
const entities = [0, 0, 0, 0, 0, 0, 0, 0, 0];
/**
* @param {number} type
* @param {number} location
*/
function create(type, location) {
const entity = next;
next = next + 1;
macroViewModel.put(entity, location, type);
return entity;
}
/**
* @param {number} slot
* @param {number} tileType
*/
function spawn(slot, tileType) {
const location = gridLocations[slot];
const entity = create(tileType, location);
macroViewModel.enter(entity);
entities[slot] = entity;
}
/** @param {number} slot */
function despawn(slot) {
const entity = entities[slot];
assertNonZero(entity);
macroViewModel.exit(entity);
entities[slot] = 0;
}
/**
* @param {number} directionOcturns
*/
function despawnOutward(directionOcturns) {
const gridIndex = octurnGridIndexes[directionOcturns];
const entity = entities[gridIndex];
assertNonZero(
entity,
`Expected an entity at gridIndex ${gridIndex} for direction ${directionOcturns}/8th turn clockwise from north`,
);
macroViewModel.take(entity, directionOcturns);
entities[gridIndex] = 0;
}
/**
* @param {number} tileType
* @param {number} directionOcturns
*/
function spawnInward(tileType, directionOcturns) {
const gridIndex = octurnGridIndexes[directionOcturns];
assert(
entities[gridIndex] === 0,
`Cannot spawnInward on ${gridIndex} because that space is occupied by ${entities[gridIndex]}`,
);
const { x, y } = octurnVectors[directionOcturns];
const entity = create(tileType, locate(1 + x * 2, 1 + y * 2));
macroViewModel.move(
entity,
locate(1 + x, 1 + y),
(directionOcturns + halfOcturn) % fullOcturn,
0,
);
entities[gridIndex] = entity;
}
/**
* @param {number} fromSlot
* @param {number} toSlot
* @param {number} directionOcturns
* @param {number} spinOcturns
*/
function move(fromSlot, toSlot, directionOcturns, spinOcturns) {
const entity = entities[fromSlot];
assertNonZero(
entity,
`Cannot move entity in nine-key-view from ${fromSlot} to ${toSlot} because the slot is empty`,
);
const destination = gridLocations[toSlot];
macroViewModel.move(entity, destination, directionOcturns, spinOcturns);
entities[toSlot] = entity;
entities[fromSlot] = 0;
}
/**
* @param {number} slot
* @param {number} directionOcturns
*/
function take(slot, directionOcturns) {
const entity = entities[slot];
assertNonZero(entity);
macroViewModel.take(entity, directionOcturns);
entities[slot] = 0;
}
/**
* @param {number} slot
* @param {number} type
* @param {number} directionOcturns
*/
function give(slot, type, directionOcturns) {
const entity = next;
next = next + 1;
const toLocation = gridLocations[slot];
const { x, y } = gridCoordinates[slot];
const { x: dx, y: dy } = octurnVectors[directionOcturns];
const fromLocation = locate(x - dx, y - dy);
macroViewModel.give(
entity,
fromLocation,
toLocation,
type,
directionOcturns,
);
entities[slot] = entity;
}
/**
* @param {number} slot
* @param {number} tileType
*/
function replace(slot, tileType) {
const entity = entities[slot];
if (entity !== 0) {
macroViewModel.replace(entity, tileType);
} else {
spawn(slot, tileType);
}
}
/**
* @param {number} slot
* @param {number} directionOcturns
*/
function bounce(slot, directionOcturns) {
const entity = entities[slot];
assertNonZero(entity);
macroViewModel.bounce(entity, directionOcturns);
}
/**
* @param {number} slot
*/
function down(slot) {
const entity = entities[slot];
if (entity !== 0) {
return macroViewModel.down(entity);
}
return noop;
}
return {
create,
spawn,
despawn,
despawnOutward,
spawnInward,
move,
take,
replace,
bounce,
give,
down,
};
}