Repository navigation
SAM Launcher Defense Radius #1981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SAMRadiusLayer is never rendered (not added to layers). You construct samRadiusLayer but don’t include it in layers, so the radius never shows. Add it just before UILayer so it draws beneath UI but above FX/units as desired: new UnitLayer(game, eventBus, transformHandler),
new FxLayer(game),
+ samRadiusLayer,
new UILayer(game, eventBus),If you prefer the radii under units, place it before UnitLayer instead. Also applies to: 235-245 🤖 Prompt for AI Agents |
||
|
|
||
| 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, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<number> = 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(); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
Wire up ts-reset early or via tsconfig types.
@total-typescript/ts-reset only works if imported first or added to tsconfig "types".
Run:
🏁 Script executed:
Length of output: 68
🏁 Script executed:
Length of output: 825
🏁 Script executed:
Length of output: 1843
Wire up @total-typescript/ts-reset — import first or add to tsconfig types
🤖 Prompt for AI Agents