From ba474e94e2b2777c1302869608be26b5dbf64cbd Mon Sep 17 00:00:00 2001 From: Wissam Nusair Date: Sun, 24 Aug 2025 13:40:32 -0400 Subject: [PATCH 1/2] added a SAM launcher border radius in unique layer --- src/client/graphics/GameRenderer.ts | 5 +- src/client/graphics/layers/SAMRadiusLayer.ts | 137 +++++++++++++++++++ 2 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 src/client/graphics/layers/SAMRadiusLayer.ts diff --git a/src/client/graphics/GameRenderer.ts b/src/client/graphics/GameRenderer.ts index 1b8453f0d2a..5471e460d46 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"; @@ -234,11 +235,11 @@ export function createRenderer( new TerrainLayer(game, transformHandler), new TerritoryLayer(game, eventBus, transformHandler, userSettings), new RailroadLayer(game), + new SAMRadiusLayer(game, eventBus, transformHandler), structureLayer, new UnitLayer(game, eventBus, transformHandler), new FxLayer(game), - new UILayer(game, eventBus), - new StructureIconsLayer(game, eventBus, transformHandler), + new UILayer(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..33403b9b225 --- /dev/null +++ b/src/client/graphics/layers/SAMRadiusLayer.ts @@ -0,0 +1,137 @@ +import { Layer } from "./Layer"; +import { GameView, UnitView } from "../../../core/game/GameView"; +import { EventBus } from "../../../core/EventBus"; +import { TransformHandler } from "../TransformHandler"; +import { UnitType } from "../../../core/game/Game"; +import { GameUpdateType } from "../../../core/game/GameUpdates"; + +/** + * Layer responsible for rendering SAM launcher defense radiuses + */ +export class SAMRadiusLayer implements Layer { + private canvas: HTMLCanvasElement; + private context: CanvasRenderingContext2D; + private 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"); + this.context = this.canvas.getContext("2d")!; + 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(); + } +} From 68d539910d571700edd2743774281f036805bda3 Mon Sep 17 00:00:00 2001 From: Wissam Nusair Date: Sun, 24 Aug 2025 14:24:28 -0400 Subject: [PATCH 2/2] added a SAM launcher border radius in unique layer --- .prettierrc | 2 +- package-lock.json | 43 +++++++++++++++++ package.json | 1 + src/client/graphics/GameRenderer.ts | 5 +- src/client/graphics/layers/SAMRadiusLayer.ts | 50 +++++++++++--------- 5 files changed, 75 insertions(+), 26 deletions(-) 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 5471e460d46..b2ce3663f84 100644 --- a/src/client/graphics/GameRenderer.ts +++ b/src/client/graphics/GameRenderer.ts @@ -200,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,11 +236,11 @@ export function createRenderer( new TerrainLayer(game, transformHandler), new TerritoryLayer(game, eventBus, transformHandler, userSettings), new RailroadLayer(game), - new SAMRadiusLayer(game, eventBus, transformHandler), structureLayer, + new StructureIconsLayer(game, eventBus, transformHandler), new UnitLayer(game, eventBus, transformHandler), new FxLayer(game), - new UILayer(game, eventBus, transformHandler), + new UILayer(game, eventBus), new NameLayer(game, transformHandler, eventBus), eventsDisplay, chatDisplay, diff --git a/src/client/graphics/layers/SAMRadiusLayer.ts b/src/client/graphics/layers/SAMRadiusLayer.ts index 33403b9b225..2425ef6717a 100644 --- a/src/client/graphics/layers/SAMRadiusLayer.ts +++ b/src/client/graphics/layers/SAMRadiusLayer.ts @@ -1,17 +1,17 @@ +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 { GameView, UnitView } from "../../../core/game/GameView"; -import { EventBus } from "../../../core/EventBus"; import { TransformHandler } from "../TransformHandler"; import { UnitType } from "../../../core/game/Game"; -import { GameUpdateType } from "../../../core/game/GameUpdates"; /** * Layer responsible for rendering SAM launcher defense radiuses */ export class SAMRadiusLayer implements Layer { - private canvas: HTMLCanvasElement; - private context: CanvasRenderingContext2D; - private samLaunchers: Set = new Set(); // Track SAM launcher IDs + private readonly canvas: HTMLCanvasElement; + private readonly context: CanvasRenderingContext2D; + private readonly samLaunchers: Set = new Set(); // Track SAM launcher IDs private needsRedraw = true; constructor( @@ -20,7 +20,11 @@ export class SAMRadiusLayer implements Layer { private readonly transformHandler: TransformHandler, ) { this.canvas = document.createElement("canvas"); - this.context = this.canvas.getContext("2d")!; + 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(); } @@ -38,16 +42,16 @@ export class SAMRadiusLayer implements Layer { // 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); @@ -59,7 +63,7 @@ export class SAMRadiusLayer implements Layer { } } } - + if (hasChanges) { this.needsRedraw = true; } @@ -87,11 +91,11 @@ export class SAMRadiusLayer implements Layer { 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()); - + 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())); + samLaunchers.forEach((sam) => this.samLaunchers.add(sam.id())); // Draw radius for each SAM launcher for (const sam of samLaunchers) { @@ -104,34 +108,34 @@ export class SAMRadiusLayer implements Layer { 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.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.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(); } }