have GameRunner calculate Name location

This commit is contained in:
Evan
2025-01-03 20:47:39 -08:00
parent 12e535a21f
commit 37522b6467
5 changed files with 37 additions and 12 deletions
+8 -4
View File
@@ -1,5 +1,5 @@
import { Game, Player, Tile, Cell } from '../../core/game/Game';
import { GameView } from '../../core/GameView';
import { GameView, NameViewData } from '../../core/GameView';
import { calculateBoundingBox, within } from '../../core/Util';
export interface Point {
@@ -15,7 +15,7 @@ export interface Rectangle {
}
export function placeName(game: GameView, player: Player): [position: Cell, fontSize: number] {
export function placeName(game: Game, player: Player): NameViewData {
const boundingBox = calculateBoundingBox(player.borderTiles());
const rawScalingFactor = (boundingBox.max.x - boundingBox.min.x) / 50
@@ -36,10 +36,14 @@ export function placeName(game: GameView, player: Player): [position: Cell, font
const fontSize = calculateFontSize(largestRectangle, player.name());
center = new Cell(center.x, center.y - fontSize / 3)
return [center, fontSize]
return {
nameX: center.x,
nameY: center.y,
nameSize: fontSize,
}
}
export function createGrid(game: GameView, player: Player, boundingBox: { min: Point; max: Point }, scalingFactor: number): boolean[][] {
export function createGrid(game: Game, player: Player, boundingBox: { min: Point; max: Point }, scalingFactor: number): boolean[][] {
const scaledBoundingBox: { min: Point; max: Point } = {
min: {
x: Math.floor(boundingBox.min.x / scalingFactor),
+1 -1
View File
@@ -89,7 +89,7 @@ export class TransformHandler {
onGoToPlayer(event: GoToPlayerEvent) {
let unused = null;
this.clearTarget();
[this.target, unused] = placeName(this.game, event.player);
// [this.target, unused] = placeName(this.game, event.player);
this.intervalID = setInterval(() => this.goTo(), 1)
}
+3 -3
View File
@@ -153,9 +153,9 @@ export class NameLayer implements Layer {
render.fontSize = 0
return
}
const [cell, size] = placeName(this.game, render.player)
render.location = cell
render.fontSize = Math.max(1, Math.floor(size))
// const [cell, size] = placeName(this.game, render.player)
// render.location = cell
// render.fontSize = Math.max(1, Math.floor(size))
}
renderPlayerInfo(render: RenderInfo, context: CanvasRenderingContext2D, scale: number, uppperLeft: Cell, bottomRight: Cell) {
+17 -4
View File
@@ -1,3 +1,4 @@
import { placeName } from "../client/graphics/NameBoxCalculator";
import { getConfig } from "./configuration/Config";
import { EventBus } from "./EventBus";
import { Executor } from "./execution/ExecutionManager";
@@ -5,7 +6,7 @@ import { WinCheckExecution } from "./execution/WinCheckExecution";
import { Game, MutableGame, MutableTile, PlayerID, Tile, TileEvent } from "./game/Game";
import { createGame } from "./game/GameImpl";
import { loadTerrainMap } from "./game/TerrainMapLoader";
import { GameUpdateViewData, packTileData, PlayerViewData } from "./GameView";
import { GameUpdateViewData, NameViewData, packTileData, PlayerViewData } from "./GameView";
import { GameConfig, Turn } from "./Schemas";
export async function createGameRunner(gameID: string, gameConfig: GameConfig, callBack: (gu: GameUpdateViewData) => void): Promise<GameRunner> {
@@ -25,6 +26,8 @@ export class GameRunner {
private currTurn = 0
private isExecuting = false
private playerToName = new Map<PlayerID, NameViewData>()
constructor(
private game: MutableGame,
private eventBus: EventBus,
@@ -64,16 +67,26 @@ export class GameRunner {
this.currTurn++
this.game.executeNextTick()
if (this.game.ticks() % 10 == 0) {
this.game.players()
.forEach(p => this.playerToName.set(p.id(), placeName(this.game, p)))
}
const playerViewData = {}
for (const player of this.game.allPlayers()) {
const viewData = player.toViewData()
viewData.nameViewData = this.playerToName.get(player.id())
playerViewData[player.id()] = viewData
}
this.callBack({
tick: this.game.ticks(),
units: this.game.units().map(u => u.toViewData()),
packedTileUpdates: Array.from(this.updatedTiles).map(t => packTileData(t.toViewData())),
players: Object.fromEntries(
this.game.allPlayers().map(p => [p.id(), p.toViewData()])
) as Record<PlayerID, PlayerViewData>
players: playerViewData
})
this.isExecuting = false
}
+8
View File
@@ -98,7 +98,15 @@ export class UnitView {
}
}
export interface NameViewData {
nameX: number,
nameY: number,
nameSize: number,
}
export interface PlayerViewData extends ViewData<PlayerViewData> {
nameViewData?: NameViewData,
clientID: ClientID,
name: string,
displayName: string,