Files
OpenFrontIO/src/core/game/TerrainMapFileLoader.ts
T
Duwibi ccba7a5617 Add Antarctica Map (#544)
## Description:
This PR adds the new Deglaciated Antarctica map(suggested by Backn). It
has 9 nations - most of the country territorial claims on the continent
and the "Penguin Empire".

fixes #545 

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced
- [x] I understand that submitting code with bugs that could have been
caught through manual testing blocks releases and new features for all
contributors

## Please put your Discord username so you can be contacted if a bug or
regression is found:

Nikola123

---------

Co-authored-by: Loymdayddaud <145969603+TheGiraffe3@users.noreply.github.com>
2025-04-28 12:58:10 -07:00

109 lines
3.0 KiB
TypeScript

import { GameMapType } from "./Game";
import { NationMap } from "./TerrainMapLoader";
interface MapData {
mapBin: string;
miniMapBin: string;
nationMap: NationMap;
}
interface MapCache {
bin?: string;
miniMapBin?: string;
nationMap?: NationMap;
}
interface BinModule {
default: string;
}
interface NationMapModule {
default: NationMap;
}
// Mapping from GameMap enum values to file names
const MAP_FILE_NAMES: Record<GameMapType, string> = {
[GameMapType.World]: "WorldMap",
[GameMapType.Europe]: "Europe",
[GameMapType.Mena]: "Mena",
[GameMapType.NorthAmerica]: "NorthAmerica",
[GameMapType.Oceania]: "Oceania",
[GameMapType.BlackSea]: "BlackSea",
[GameMapType.Africa]: "Africa",
[GameMapType.Pangaea]: "Pangaea",
[GameMapType.Asia]: "Asia",
[GameMapType.Mars]: "Mars",
[GameMapType.SouthAmerica]: "SouthAmerica",
[GameMapType.Britannia]: "Britannia",
[GameMapType.GatewayToTheAtlantic]: "GatewayToTheAtlantic",
[GameMapType.Australia]: "Australia",
[GameMapType.Iceland]: "Iceland",
[GameMapType.Japan]: "Japan",
[GameMapType.BetweenTwoSeas]: "BetweenTwoSeas",
[GameMapType.KnownWorld]: "KnownWorld",
[GameMapType.FaroeIslands]: "FaroeIslands",
[GameMapType.DeglaciatedAntarctica]: "DeglaciatedAntarctica",
[GameMapType.EuropeClassic]: "EuropeClassic",
};
class GameMapLoader {
private maps: Map<GameMapType, MapCache>;
private loadingPromises: Map<GameMapType, Promise<MapData>>;
constructor() {
this.maps = new Map<GameMapType, MapCache>();
this.loadingPromises = new Map<GameMapType, Promise<MapData>>();
}
public async getMapData(map: GameMapType): Promise<MapData> {
const cachedMap = this.maps.get(map);
if (cachedMap?.bin && cachedMap?.nationMap) {
return cachedMap as MapData;
}
if (!this.loadingPromises.has(map)) {
this.loadingPromises.set(map, this.loadMapData(map));
}
const data = await this.loadingPromises.get(map)!;
this.maps.set(map, data);
return data;
}
private async loadMapData(map: GameMapType): Promise<MapData> {
const fileName = MAP_FILE_NAMES[map];
if (!fileName) {
throw new Error(`No file name mapping found for map: ${map}`);
}
const [binModule, miniBinModule, infoModule] = await Promise.all([
import(
`!!binary-loader!../../../resources/maps/${fileName}.bin`
) as Promise<BinModule>,
import(
`!!binary-loader!../../../resources/maps/${fileName}Mini.bin`
) as Promise<BinModule>,
import(
`../../../resources/maps/${fileName}.json`
) as Promise<NationMapModule>,
]);
return {
mapBin: binModule.default,
miniMapBin: miniBinModule.default,
nationMap: infoModule.default,
};
}
public isMapLoaded(map: GameMapType): boolean {
const mapData = this.maps.get(map);
return !!mapData?.bin && !!mapData?.nationMap;
}
public getLoadedMaps(): GameMapType[] {
return Array.from(this.maps.keys()).filter((map) => this.isMapLoaded(map));
}
}
export const terrainMapFileLoader = new GameMapLoader();