-
Notifications
You must be signed in to change notification settings - Fork 707
Add warship patrol area indicator #2543
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
Open
BCNelson
wants to merge
5
commits into
openfrontio:main
Choose a base branch
from
BCNelson:feature/warship-patrol-radius-indicator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bc66a6e
Add warship patrol radius indicator 🚢
BCNelson 7f29264
Change warship patrol indicator from circle to square
BCNelson 2a4947b
Fix warship deselection to only clear for the selected warship
BCNelson aba3a75
Clear warship selection when a non-warship unit is selected
BCNelson 6868335
Add tests for WarshipRadiusLayer
BCNelson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| import type { EventBus } from "../../../core/EventBus"; | ||
| import { UnitType } from "../../../core/game/Game"; | ||
| import type { GameView, UnitView } from "../../../core/game/GameView"; | ||
| import { MouseMoveEvent, UnitSelectionEvent } from "../../InputHandler"; | ||
| import { TransformHandler } from "../TransformHandler"; | ||
| import { UIState } from "../UIState"; | ||
| import { Layer } from "./Layer"; | ||
|
|
||
| /** | ||
| * Layer responsible for rendering warship patrol area indicators. | ||
| * Shows: | ||
| * - Current patrol area (solid line square) - centered on warship's patrolTile | ||
| * - Preview patrol area (dashed line square) - follows cursor for placement preview | ||
| */ | ||
| export class WarshipRadiusLayer implements Layer { | ||
| private readonly canvas: HTMLCanvasElement; | ||
| private readonly context: CanvasRenderingContext2D; | ||
|
|
||
| // State tracking | ||
| private selectedWarship: UnitView | null = null; | ||
| private needsRedraw = true; | ||
| private selectedShow = false; // Warship is selected | ||
| private ghostShow = false; // In warship spawn mode | ||
|
|
||
| // Animation for dashed preview squares | ||
| private dashOffset = 0; | ||
| private animationSpeed = 14; // px per second (matches SAMRadiusLayer) | ||
| private lastTickTime = Date.now(); | ||
|
|
||
| // Cursor tracking for preview squares | ||
| private mouseWorldPos: { x: number; y: number } | null = null; | ||
|
|
||
| constructor( | ||
| private readonly game: GameView, | ||
| private readonly eventBus: EventBus, | ||
| private readonly transformHandler: TransformHandler, | ||
| private readonly uiState: UIState, | ||
| ) { | ||
| 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(); | ||
| } | ||
|
|
||
| shouldTransform(): boolean { | ||
| return true; | ||
| } | ||
|
|
||
| init() { | ||
| this.eventBus.on(UnitSelectionEvent, (e) => this.handleUnitSelection(e)); | ||
| this.eventBus.on(MouseMoveEvent, (e) => this.handleMouseMove(e)); | ||
| this.redraw(); | ||
| } | ||
|
|
||
| tick() { | ||
| // Update ghost mode state | ||
| const wasGhostShow = this.ghostShow; | ||
| this.ghostShow = this.uiState.ghostStructure === UnitType.Warship; | ||
|
|
||
| // Clear mouse position when ghost mode ends (e.g., after placing warship) | ||
| if (wasGhostShow && !this.ghostShow) { | ||
| this.mouseWorldPos = null; | ||
| this.needsRedraw = true; | ||
| } | ||
|
|
||
| // Check if selected warship was destroyed | ||
| if (this.selectedWarship && !this.selectedWarship.isActive()) { | ||
| this.selectedWarship = null; | ||
| this.selectedShow = false; | ||
| this.needsRedraw = true; | ||
| } | ||
|
|
||
| // Animate dash offset only when preview square is visible | ||
| const now = Date.now(); | ||
| const dt = now - this.lastTickTime; | ||
| this.lastTickTime = now; | ||
|
|
||
| const previewVisible = | ||
| (this.selectedShow || this.ghostShow) && this.mouseWorldPos; | ||
| if (previewVisible) { | ||
| this.dashOffset += (this.animationSpeed * dt) / 1000; | ||
| if (this.dashOffset > 1e6) this.dashOffset = this.dashOffset % 1000000; | ||
| this.needsRedraw = true; | ||
| } | ||
|
|
||
| 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(), | ||
| ); | ||
| } | ||
|
|
||
| private handleUnitSelection(e: UnitSelectionEvent) { | ||
| if (e.unit?.type() === UnitType.Warship && e.isSelected) { | ||
| this.selectedWarship = e.unit; | ||
| this.selectedShow = true; | ||
| } else if (!e.isSelected && this.selectedWarship === e.unit) { | ||
| this.selectedWarship = null; | ||
| this.selectedShow = false; | ||
| } | ||
| this.needsRedraw = true; | ||
| } | ||
|
|
||
| private handleMouseMove(e: MouseMoveEvent) { | ||
| if (!this.selectedShow && !this.ghostShow) return; | ||
|
|
||
| const rect = this.transformHandler.boundingRect(); | ||
| if (!rect) return; | ||
|
|
||
| // Convert screen coordinates to world coordinates | ||
| const worldPos = this.transformHandler.screenToWorldCoordinates( | ||
| e.x - rect.left, | ||
| e.y - rect.top, | ||
| ); | ||
|
|
||
| this.mouseWorldPos = worldPos; | ||
| this.needsRedraw = true; | ||
| } | ||
|
|
||
| redraw() { | ||
| this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); | ||
|
|
||
| // Draw current patrol area (solid) when warship selected | ||
| if (this.selectedWarship && this.selectedShow) { | ||
| const patrolTile = this.selectedWarship.patrolTile(); | ||
| if (patrolTile) { | ||
| const x = this.game.x(patrolTile); | ||
| const y = this.game.y(patrolTile); | ||
| this.drawCurrentPatrol(x, y); | ||
| } | ||
| } | ||
|
|
||
| // Draw preview at cursor (dashed) when warship selected OR ghost mode | ||
| if ((this.selectedShow || this.ghostShow) && this.mouseWorldPos) { | ||
| this.drawPreviewPatrol(this.mouseWorldPos.x, this.mouseWorldPos.y); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Draw current patrol area with solid line square | ||
| */ | ||
| private drawCurrentPatrol(centerX: number, centerY: number) { | ||
| const ctx = this.context; | ||
| const patrolRange = this.game.config().warshipPatrolRange(); | ||
| const halfSize = patrolRange / 2; | ||
|
|
||
| ctx.save(); | ||
| ctx.lineWidth = 2; | ||
| ctx.strokeStyle = "rgba(0, 0, 0, 0.2)"; | ||
|
|
||
| ctx.beginPath(); | ||
| ctx.rect(centerX - halfSize, centerY - halfSize, patrolRange, patrolRange); | ||
| ctx.stroke(); | ||
|
|
||
| ctx.restore(); | ||
| } | ||
|
|
||
| /** | ||
| * Draw preview patrol area with dashed line square (animated) | ||
| */ | ||
| private drawPreviewPatrol(centerX: number, centerY: number) { | ||
| const ctx = this.context; | ||
| const patrolRange = this.game.config().warshipPatrolRange(); | ||
| const halfSize = patrolRange / 2; | ||
|
|
||
| ctx.save(); | ||
| ctx.lineWidth = 2; | ||
| ctx.setLineDash([12, 6]); | ||
| ctx.lineDashOffset = this.dashOffset; | ||
| ctx.strokeStyle = "rgba(0, 0, 0, 0.2)"; | ||
|
|
||
| ctx.beginPath(); | ||
| ctx.rect(centerX - halfSize, centerY - halfSize, patrolRange, patrolRange); | ||
| ctx.stroke(); | ||
|
|
||
| ctx.restore(); | ||
| } | ||
BCNelson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.