feat: upgrade from the village overview: a button next to each building's level queues the next level; hovering shows cost against the stock and build time

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-10-30 12:41:38 +01:00
parent 13f98cf8ae
commit 5ad3087326
4 changed files with 78 additions and 9 deletions

File diff suppressed because one or more lines are too long

View File

@ -185,6 +185,9 @@ useShortcuts(() => {
:resources="v.resources.value"
:dismiss="picked"
:seconds-until="v.secondsUntil"
:can-afford="v.canAfford"
:busy="v.busy.value"
@upgrade="v.upgrade"
/>
</div>
<!-- The map draws itself across the screen and puts its panels into the dock. -->

View File

@ -1,5 +1,5 @@
<script setup lang="ts">
import type { BuildingType, Resources, VillageState } from '~/types/game'
import type { BuildingType, Cost, Offer, Resources, VillageState } from '~/types/game'
import { buildScene, type SceneBuilding, type SceneInset, type ScenePlot } from '~/diorama/scene'
import type { BuildingTag } from '~/utils/buildingTags'
@ -17,7 +17,12 @@ const props = defineProps<{
resources?: Resources | null
/** A window or card is open: a click in the village closes it instead of opening another. */
dismiss?: boolean
/** Given: every tag gets an upgrade button next to its level (the overview). */
canAfford?: (cost: Cost) => boolean
/** A change is under way: the upgrade buttons wait. */
busy?: boolean
}>()
const emit = defineEmits<{ upgrade: [buildingId: number] }>()
/** Id of the building whose details are open. */
const selected = defineModel<number | null>('selected', { default: null })
/** Type whose empty plot was picked, to build it. */
@ -107,7 +112,7 @@ const k = computed(() => (view.value.w / Math.max(1, size.value.w)) * (size.valu
// Every building wears a tag like the old village views: its level and the one number
// that says what it does now (app/utils/buildingTags.ts). Sizes are screen pixels, scaled
// by k into the scene, so tags read the same however the village is scaled.
const TAG = { h: 20, chip: 20, icon: 14, gap: 4, pad: 6, font: 12 }
const TAG = { h: 20, chip: 20, btn: 18, icon: 14, gap: 4, pad: 6, font: 12 }
const FONT = 'ui-sans-serif, system-ui, sans-serif'
let measurer: CanvasRenderingContext2D | null = null
const widths = new Map<string, number>()
@ -129,22 +134,37 @@ const TONES = {
const badges = computed(() => {
const K = k.value
const list = scene.value.objects.flatMap((o) => (o.building?.focusable ? [o.building] : []))
const placed: { b: SceneBuilding; info: BuildingTag | null; x: number; y: number; w: number; h: number }[] = []
const placed: { b: SceneBuilding; info: BuildingTag | null; offer: Offer | null; btn: number; x: number; y: number; w: number; h: number }[] = []
const overlaps = (x: number, y: number, w: number, h: number) =>
placed.some((p) => Math.abs(p.x - x) < (p.w + w) / 2 + 2 * K && Math.abs(p.y - y) < (p.h + h) / 2 + 2 * K)
for (const b of [...list].sort((p, q) => q.label[1] - p.label[1])) {
const building = props.village.buildings.find((x) => x.id === b.id)
const info = building && props.resources ? buildingTag(props.village, building, props.resources, props.secondsUntil, t) : null
const inner = info ? TAG.pad + (info.icon ? TAG.icon + TAG.gap : 0) + textWidth(info.text) + TAG.pad : 0
const w = (TAG.chip + inner) * K
// The next level, one click away from the overview.
const offer = props.canAfford ? (building?.upgrade ?? null) : null
const btn = offer ? TAG.btn : 0
const w = (TAG.chip + btn + inner) * K
const h = TAG.h * K
const [x] = b.label
let y = b.label[1]
for (let n = 0; n < 8 && overlaps(x, y, w, h); n++) y -= h * 1.2
placed.push({ b, info, x, y, w, h })
placed.push({ b, info, offer, btn, x, y, w, h })
}
return placed
})
// The upgrade button's card: target level, cost against the stock, build time.
const tip = ref<{ id: number; x: number; y: number } | null>(null)
const tipOffer = computed(() => (tip.value ? (badges.value.find((p) => p.b.id === tip.value!.id)?.offer ?? null) : null))
function showTip(id: number, e: Event) {
const r = (e.currentTarget as Element).getBoundingClientRect()
tip.value = { id, x: r.left + r.width / 2, y: r.top }
}
const affordable = (offer: Offer) => !offer.blocked && !!props.canAfford?.(offer.cost)
function upgradeNow(id: number, offer: Offer) {
if (!props.busy && affordable(offer)) emit('upgrade', id)
}
const tagTitle = (b: SceneBuilding, info: BuildingTag | null) => [label(b), info?.title].filter(Boolean).join(' · ')
// Countdowns sit right above what is being built: over the level badge, or over the
@ -236,7 +256,7 @@ const timers = computed(() => {
<!-- Building tags (level and what it does now) and countdowns. Flat colours only. -->
<g class="pointer-events-none" :font-family="FONT" text-anchor="middle">
<g
v-for="{ b, info, x, y, w } in badges"
v-for="{ b, info, offer, btn, x, y, w } in badges"
:key="`badge-${b.id}`"
class="pointer-events-auto cursor-pointer"
:transform="`translate(${x - w / 2} ${y}) scale(${k})`"
@ -253,14 +273,40 @@ const timers = computed(() => {
/>
<rect :width="TAG.chip" :height="TAG.h" :y="-TAG.h / 2" :fill="b.upgrading ? '#c49a2c' : '#57391f'" />
<text :x="TAG.chip / 2" :y="4.3" :font-size="TAG.font" font-weight="700" :fill="b.upgrading ? '#1f150c' : '#f3e7c9'">{{ b.level }}</text>
<!-- Upgrade right here: green when the stock covers it, grey when not. -->
<g
v-if="offer"
role="button"
:tabindex="0"
:aria-label="$t('village.diorama.upgrade_aria', { name: label(b), level: offer.target_level })"
:aria-disabled="!affordable(offer) || busy"
:class="affordable(offer) && !busy ? 'cursor-pointer' : 'cursor-not-allowed'"
@click.stop="upgradeNow(b.id, offer)"
@keydown.enter.stop.prevent="upgradeNow(b.id, offer)"
@pointerenter="showTip(b.id, $event)"
@pointerleave="tip = null"
@focus="showTip(b.id, $event)"
@blur="tip = null"
>
<title />
<rect :x="TAG.chip" :width="TAG.btn" :height="TAG.h" :y="-TAG.h / 2" :fill="affordable(offer) ? '#4f6b2a' : '#cdbf9e'" />
<path
:d="`M${TAG.chip + TAG.btn / 2} -5.5v11M${TAG.chip + TAG.btn / 2 - 4.5} -1l4.5 -4.5 4.5 4.5`"
fill="none"
:stroke="affordable(offer) ? '#f3e7c9' : '#8a7d63'"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
/>
</g>
<template v-if="info">
<g v-if="info.icon" :transform="`translate(${TAG.chip + TAG.pad} ${-TAG.icon / 2})`" :style="{ color: TONES[info.tone].icon }">
<g v-if="info.icon" :transform="`translate(${TAG.chip + btn + TAG.pad} ${-TAG.icon / 2})`" :style="{ color: TONES[info.tone].icon }">
<IconResource v-if="info.icon.kind === 'resource'" :resource="info.icon.name" :width="TAG.icon" :height="TAG.icon" />
<IconUnit v-else-if="info.icon.kind === 'unit'" :unit="info.icon.name" :width="TAG.icon" :height="TAG.icon" />
<IconUi v-else :name="info.icon.name" :width="TAG.icon" :height="TAG.icon" />
</g>
<text
:x="TAG.chip + TAG.pad + (info.icon ? TAG.icon + TAG.gap : 0)"
:x="TAG.chip + btn + TAG.pad + (info.icon ? TAG.icon + TAG.gap : 0)"
:y="4.3"
text-anchor="start"
:font-size="TAG.font"
@ -304,6 +350,24 @@ const timers = computed(() => {
+
</button>
</div>
<!-- The upgrade button's card, over everything. -->
<Teleport to="body">
<div
v-if="tip && tipOffer"
class="float pointer-events-none fixed z-50 -translate-x-1/2 -translate-y-full border border-wood-900 bg-parchment-100 px-2 py-1.5 text-sm"
:style="{ left: `${tip.x}px`, top: `${tip.y - 6}px` }"
role="tooltip"
>
<p class="mb-1 font-semibold whitespace-nowrap text-wood-900">{{ $t('village.diorama.upgrade_to', { level: tipOffer.target_level }) }}</p>
<div class="flex items-center gap-3 whitespace-nowrap">
<UiCost :cost="tipOffer.cost" :have="resources ?? undefined" nowrap />
<span class="inline-flex items-center gap-1 text-ink-700 tabular-nums"><IconUi name="clock" class="size-3.5" />{{ formatDuration(tipOffer.seconds) }}</span>
</div>
<p v-if="tipOffer.blocked" class="mt-1 text-xs font-semibold text-blood-700">{{ $t(`village.blocked.${tipOffer.blocked}`) }}</p>
<p v-else-if="!affordable(tipOffer)" class="mt-1 text-xs font-semibold text-blood-700">{{ $t('village.blocked.resources') }}</p>
</div>
</Teleport>
</div>
</template>

View File

@ -285,6 +285,8 @@ export default {
title: 'Village overview',
label: '{name}, level {level}',
planned: 'Under construction',
upgrade_to: 'Upgrade to level {level}',
upgrade_aria: '{name}: upgrade to level {level}',
plot: '{name}: not built yet',
plot_another: '{name}: room for another',
space: 'Building space',