game-village-politics-frontend/app/components/village/Grid.vue

121 lines
4.6 KiB
Vue
Raw Normal View History

<script setup lang="ts">
import type { BuildingType, VillageState } from '~/types/game'
const props = defineProps<{ village: VillageState }>()
const selected = defineModel<{ x: number; y: number } | null>('selected', { default: null })
const { t } = useI18n()
interface Tile {
key: string
type: BuildingType
level: number
x: number
y: number
w: number
h: number
planned: boolean
}
// Buildings and queued new buildings as tiles spanning their footprint. The wall takes
// no cells; it shows as the frame around the grid instead.
const tiles = computed<Tile[]>(() => [
...props.village.buildings.map((b) => ({ ...b, key: `b${b.id}`, planned: false })),
...props.village.queue
.filter((o) => o.action === 'build')
.map((o) => ({ ...o, key: `o${o.id}`, level: 0, planned: true })),
].flatMap((it) => {
const [w, h] = it.footprint
return w && h ? [{ key: it.key, type: it.type, level: it.level, x: it.x, y: it.y, w, h, planned: it.planned }] : []
}))
const freeCells = computed(() => {
const taken = new Set<string>()
for (const t of tiles.value)
for (let dy = 0; dy < t.h; dy++) for (let dx = 0; dx < t.w; dx++) taken.add(`${t.x + dx},${t.y + dy}`)
const cells: { x: number; y: number }[] = []
for (let y = 0; y < props.village.grid.h; y++)
for (let x = 0; x < props.village.grid.w; x++) if (!taken.has(`${x},${y}`)) cells.push({ x, y })
return cells
})
const hasWall = computed(() => props.village.buildings.some((b) => b.type === 'wall'))
const gridStyle = computed(() => ({
gridTemplateColumns: `repeat(${props.village.grid.w}, minmax(0, 1fr))`,
gridTemplateRows: `repeat(${props.village.grid.h}, minmax(0, 1fr))`,
aspectRatio: `${props.village.grid.w} / ${props.village.grid.h}`,
}))
const place = (x: number, y: number, w = 1, h = 1) => ({
gridColumn: `${x + 1} / span ${w}`,
gridRow: `${y + 1} / span ${h}`,
})
const isSelected = (x: number, y: number) => selected.value?.x === x && selected.value?.y === y
function toggle(x: number, y: number) {
selected.value = isSelected(x, y) ? null : { x, y }
}
function tileTitle(tile: Tile) {
const name = t(`buildings.${tile.type}`)
return tile.planned ? `${name} (${t('village.grid.planned')})` : `${name}, ${t('village.buildings.level_n', { level: tile.level })}`
}
</script>
<template>
<section class="panel">
<div class="panel-head">
<h2 class="panel-title">{{ $t('village.grid.title') }}</h2>
<p class="text-xs text-ink-500">{{ $t('village.grid.hint') }}</p>
</div>
<div class="panel-body">
<div
class="rounded-xl bg-[#e6d6b5] p-1.5 sm:p-2"
:class="hasWall ? 'ring-[6px] ring-stone-400' : 'ring-1 ring-parchment-300'"
:title="hasWall ? $t('buildings.wall') : undefined"
>
<div class="grid gap-1" :style="gridStyle">
<button
v-for="c in freeCells"
:key="`${c.x},${c.y}`"
type="button"
class="group grid place-items-center rounded-md border transition-colors"
:class="
isSelected(c.x, c.y)
? 'border-amber-600 bg-amber-100 text-amber-700 ring-2 ring-amber-500'
: 'border-dashed border-[#cdb68c] bg-[#efe3c9] text-transparent hover:border-amber-500 hover:bg-amber-50 hover:text-amber-500'
"
:style="place(c.x, c.y)"
:title="`${$t('village.grid.free_cell')} ${c.x}|${c.y}`"
:aria-pressed="isSelected(c.x, c.y)"
@click="toggle(c.x, c.y)"
>
<IconUi name="pin" class="size-3/5 max-h-5 max-w-5" />
</button>
<div
v-for="tile in tiles"
:key="tile.key"
class="relative flex min-w-0 flex-col items-center justify-center gap-0.5 overflow-hidden rounded-lg border-2 p-1 text-center"
:class="tile.planned ? 'border-dashed opacity-60' : 'shadow-sm'"
:style="{ ...place(tile.x, tile.y, tile.w, tile.h), ...buildingTileStyle(tile.type) }"
:title="tileTitle(tile)"
>
<IconBuilding :type="tile.type" :class="tile.w > 1 ? 'size-6 sm:size-9' : 'size-4 sm:size-6'" />
<span v-if="tile.w > 1" class="hidden max-w-full truncate text-xs font-medium text-ink-700 sm:block">
{{ $t(`buildings.${tile.type}`) }}
</span>
<span
class="absolute top-0.5 right-0.5 rounded bg-white/85 px-1 text-[10px] leading-4 font-semibold text-ink-700 tabular-nums"
:class="tile.w > 1 ? '' : 'hidden sm:block'"
>
{{ tile.planned ? '…' : tile.level }}
</span>
</div>
</div>
</div>
</div>
</section>
</template>