mirror of
https://github.com/Terncode/pixel.horse.git
synced 2026-12-03 05:10:04 +01:00
Load maps from JSON files with seasonal layers
This commit is contained in:
1 parent
e74cc2c3a5
commit
5271f6e541
32 files changed
+7912
-6339
No files matched your search
@@ -0,0 +1,178 @@
|
||||
import * as fs from 'fs';
|
||||
import { ServerFlags } from '../common/interfaces';
|
||||
import { Holiday, Season } from '../common/commonInterfaces';
|
||||
import { hasFlag } from '../common/utils';
|
||||
import { pathTo } from './paths';
|
||||
import { ServerMap, ServerEntity } from './serverInterfaces';
|
||||
import { World } from './world';
|
||||
import {
|
||||
MapData, MapLayerData, loadMap, loadEntities, decodeLockedTiles, findEntities, saveMap, stringifyMapData,
|
||||
isSaveableEntity, serializeEntities
|
||||
} from './serverMap';
|
||||
|
||||
export interface MapFiles {
|
||||
name: string;
|
||||
base: MapData;
|
||||
layers: Map<string, MapLayerData>;
|
||||
}
|
||||
|
||||
export function getMapsPath(...path: string[]) {
|
||||
return pathTo('src', 'maps', ...path);
|
||||
}
|
||||
|
||||
export function getSeasonLayerName(season: Season): string {
|
||||
switch (season) {
|
||||
case Season.Summer: return 'summer';
|
||||
case Season.Autumn: return 'autumn';
|
||||
case Season.Winter: return 'winter';
|
||||
case Season.Spring: return 'spring';
|
||||
default: throw new Error(`Invalid season (${season})`);
|
||||
}
|
||||
}
|
||||
|
||||
export function getHolidayLayerName(holiday: Holiday): string | undefined {
|
||||
switch (holiday) {
|
||||
case Holiday.None: return undefined;
|
||||
case Holiday.Christmas: return 'christmas';
|
||||
case Holiday.Halloween: return 'halloween';
|
||||
case Holiday.StPatricks: return 'stpatricks';
|
||||
case Holiday.Easter: return 'easter';
|
||||
default: throw new Error(`Invalid holiday (${holiday})`);
|
||||
}
|
||||
}
|
||||
|
||||
export const seasonLayerNames = [Season.Summer, Season.Autumn, Season.Winter, Season.Spring].map(getSeasonLayerName);
|
||||
|
||||
export const holidayLayerNames = [Holiday.Christmas, Holiday.Halloween, Holiday.StPatricks, Holiday.Easter]
|
||||
.map(h => getHolidayLayerName(h)!);
|
||||
|
||||
export function getActiveLayerNames(season: Season, holiday: Holiday): string[] {
|
||||
const holidayLayer = getHolidayLayerName(holiday);
|
||||
return holidayLayer ? [getSeasonLayerName(season), holidayLayer] : [getSeasonLayerName(season)];
|
||||
}
|
||||
|
||||
function readJSON(fileName: string) {
|
||||
return JSON.parse(fs.readFileSync(fileName, 'utf8'));
|
||||
}
|
||||
|
||||
export function readMapFiles(name: string, path = getMapsPath()): MapFiles {
|
||||
const base = readJSON(`${path}/${name}.json`) as MapData;
|
||||
const layers = new Map<string, MapLayerData>();
|
||||
const prefix = `${name}.`;
|
||||
|
||||
for (const file of fs.readdirSync(path)) {
|
||||
if (file.startsWith(prefix) && file.endsWith('.json')) {
|
||||
const layer = file.substring(prefix.length, file.length - '.json'.length);
|
||||
|
||||
if (layer && layer.indexOf('.') === -1) {
|
||||
layers.set(layer, readJSON(`${path}/${file}`));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { name, base, layers };
|
||||
}
|
||||
|
||||
export interface LoadMapFilesOptions {
|
||||
skipTiles?: boolean;
|
||||
editable?: boolean;
|
||||
}
|
||||
|
||||
const loadedFiles = new WeakMap<ServerMap, MapFiles>();
|
||||
|
||||
export function getMapFiles(map: ServerMap): MapFiles | undefined {
|
||||
return loadedFiles.get(map);
|
||||
}
|
||||
|
||||
export function loadBaseMap(world: World, map: ServerMap, files: MapFiles, options: LoadMapFilesOptions = {}) {
|
||||
loadedFiles.set(map, files);
|
||||
|
||||
const { entities, ...rest } = files.base;
|
||||
const data = options.skipTiles ? { ...rest, tiles: undefined } : rest;
|
||||
loadMap(world, map, data, { loadSpawns: true, loadLockedTiles: true, loadWalls: true });
|
||||
|
||||
if (entities) {
|
||||
loadEntities(world, map, entities, { editable: options.editable });
|
||||
}
|
||||
}
|
||||
|
||||
const loadedLayers = new WeakMap<ServerMap, string[]>();
|
||||
|
||||
export function getLoadedLayers(map: ServerMap): string[] {
|
||||
return loadedLayers.get(map) || [];
|
||||
}
|
||||
|
||||
export function applyMapLayers(world: World, map: ServerMap, files: MapFiles, layerNames: string[]) {
|
||||
loadedLayers.set(map, layerNames.filter(name => files.layers.has(name)));
|
||||
|
||||
for (const entity of findEntities(map, e => hasFlag(e.serverFlags, ServerFlags.Seasonal))) {
|
||||
world.removeEntity(entity, map);
|
||||
}
|
||||
|
||||
map.lockedTiles = decodeLockedTiles(files.base.lockedTiles);
|
||||
|
||||
for (const name of layerNames) {
|
||||
const layer = files.layers.get(name);
|
||||
|
||||
if (layer) {
|
||||
decodeLockedTiles(layer.lockedTiles, map.lockedTiles);
|
||||
|
||||
if (layer.entities) {
|
||||
loadEntities(world, map, layer.entities, { serverFlags: ServerFlags.Seasonal, layer: name });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function applySeasonLayers(world: World, map: ServerMap, files: MapFiles, season: Season, holiday: Holiday) {
|
||||
applyMapLayers(world, map, files, getActiveLayerNames(season, holiday));
|
||||
}
|
||||
|
||||
export function saveMapFiles(map: ServerMap, path: string, name: string) {
|
||||
const warnings: string[] = [];
|
||||
const warn = (message: string) => warnings.push(message);
|
||||
const files = getMapFiles(map);
|
||||
const saved: string[] = [];
|
||||
|
||||
const base = saveMap(map, {
|
||||
saveTiles: true, saveSpawns: true, saveLockedTiles: !files, saveWalls: true, saveEntities: true,
|
||||
filter: e => e.layer === undefined, warn,
|
||||
});
|
||||
|
||||
if (files && files.base.lockedTiles) {
|
||||
base.lockedTiles = files.base.lockedTiles;
|
||||
}
|
||||
|
||||
fs.writeFileSync(`${path}/${name}.json`, stringifyMapData(base), 'utf8');
|
||||
saved.push(`${name}.json`);
|
||||
|
||||
const layerEntities = new Map<string, ServerEntity[]>();
|
||||
|
||||
for (const entity of findEntities(map, e => e.layer !== undefined && isSaveableEntity(e))) {
|
||||
const list = layerEntities.get(entity.layer!) || [];
|
||||
list.push(entity);
|
||||
layerEntities.set(entity.layer!, list);
|
||||
}
|
||||
|
||||
if (files) {
|
||||
for (const layer of getLoadedLayers(map)) {
|
||||
if (!layerEntities.has(layer)) {
|
||||
layerEntities.set(layer, []);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
layerEntities.forEach((entities, layer) => {
|
||||
const data: MapLayerData = { entities: serializeEntities(entities, warn) };
|
||||
const layerData = files && files.layers.get(layer);
|
||||
|
||||
if (layerData && layerData.lockedTiles) {
|
||||
data.lockedTiles = layerData.lockedTiles;
|
||||
}
|
||||
|
||||
fs.writeFileSync(`${path}/${name}.${layer}.json`, stringifyMapData(data), 'utf8');
|
||||
saved.push(`${name}.${layer}.json`);
|
||||
});
|
||||
|
||||
return { saved, warnings };
|
||||
}
|
||||
Reference in new issue
Block a user