hmm?
This commit is contained in:
parent
ea0067dc0a
commit
112e6bf86b
11 changed files with 1586 additions and 1253 deletions
74
worldMap.js
74
worldMap.js
|
|
@ -8,6 +8,8 @@ const NEGATIVE_ONE_FIELDS = new Set([
|
|||
"prefectureRegionId",
|
||||
"regionId",
|
||||
"municipalityId",
|
||||
"naturalCompartmentId",
|
||||
"watershedId",
|
||||
]);
|
||||
|
||||
function isCellField(value) {
|
||||
|
|
@ -24,7 +26,7 @@ function defaultForField(name, Constructor) {
|
|||
}
|
||||
|
||||
function makeWorldField(name, source, worldWidth, worldHeight, originX, originY) {
|
||||
const Constructor = source.constructor;
|
||||
const Constructor = NEGATIVE_ONE_FIELDS.has(name) ? Int32Array : source.constructor;
|
||||
const out = new Constructor(worldWidth * worldHeight);
|
||||
const fallback = defaultForField(name, Constructor);
|
||||
if (fallback !== 0) out.fill(fallback);
|
||||
|
|
@ -98,3 +100,73 @@ export function clampCameraToWorld(camera, world, viewWidth = MAP_W, viewHeight
|
|||
y: Math.min(Math.max(Math.round(camera.y || 0), 0), maxY),
|
||||
};
|
||||
}
|
||||
|
||||
function expandRectByOffset(rect, dx, dy) {
|
||||
if (!rect) return rect;
|
||||
return { ...rect, x0: rect.x0 + dx, y0: rect.y0 + dy, x1: rect.x1 + dx, y1: rect.y1 + dy };
|
||||
}
|
||||
|
||||
function shiftRectCollections(world, dx, dy) {
|
||||
if (!dx && !dy) return;
|
||||
for (const key of ["generatedRects", "invalidatedRects", "humanPatchHistory"]) {
|
||||
if (!Array.isArray(world[key])) continue;
|
||||
world[key] = world[key].map((item) => {
|
||||
const out = expandRectByOffset(item, dx, dy);
|
||||
for (const sub of ["coreRect", "writeRect", "repairRect", "contextRect", "blendRect", "transportReachRect"]) {
|
||||
if (out?.[sub]) out[sub] = expandRectByOffset(out[sub], dx, dy);
|
||||
}
|
||||
return out;
|
||||
});
|
||||
}
|
||||
if (world.lastPatchResult) {
|
||||
world.lastPatchResult = { ...world.lastPatchResult };
|
||||
for (const sub of ["coreRect", "writeRect", "repairRect", "contextRect", "blendRect", "transportReachRect"]) {
|
||||
if (world.lastPatchResult[sub]) world.lastPatchResult[sub] = expandRectByOffset(world.lastPatchResult[sub], dx, dy);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function expandWorldMap(world, margins = {}) {
|
||||
if (!world) return { world, dx: 0, dy: 0, expanded: false };
|
||||
const left = Math.max(0, Math.floor(margins.left || 0));
|
||||
const right = Math.max(0, Math.floor(margins.right || 0));
|
||||
const top = Math.max(0, Math.floor(margins.top || 0));
|
||||
const bottom = Math.max(0, Math.floor(margins.bottom || 0));
|
||||
if (!left && !right && !top && !bottom) return { world, dx: 0, dy: 0, expanded: false };
|
||||
const oldWidth = world.width;
|
||||
const oldHeight = world.height;
|
||||
const newWidth = oldWidth + left + right;
|
||||
const newHeight = oldHeight + top + bottom;
|
||||
const newFields = {};
|
||||
for (const [name, field] of Object.entries(world.fields || {})) {
|
||||
if (!ArrayBuffer.isView(field)) continue;
|
||||
const Constructor = NEGATIVE_ONE_FIELDS.has(name) ? Int32Array : field.constructor;
|
||||
const out = new Constructor(newWidth * newHeight);
|
||||
const fallback = defaultForField(name, Constructor);
|
||||
if (fallback !== 0) out.fill(fallback);
|
||||
for (let y = 0; y < oldHeight; y++) {
|
||||
const srcRow = y * oldWidth;
|
||||
const dstRow = (y + top) * newWidth + left;
|
||||
for (let x = 0; x < oldWidth; x++) out[dstRow + x] = field[srcRow + x];
|
||||
}
|
||||
newFields[name] = out;
|
||||
}
|
||||
world.width = newWidth;
|
||||
world.height = newHeight;
|
||||
world.originX += left;
|
||||
world.originY += top;
|
||||
world.fields = newFields;
|
||||
shiftRectCollections(world, left, top);
|
||||
return { world, dx: left, dy: top, expanded: true };
|
||||
}
|
||||
|
||||
export function ensureWorldPaddingForCamera(world, camera, viewWidth = MAP_W, viewHeight = MAP_H, padding = Math.floor(Math.min(MAP_W, MAP_H) * 0.45)) {
|
||||
if (!world || !camera) return { dx: 0, dy: 0, expanded: false };
|
||||
const grow = Math.max(64, Math.floor(padding));
|
||||
const margins = { left: 0, right: 0, top: 0, bottom: 0 };
|
||||
if (camera.x < grow) margins.left = grow;
|
||||
if (camera.y < grow) margins.top = grow;
|
||||
if (camera.x + viewWidth > world.width - grow) margins.right = grow;
|
||||
if (camera.y + viewHeight > world.height - grow) margins.bottom = grow;
|
||||
return expandWorldMap(world, margins);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue