diff --git a/.prettierrc b/.prettierrc index b203bcc81a3..7daee243588 100644 --- a/.prettierrc +++ b/.prettierrc @@ -9,7 +9,7 @@ { "files": "Dockerfile", "options": { - "plugins": [] + "plugins": ["@trivago/prettier-plugin-sort-imports"] } } ], diff --git a/package-lock.json b/package-lock.json index 6b0ba88a55a..3ad172a6a41 100644 --- a/package-lock.json +++ b/package-lock.json @@ -47,6 +47,7 @@ "@swc/core": "^1.13.3", "@swc/jest": "^0.2.39", "@total-typescript/ts-reset": "^0.6.1", + "@trivago/prettier-plugin-sort-imports": "^5.2.2", "@types/benchmark": "^2.1.5", "@types/chai": "^4.3.17", "@types/d3": "^7.4.3", @@ -6653,6 +6654,41 @@ "dev": true, "license": "MIT" }, + "node_modules/@trivago/prettier-plugin-sort-imports": { + "version": "5.2.2", + "resolved": "https://registry.npmjs.org/@trivago/prettier-plugin-sort-imports/-/prettier-plugin-sort-imports-5.2.2.tgz", + "integrity": "sha512-fYDQA9e6yTNmA13TLVSA+WMQRc5Bn/c0EUBditUHNfMMxN7M82c38b1kEggVE3pLpZ0FwkwJkUEKMiOi52JXFA==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "@babel/generator": "^7.26.5", + "@babel/parser": "^7.26.7", + "@babel/traverse": "^7.26.7", + "@babel/types": "^7.26.7", + "javascript-natural-sort": "^0.7.1", + "lodash": "^4.17.21" + }, + "engines": { + "node": ">18.12" + }, + "peerDependencies": { + "@vue/compiler-sfc": "3.x", + "prettier": "2.x - 3.x", + "prettier-plugin-svelte": "3.x", + "svelte": "4.x || 5.x" + }, + "peerDependenciesMeta": { + "@vue/compiler-sfc": { + "optional": true + }, + "prettier-plugin-svelte": { + "optional": true + }, + "svelte": { + "optional": true + } + } + }, "node_modules/@tsconfig/node10": { "version": "1.0.11", "resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.11.tgz", @@ -13252,6 +13288,13 @@ "@pkgjs/parseargs": "^0.11.0" } }, + "node_modules/javascript-natural-sort": { + "version": "0.7.1", + "resolved": "https://registry.npmjs.org/javascript-natural-sort/-/javascript-natural-sort-0.7.1.tgz", + "integrity": "sha512-nO6jcEfZWQXDhOiBtG2KvKyEptz7RVbpGP4vTD2hLBdmNQSsCiicO2Ioinv6UI4y9ukqnBpy+XZ9H6uLNgJTlw==", + "dev": true, + "license": "MIT" + }, "node_modules/jest": { "version": "30.0.0", "resolved": "https://registry.npmjs.org/jest/-/jest-30.0.0.tgz", diff --git a/package.json b/package.json index 9f131e0ec73..fd6dbf01a62 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "@swc/core": "^1.13.3", "@swc/jest": "^0.2.39", "@total-typescript/ts-reset": "^0.6.1", + "@trivago/prettier-plugin-sort-imports": "^5.2.2", "@types/benchmark": "^2.1.5", "@types/chai": "^4.3.17", "@types/d3": "^7.4.3", diff --git a/src/client/graphics/GameRenderer.ts b/src/client/graphics/GameRenderer.ts index 1b8453f0d2a..b2ce3663f84 100644 --- a/src/client/graphics/GameRenderer.ts +++ b/src/client/graphics/GameRenderer.ts @@ -24,6 +24,7 @@ import { PlayerPanel } from "./layers/PlayerPanel"; import { RailroadLayer } from "./layers/RailroadLayer"; import { RedrawGraphicsEvent } from "../InputHandler"; import { ReplayPanel } from "./layers/ReplayPanel"; +import { SAMRadiusLayer } from "./layers/SAMRadiusLayer"; import { SettingsModal } from "./layers/SettingsModal"; import { SpawnAd } from "./layers/SpawnAd"; import { SpawnTimer } from "./layers/SpawnTimer"; @@ -199,6 +200,7 @@ export function createRenderer( headsUpMessage.game = game; const structureLayer = new StructureLayer(game, eventBus, transformHandler); + const samRadiusLayer = new SAMRadiusLayer(game, eventBus, transformHandler); const fpsDisplay = document.querySelector("fps-display") as FPSDisplay; if (!(fpsDisplay instanceof FPSDisplay)) { @@ -235,10 +237,10 @@ export function createRenderer( new TerritoryLayer(game, eventBus, transformHandler, userSettings), new RailroadLayer(game), structureLayer, + new StructureIconsLayer(game, eventBus, transformHandler), new UnitLayer(game, eventBus, transformHandler), new FxLayer(game), new UILayer(game, eventBus), - new StructureIconsLayer(game, eventBus, transformHandler), new NameLayer(game, transformHandler, eventBus), eventsDisplay, chatDisplay, diff --git a/src/client/graphics/layers/SAMRadiusLayer.ts b/src/client/graphics/layers/SAMRadiusLayer.ts new file mode 100644 index 00000000000..2425ef6717a --- /dev/null +++ b/src/client/graphics/layers/SAMRadiusLayer.ts @@ -0,0 +1,141 @@ +import type { GameView, UnitView } from "../../../core/game/GameView"; +import type { EventBus } from "../../../core/EventBus"; +import { GameUpdateType } from "../../../core/game/GameUpdates"; +import { Layer } from "./Layer"; +import { TransformHandler } from "../TransformHandler"; +import { UnitType } from "../../../core/game/Game"; + +/** + * Layer responsible for rendering SAM launcher defense radiuses + */ +export class SAMRadiusLayer implements Layer { + private readonly canvas: HTMLCanvasElement; + private readonly context: CanvasRenderingContext2D; + private readonly samLaunchers: Set = new Set(); // Track SAM launcher IDs + private needsRedraw = true; + + constructor( + private readonly game: GameView, + private readonly eventBus: EventBus, + private readonly transformHandler: TransformHandler, + ) { + this.canvas = document.createElement("canvas"); + const ctx = this.canvas.getContext("2d"); + if (!ctx) { + throw new Error("2d context not supported"); + } + this.context = ctx; + this.canvas.width = this.game.width(); + this.canvas.height = this.game.height(); + } + + init() { + // Listen for game updates to detect SAM launcher changes + this.redraw(); + } + + shouldTransform(): boolean { + return true; + } + + tick() { + // Check for updates to SAM launchers + const updates = this.game.updatesSinceLastTick(); + const unitUpdates = updates?.[GameUpdateType.Unit]; + + if (unitUpdates) { + let hasChanges = false; + + for (const update of unitUpdates) { + const unit = this.game.unit(update.id); + if (unit && unit.type() === UnitType.SAMLauncher) { + const wasTracked = this.samLaunchers.has(update.id); + const shouldTrack = unit.isActive(); + + if (wasTracked && !shouldTrack) { + // SAM was destroyed + this.samLaunchers.delete(update.id); + hasChanges = true; + } else if (!wasTracked && shouldTrack) { + // New SAM was built + this.samLaunchers.add(update.id); + hasChanges = true; + } + } + } + + if (hasChanges) { + this.needsRedraw = true; + } + } + + // Redraw if transform changed or if we need to redraw + if (this.transformHandler.hasChanged() || this.needsRedraw) { + this.redraw(); + this.needsRedraw = false; + } + } + + renderLayer(context: CanvasRenderingContext2D) { + context.drawImage( + this.canvas, + -this.game.width() / 2, + -this.game.height() / 2, + this.game.width(), + this.game.height(), + ); + } + + redraw() { + // Clear the canvas + this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); + + // Get all active SAM launchers + const samLaunchers = this.game.units(UnitType.SAMLauncher).filter((unit) => unit.isActive()); + + // Update our tracking set + this.samLaunchers.clear(); + samLaunchers.forEach((sam) => this.samLaunchers.add(sam.id())); + + // Draw radius for each SAM launcher + for (const sam of samLaunchers) { + this.drawSAMRadius(sam); + } + } + + private drawSAMRadius(sam: UnitView) { + const samTile = sam.tile(); + const centerX = this.game.x(samTile); + const centerY = this.game.y(samTile); + const radius = this.game.config().defaultSamRange(); // 70 pixels + + // Set up the drawing style + this.context.save(); + + // Create a contrasting color that will be visible against any background + // Use a bright, highly visible color with good opacity + this.context.strokeStyle = "rgba(255, 255, 0, 0.9)"; // Bright yellow with high opacity + this.context.fillStyle = "rgba(255, 255, 0, 0.15)"; // Light yellow fill with more visibility + this.context.lineWidth = 3; // Thicker line for better visibility + this.context.setLineDash([8, 4]); // Larger dashed line for better visibility + + // Draw the circle + this.context.beginPath(); + this.context.arc(centerX, centerY, radius, 0, 2 * Math.PI); + + // Fill first (very light), then stroke (more visible) + this.context.fill(); + this.context.stroke(); + + // Also add a secondary inner ring for extra visibility + this.context.strokeStyle = "rgba(255, 255, 0, 0.6)"; + this.context.lineWidth = 1; + this.context.setLineDash([4, 4]); + + this.context.beginPath(); + this.context.arc(centerX, centerY, radius - 5, 0, 2 * Math.PI); + this.context.stroke(); + + this.context.restore(); + } +}