feat: countdown above every building under construction in the village view
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
1db4eb28a9
commit
af096c6d08
|
|
@ -2,7 +2,11 @@
|
||||||
import type { VillageState } from '~/types/game'
|
import type { VillageState } from '~/types/game'
|
||||||
import { buildScene, type SceneBuilding } from '~/diorama/scene'
|
import { buildScene, type SceneBuilding } from '~/diorama/scene'
|
||||||
|
|
||||||
const props = defineProps<{ village: VillageState }>()
|
const props = defineProps<{
|
||||||
|
village: VillageState
|
||||||
|
/** Whole seconds until a server timestamp, on the server's clock. */
|
||||||
|
secondsUntil: (iso: string) => number
|
||||||
|
}>()
|
||||||
/** Id of the building whose details are open. */
|
/** Id of the building whose details are open. */
|
||||||
const selected = defineModel<number | null>('selected', { default: null })
|
const selected = defineModel<number | null>('selected', { default: null })
|
||||||
const { t } = useI18n()
|
const { t } = useI18n()
|
||||||
|
|
@ -56,6 +60,31 @@ const badges = computed(() => {
|
||||||
}
|
}
|
||||||
return placed
|
return placed
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Countdowns sit right above what is being built: over the level badge (and its name
|
||||||
|
// tag while that is showing), or over the scaffold of a new building.
|
||||||
|
// A timer that would cover another timer or a level badge moves up until it is clear.
|
||||||
|
const timers = computed(() => {
|
||||||
|
const K = k.value
|
||||||
|
const half = { w: 36 * K, h: 11 * K }
|
||||||
|
const placed: { x: number; y: number }[] = []
|
||||||
|
const hits = (x: number, y: number) =>
|
||||||
|
placed.some((p) => Math.abs(p.x - x) < half.w * 2 && Math.abs(p.y - y) < half.h * 2) ||
|
||||||
|
badges.value.some((p) => Math.abs(p.x - x) < half.w + 11 * K && Math.abs(p.y - y) < half.h + 11 * K)
|
||||||
|
return scene.value.timers
|
||||||
|
.map((tm) => {
|
||||||
|
const badge = tm.buildingId !== null ? badges.value.find((p) => p.b.id === tm.buildingId) : undefined
|
||||||
|
const lift = badge ? (isActive(badge.b.id) ? 52 : 25) : 2
|
||||||
|
return { ...tm, x: badge?.x ?? tm.at[0], y: (badge?.y ?? tm.at[1]) - lift * K }
|
||||||
|
})
|
||||||
|
.sort((a, b) => b.y - a.y)
|
||||||
|
.map((tm) => {
|
||||||
|
let y = tm.y
|
||||||
|
for (let n = 0; n < 8 && hits(tm.x, y); n++) y -= half.h * 2.1
|
||||||
|
placed.push({ x: tm.x, y })
|
||||||
|
return { ...tm, y }
|
||||||
|
})
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|
@ -129,6 +158,17 @@ const badges = computed(() => {
|
||||||
/>
|
/>
|
||||||
<text :y="4 * k" :font-size="11 * k" font-weight="700" :fill="b.upgrading ? '#1f150c' : '#2f2013'">{{ b.level }}</text>
|
<text :y="4 * k" :font-size="11 * k" font-weight="700" :fill="b.upgrading ? '#1f150c' : '#2f2013'">{{ b.level }}</text>
|
||||||
</g>
|
</g>
|
||||||
|
|
||||||
|
<g v-for="tm in timers" :key="`timer-${tm.orderId}`" :transform="`translate(${tm.x} ${tm.y})`">
|
||||||
|
<rect :x="-34 * k" :y="-10 * k" :width="68 * k" :height="19 * k" :rx="2 * k" fill="#2f2013" stroke="#c49a2c" :stroke-width="1.2 * k" />
|
||||||
|
<g :transform="`translate(${-24 * k} ${-0.5 * k}) scale(${k})`" fill="none" stroke="#d9b44a" stroke-width="1.4" stroke-linecap="round">
|
||||||
|
<circle r="5" />
|
||||||
|
<path d="M0 -3v3l2 1.5" />
|
||||||
|
</g>
|
||||||
|
<text :x="6 * k" :y="4 * k" :font-size="11.5 * k" font-weight="700" fill="#f3e7c9" class="tabular-nums">
|
||||||
|
{{ formatDuration(secondsUntil(tm.finishesAt)) }}
|
||||||
|
</text>
|
||||||
|
</g>
|
||||||
</g>
|
</g>
|
||||||
</svg>
|
</svg>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -62,6 +62,16 @@ export interface SceneObject {
|
||||||
planned?: boolean
|
planned?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** A running build order's countdown, anchored above what it works on. */
|
||||||
|
export interface SceneTimer {
|
||||||
|
orderId: number
|
||||||
|
finishesAt: string
|
||||||
|
/** Screen point above the roof or scaffold. */
|
||||||
|
at: V2
|
||||||
|
/** The building whose level badge sits at this spot; the timer goes above that badge. */
|
||||||
|
buildingId: number | null
|
||||||
|
}
|
||||||
|
|
||||||
export interface Scene {
|
export interface Scene {
|
||||||
viewBox: string
|
viewBox: string
|
||||||
/** viewBox size, for keeping badges a fixed size on screen. */
|
/** viewBox size, for keeping badges a fixed size on screen. */
|
||||||
|
|
@ -70,6 +80,7 @@ export interface Scene {
|
||||||
terrain: Poly[]
|
terrain: Poly[]
|
||||||
decals: Poly[]
|
decals: Poly[]
|
||||||
objects: SceneObject[]
|
objects: SceneObject[]
|
||||||
|
timers: SceneTimer[]
|
||||||
}
|
}
|
||||||
|
|
||||||
/** A scene object with its painter order (x + y of its ground point). */
|
/** A scene object with its painter order (x + y of its ground point). */
|
||||||
|
|
@ -327,6 +338,9 @@ export function buildScene(village: VillageState, aspect = 2.2): Scene {
|
||||||
const running = new Set(village.queue.filter((o) => o.builder !== null && o.building_id !== null).map((o) => o.building_id))
|
const running = new Set(village.queue.filter((o) => o.builder !== null && o.building_id !== null).map((o) => o.building_id))
|
||||||
const objects: Ordered[] = []
|
const objects: Ordered[] = []
|
||||||
const labelTops: number[] = []
|
const labelTops: number[] = []
|
||||||
|
/** Badge point per building id, and per queued new building (order id), for the timers. */
|
||||||
|
const anchorOfBuilding = new Map<number, V2>()
|
||||||
|
const anchorOfOrder = new Map<number, V2>()
|
||||||
|
|
||||||
// Buildings and queued new ones.
|
// Buildings and queued new ones.
|
||||||
for (const b of village.buildings) {
|
for (const b of village.buildings) {
|
||||||
|
|
@ -339,6 +353,7 @@ export function buildScene(village: VillageState, aspect = 2.2): Scene {
|
||||||
const faces = upgrading ? [...model.faces, ...crane(s, model.height)] : model.faces
|
const faces = upgrading ? [...model.faces, ...crane(s, model.height)] : model.faces
|
||||||
const label = project([slot.x, slot.y, model.height + 10])
|
const label = project([slot.x, slot.y, model.height + 10])
|
||||||
labelTops.push(label[1])
|
labelTops.push(label[1])
|
||||||
|
anchorOfBuilding.set(b.id, label)
|
||||||
objects.push({
|
objects.push({
|
||||||
key: `b${b.id}`,
|
key: `b${b.id}`,
|
||||||
depth: slot.x + slot.y,
|
depth: slot.x + slot.y,
|
||||||
|
|
@ -349,6 +364,7 @@ export function buildScene(village: VillageState, aspect = 2.2): Scene {
|
||||||
for (const o of planned) {
|
for (const o of planned) {
|
||||||
const slot = slotOf(`o${o.id}`)
|
const slot = slotOf(`o${o.id}`)
|
||||||
const model = scaffold(ZONE_OF[o.type] ? slot.s : halfSize(o.space))
|
const model = scaffold(ZONE_OF[o.type] ? slot.s : halfSize(o.space))
|
||||||
|
anchorOfOrder.set(o.id, project([slot.x, slot.y, model.height + 10]))
|
||||||
objects.push({ key: `o${o.id}`, depth: slot.x + slot.y, polys: toPolys(model.faces, [slot.x, slot.y, 0]), planned: true })
|
objects.push({ key: `o${o.id}`, depth: slot.x + slot.y, polys: toPolys(model.faces, [slot.x, slot.y, 0]), planned: true })
|
||||||
}
|
}
|
||||||
const wellSlot = slotOf('well')
|
const wellSlot = slotOf('well')
|
||||||
|
|
@ -365,6 +381,8 @@ export function buildScene(village: VillageState, aspect = 2.2): Scene {
|
||||||
const gateSide = FRONT + deg(24)
|
const gateSide = FRONT + deg(24)
|
||||||
const label = project([Math.cos(gateSide) * Rv, Math.sin(gateSide) * Rv, wallTop + 10])
|
const label = project([Math.cos(gateSide) * Rv, Math.sin(gateSide) * Rv, wallTop + 10])
|
||||||
labelTops.push(label[1])
|
labelTops.push(label[1])
|
||||||
|
if (wall) anchorOfBuilding.set(wall.id, label)
|
||||||
|
else for (const o of village.queue) if (o.action === 'build' && o.type === 'wall') anchorOfOrder.set(o.id, label)
|
||||||
const ring = { cx: 0, cy: 0, rx: 0, ry: 0 }
|
const ring = { cx: 0, cy: 0, rx: 0, ry: 0 }
|
||||||
// The segment nearest the badge takes keyboard focus for the whole wall.
|
// The segment nearest the badge takes keyboard focus for the whole wall.
|
||||||
const focusIndex = segments.reduce((best, seg, i) => (seg.depth > segments[best]!.depth ? i : best), 0)
|
const focusIndex = segments.reduce((best, seg, i) => (seg.depth > segments[best]!.depth ? i : best), 0)
|
||||||
|
|
@ -490,6 +508,15 @@ export function buildScene(village: VillageState, aspect = 2.2): Scene {
|
||||||
objects.push({ key: `stone-${k}`, depth: x + y, polys: toPolys(rock(x, y, U * (0.45 + 0.4 * hash(k, 73)), 200 + k)) })
|
objects.push({ key: `stone-${k}`, depth: x + y, polys: toPolys(rock(x, y, U * (0.45 + 0.4 * hash(k, 73)), 200 + k)) })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Countdowns for the orders builders are working on, above what they change.
|
||||||
|
const timers: SceneTimer[] = []
|
||||||
|
for (const o of village.queue) {
|
||||||
|
if (!o.finishes_at) continue
|
||||||
|
const onBuilding = o.building_id !== null ? anchorOfBuilding.get(o.building_id) : undefined
|
||||||
|
const at = onBuilding ?? anchorOfOrder.get(o.id)
|
||||||
|
if (at) timers.push({ orderId: o.id, finishesAt: o.finishes_at, at, buildingId: onBuilding ? o.building_id : null })
|
||||||
|
}
|
||||||
|
|
||||||
objects.sort((a, b) => a.depth - b.depth)
|
objects.sort((a, b) => a.depth - b.depth)
|
||||||
return {
|
return {
|
||||||
viewBox: `${view.x.toFixed(1)} ${view.y.toFixed(1)} ${w.toFixed(1)} ${h.toFixed(1)}`,
|
viewBox: `${view.x.toFixed(1)} ${view.y.toFixed(1)} ${w.toFixed(1)} ${h.toFixed(1)}`,
|
||||||
|
|
@ -499,5 +526,6 @@ export function buildScene(village: VillageState, aspect = 2.2): Scene {
|
||||||
// One at a time, so the layers keep the order they were added in.
|
// One at a time, so the layers keep the order they were added in.
|
||||||
decals: decalFaces.flatMap((face) => toPolys([face])),
|
decals: decalFaces.flatMap((face) => toPolys([face])),
|
||||||
objects: objects.map(({ key, polys, building, planned }) => ({ key, polys, building, planned })),
|
objects: objects.map(({ key, polys, building, planned }) => ({ key, polys, building, planned })),
|
||||||
|
timers,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -60,7 +60,7 @@ function showInVillage(id: number) {
|
||||||
|
|
||||||
<div class="relative">
|
<div class="relative">
|
||||||
<div class="h-[clamp(260px,82vw,460px)] sm:h-[clamp(340px,62vh,760px)]">
|
<div class="h-[clamp(260px,82vw,460px)] sm:h-[clamp(340px,62vh,760px)]">
|
||||||
<VillageDiorama v-model:selected="selected" :village="village" />
|
<VillageDiorama v-model:selected="selected" :village="village" :seconds-until="v.secondsUntil" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p
|
<p
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue