Files
OpenFrontIO/src/client/graphics/AnimatedSpriteLoader.ts
T
DevelopingTom 902a0b42ac Remove useless sprite setting (#3363)
## Description:

Redundant animated sprite setting: the sprite frame width can be
computed directly.

## 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:

IngloriousTom
2026-03-06 19:58:14 -08:00

237 lines
6.2 KiB
TypeScript

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 { Theme } from "../../core/configuration/Config";
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);
}
}