mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-22 09:58:10 +00:00
275fd0dccc
## Description: This is a refactor to simplify config handling. Replaces the per-environment DevConfig/PreprodConfig/ProdConfig class hierarchy with two static classes: ClientEnv (browser main thread, reads from window.BOOTSTRAP_CONFIG) and ServerEnv (Node server, reads from process.env). The four config classes are deleted, the abstract DefaultServerConfig is gone, and DefaultConfig is renamed to Config. The values that flow server → client (gameEnv, numWorkers, turnstileSiteKey, jwtAudience, instanceId) used to be baked into the hardcoded per-env classes. They're now real env vars on the server, embedded into a single window.BOOTSTRAP_CONFIG object in index.html at request time (alongside the existing gitCommit/assetManifest/cdnBase globals, which moved into the same object), and read back by ClientEnv on the client. The dev defaults previously hidden inside DevServerConfig are now explicit in start:server-dev (NUM_WORKERS=2, TURNSTILE_SITE_KEY=1x..., JWT_AUDIENCE=localhost, etc.) and in vite.config.ts's html plugin inject.data. Production deploys plumb NUM_WORKERS and TURNSTILE_SITE_KEY through deploy.yml (GitHub vars) into the remote env file; JWT_AUDIENCE is derived from DOMAIN in deploy.sh. The dynamic /api/instance endpoint is gone — INSTANCE_ID rides along in BOOTSTRAP_CONFIG now. ServerEnv is the only thing server code touches; ClientEnv is browser-only. The two classes have intentional overlap (env, numWorkers, jwtIssuer, gameCreationRate, workerIndex, etc.) since they derive identical logic from different sources — there's a TODO in each to consolidate via a shared helper later. The game-logic Config no longer stores a ServerConfig/ClientEnv reference and its serverConfig() getter is gone; the one caller (MultiTabModal) now reads ClientEnv.env() directly. Worker init no longer carries server-config values since nothing in the worker actually reads them. ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I process any text displayed to the user through translateText() and I've added it to the en.json file - [x] I have added relevant tests to the test directory - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced ## Please put your Discord username so you can be contacted if a bug or regression is found: evan
237 lines
6.2 KiB
TypeScript
237 lines
6.2 KiB
TypeScript
import { Theme } from "src/core/configuration/Theme";
|
|
import miniBigSmoke from "../../../resources/sprites/bigsmoke.png";
|
|
import buildingExplosion from "../../../resources/sprites/buildingExplosion.png";
|
|
import conquestSword from "../../../resources/sprites/conquestSword.png";
|
|
import dust from "../../../resources/sprites/dust.png";
|
|
import miniExplosion from "../../../resources/sprites/miniExplosion.png";
|
|
import miniFire from "../../../resources/sprites/minifire.png";
|
|
import nuke from "../../../resources/sprites/nukeExplosion.png";
|
|
import SAMExplosion from "../../../resources/sprites/samExplosion.png";
|
|
import sinkingShip from "../../../resources/sprites/sinkingShip.png";
|
|
import miniSmoke from "../../../resources/sprites/smoke.png";
|
|
import miniSmokeAndFire from "../../../resources/sprites/smokeAndFire.png";
|
|
import unitExplosion from "../../../resources/sprites/unitExplosion.png";
|
|
import { PlayerView } from "../../core/game/GameView";
|
|
import { AnimatedSprite } from "./AnimatedSprite";
|
|
import { FxType } from "./fx/Fx";
|
|
import { colorizeCanvas } from "./SpriteLoader";
|
|
|
|
type AnimatedSpriteConfig = {
|
|
url: string;
|
|
frameCount: number;
|
|
frameDuration: number; // ms per frame
|
|
looping?: boolean;
|
|
originX: number;
|
|
originY: number;
|
|
};
|
|
|
|
const ANIMATED_SPRITE_CONFIG: Partial<Record<FxType, AnimatedSpriteConfig>> = {
|
|
[FxType.MiniFire]: {
|
|
url: miniFire,
|
|
frameCount: 6,
|
|
frameDuration: 100,
|
|
looping: true,
|
|
originX: 3,
|
|
originY: 11,
|
|
},
|
|
[FxType.MiniSmoke]: {
|
|
url: miniSmoke,
|
|
frameCount: 4,
|
|
frameDuration: 120,
|
|
looping: true,
|
|
originX: 2,
|
|
originY: 10,
|
|
},
|
|
[FxType.MiniBigSmoke]: {
|
|
url: miniBigSmoke,
|
|
frameCount: 5,
|
|
frameDuration: 120,
|
|
looping: true,
|
|
originX: 9,
|
|
originY: 14,
|
|
},
|
|
[FxType.MiniSmokeAndFire]: {
|
|
url: miniSmokeAndFire,
|
|
frameCount: 6,
|
|
frameDuration: 120,
|
|
looping: true,
|
|
originX: 9,
|
|
originY: 14,
|
|
},
|
|
[FxType.MiniExplosion]: {
|
|
url: miniExplosion,
|
|
frameCount: 4,
|
|
frameDuration: 70,
|
|
looping: false,
|
|
originX: 6,
|
|
originY: 6,
|
|
},
|
|
[FxType.Dust]: {
|
|
url: dust,
|
|
frameCount: 3,
|
|
frameDuration: 100,
|
|
looping: false,
|
|
originX: 4,
|
|
originY: 5,
|
|
},
|
|
[FxType.UnitExplosion]: {
|
|
url: unitExplosion,
|
|
frameCount: 4,
|
|
frameDuration: 70,
|
|
looping: false,
|
|
originX: 9,
|
|
originY: 9,
|
|
},
|
|
[FxType.BuildingExplosion]: {
|
|
url: buildingExplosion,
|
|
frameCount: 10,
|
|
frameDuration: 70,
|
|
looping: false,
|
|
originX: 8,
|
|
originY: 8,
|
|
},
|
|
[FxType.SinkingShip]: {
|
|
url: sinkingShip,
|
|
frameCount: 14,
|
|
frameDuration: 90,
|
|
looping: false,
|
|
originX: 7,
|
|
originY: 7,
|
|
},
|
|
[FxType.Nuke]: {
|
|
url: nuke,
|
|
frameCount: 9,
|
|
frameDuration: 70,
|
|
looping: false,
|
|
originX: 30,
|
|
originY: 30,
|
|
},
|
|
[FxType.SAMExplosion]: {
|
|
url: SAMExplosion,
|
|
frameCount: 9,
|
|
frameDuration: 70,
|
|
looping: false,
|
|
originX: 23,
|
|
originY: 19,
|
|
},
|
|
[FxType.Conquest]: {
|
|
url: conquestSword,
|
|
frameCount: 10,
|
|
frameDuration: 90,
|
|
looping: false,
|
|
originX: 10,
|
|
originY: 16,
|
|
},
|
|
};
|
|
|
|
export class AnimatedSpriteLoader {
|
|
private animatedSpriteImageMap: Map<FxType, HTMLCanvasElement> = new Map();
|
|
// Do not color the same sprite twice
|
|
private coloredAnimatedSpriteCache: Map<string, HTMLCanvasElement> =
|
|
new Map();
|
|
|
|
public async loadAllAnimatedSpriteImages(): Promise<void> {
|
|
const entries = Object.entries(ANIMATED_SPRITE_CONFIG);
|
|
|
|
await Promise.all(
|
|
entries.map(async ([fxType, config]) => {
|
|
const typedFxType = fxType as FxType;
|
|
if (!config?.url) return;
|
|
|
|
try {
|
|
const img = new Image();
|
|
img.crossOrigin = "anonymous";
|
|
img.src = config.url;
|
|
|
|
await new Promise<void>((resolve, reject) => {
|
|
img.onload = () => resolve();
|
|
img.onerror = (e) => reject(e);
|
|
});
|
|
|
|
const canvas = document.createElement("canvas");
|
|
canvas.width = img.width;
|
|
canvas.height = img.height;
|
|
canvas.getContext("2d")!.drawImage(img, 0, 0);
|
|
|
|
this.animatedSpriteImageMap.set(typedFxType, canvas);
|
|
} catch (err) {
|
|
console.error(`Failed to load sprite for ${typedFxType}:`, err);
|
|
}
|
|
}),
|
|
);
|
|
}
|
|
|
|
private createRegularAnimatedSprite(fxType: FxType): AnimatedSprite | null {
|
|
const config = ANIMATED_SPRITE_CONFIG[fxType];
|
|
const image = this.animatedSpriteImageMap.get(fxType);
|
|
if (!config || !image) return null;
|
|
|
|
return new AnimatedSprite(
|
|
image,
|
|
config.frameCount,
|
|
config.frameDuration,
|
|
config.looping ?? true,
|
|
config.originX,
|
|
config.originY,
|
|
);
|
|
}
|
|
|
|
private getColoredAnimatedSprite(
|
|
owner: PlayerView,
|
|
fxType: FxType,
|
|
theme: Theme,
|
|
): HTMLCanvasElement | null {
|
|
const baseImage = this.animatedSpriteImageMap.get(fxType);
|
|
const config = ANIMATED_SPRITE_CONFIG[fxType];
|
|
if (!baseImage || !config) return null;
|
|
const territoryColor = owner.territoryColor();
|
|
const borderColor = owner.borderColor();
|
|
const spawnHighlightColor = theme.spawnHighlightColor();
|
|
const key = `${fxType}-${owner.id()}`;
|
|
let coloredCanvas: HTMLCanvasElement;
|
|
if (this.coloredAnimatedSpriteCache.has(key)) {
|
|
coloredCanvas = this.coloredAnimatedSpriteCache.get(key)!;
|
|
} else {
|
|
coloredCanvas = colorizeCanvas(
|
|
baseImage,
|
|
territoryColor,
|
|
borderColor,
|
|
spawnHighlightColor,
|
|
);
|
|
|
|
this.coloredAnimatedSpriteCache.set(key, coloredCanvas);
|
|
}
|
|
return coloredCanvas;
|
|
}
|
|
|
|
private createColoredAnimatedSpriteForUnit(
|
|
fxType: FxType,
|
|
owner: PlayerView,
|
|
theme: Theme,
|
|
): AnimatedSprite | null {
|
|
const config = ANIMATED_SPRITE_CONFIG[fxType];
|
|
const image = this.getColoredAnimatedSprite(owner, fxType, theme);
|
|
if (!config || !image) return null;
|
|
|
|
return new AnimatedSprite(
|
|
image,
|
|
config.frameCount,
|
|
config.frameDuration,
|
|
config.looping ?? true,
|
|
config.originX,
|
|
config.originY,
|
|
);
|
|
}
|
|
|
|
public createAnimatedSprite(
|
|
fxType: FxType,
|
|
owner?: PlayerView,
|
|
theme?: Theme,
|
|
): AnimatedSprite | null {
|
|
if (owner && theme) {
|
|
return this.createColoredAnimatedSpriteForUnit(fxType, owner, theme);
|
|
}
|
|
return this.createRegularAnimatedSprite(fxType);
|
|
}
|
|
}
|