feat: move warship (#196)

This commit is contained in:
Ilan Schemoul
2025-03-15 00:30:30 +01:00
committed by GitHub
parent baf91db288
commit 275de50fcd
8 changed files with 182 additions and 52 deletions
+20
View File
@@ -135,6 +135,13 @@ export class SendHashEvent implements GameEvent {
) {}
}
export class MoveWarshipIntentEvent implements GameEvent {
constructor(
public readonly unitId: number,
public readonly tile: number,
) {}
}
export class Transport {
private socket: WebSocket;
@@ -194,6 +201,9 @@ export class Transport {
this.eventBus.on(CancelAttackIntentEvent, (e) =>
this.onCancelAttackIntentEvent(e),
);
this.eventBus.on(MoveWarshipIntentEvent, (e) => {
this.onMoveWarshipEvent(e);
});
}
private startPing() {
@@ -522,6 +532,16 @@ export class Transport {
});
}
private onMoveWarshipEvent(event: MoveWarshipIntentEvent) {
this.sendIntent({
type: "move_warship",
clientID: this.lobbyConfig.clientID,
playerID: this.lobbyConfig.playerID,
unitId: event.unitId,
tile: event.tile,
});
}
private sendIntent(intent: Intent) {
if (this.isLocal || this.socket.readyState === WebSocket.OPEN) {
const msg = ClientIntentMessageSchema.parse({
+13 -12
View File
@@ -17,6 +17,7 @@ import {
} from "../../../core/game/GameMap";
import { GameUpdateType } from "../../../core/game/GameUpdates";
import { TransformHandler } from "../TransformHandler";
import { MoveWarshipIntentEvent } from "../../Transport";
enum Relationship {
Self,
@@ -44,7 +45,7 @@ export class UnitLayer implements Layer {
private selectedUnit: UnitView | null = null;
// Configuration for unit selection
private readonly WARSHIP_SELECTION_RADIUS = 3; // Radius in game cells for warship selection hit zone
private readonly WARSHIP_SELECTION_RADIUS = 10; // Radius in game cells for warship selection hit zone
constructor(
private game: GameView,
@@ -121,19 +122,19 @@ export class UnitLayer implements Layer {
// Find warships near this cell, sorted by distance
const nearbyWarships = this.findWarshipsNearCell(cell);
if (nearbyWarships.length > 0) {
if (this.selectedUnit) {
const clickRef = this.game.ref(cell.x, cell.y);
if (this.game.isOcean(clickRef)) {
this.eventBus.emit(
new MoveWarshipIntentEvent(this.selectedUnit.id(), clickRef),
);
}
// Deselect
this.eventBus.emit(new UnitSelectionEvent(this.selectedUnit, false));
} else if (nearbyWarships.length > 0) {
// Toggle selection of the closest warship
const clickedUnit = nearbyWarships[0];
if (this.selectedUnit === clickedUnit) {
// Deselect if already selected
this.eventBus.emit(new UnitSelectionEvent(clickedUnit, false));
} else {
// Select the new unit
this.eventBus.emit(new UnitSelectionEvent(clickedUnit, true));
}
} else if (this.selectedUnit) {
// If clicked elsewhere and there's a selection, deselect it
this.eventBus.emit(new UnitSelectionEvent(this.selectedUnit, false));
this.eventBus.emit(new UnitSelectionEvent(clickedUnit, true));
}
}