93 lines
2.9 KiB
Vue
93 lines
2.9 KiB
Vue
<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 })
|
|
|
|
interface Item {
|
|
key: string
|
|
type: BuildingType
|
|
level: number
|
|
w: number
|
|
h: number
|
|
planned: boolean
|
|
}
|
|
type Cell = { key: string; item: Item } | { key: string; item: null; x: number; y: number }
|
|
|
|
// A plain table: each building is one cell spanning its footprint, queued new buildings
|
|
// show as planned, and the remaining cells can be picked as the next build position.
|
|
const rows = computed(() => {
|
|
const { w, h } = props.village.grid
|
|
const slots: (Item | 'covered' | null)[][] = Array.from({ length: h }, () => Array(w).fill(null))
|
|
const items = [
|
|
...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 })),
|
|
]
|
|
for (const it of items) {
|
|
const [fw, fh] = it.footprint
|
|
if (fw === 0 || fh === 0) continue
|
|
for (let dy = 0; dy < fh; dy++) for (let dx = 0; dx < fw; dx++) slots[it.y + dy]![it.x + dx] = 'covered'
|
|
slots[it.y]![it.x] = { key: it.key, type: it.type, level: it.level, w: fw, h: fh, planned: it.planned }
|
|
}
|
|
return slots.map((row, y) =>
|
|
row.flatMap((slot, x): Cell[] =>
|
|
slot === 'covered' ? [] : slot ? [{ key: slot.key, item: slot }] : [{ key: `${x},${y}`, item: null, x, y }],
|
|
),
|
|
)
|
|
})
|
|
|
|
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 }
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<section>
|
|
<h2>{{ $t('village.grid.title') }}</h2>
|
|
<p><small>{{ $t('village.grid.hint') }}</small></p>
|
|
<table class="grid">
|
|
<tbody>
|
|
<tr v-for="(row, y) in rows" :key="y">
|
|
<template v-for="cell in row" :key="cell.key">
|
|
<td v-if="cell.item" :colspan="cell.item.w" :rowspan="cell.item.h" :class="{ planned: cell.item.planned }">
|
|
{{ $t(`buildings.${cell.item.type}`) }}
|
|
<template v-if="cell.item.planned">({{ $t('village.grid.planned') }})</template>
|
|
<template v-else>{{ cell.item.level }}</template>
|
|
</td>
|
|
<td v-else class="free" :class="{ selected: isSelected(cell.x, cell.y) }" @click="toggle(cell.x, cell.y)">
|
|
{{ cell.x }}|{{ cell.y }}
|
|
</td>
|
|
</template>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</section>
|
|
</template>
|
|
|
|
<style scoped>
|
|
.grid {
|
|
border-collapse: collapse;
|
|
}
|
|
.grid td {
|
|
border: 1px solid;
|
|
width: 5em;
|
|
height: 2.5em;
|
|
text-align: center;
|
|
}
|
|
.free {
|
|
cursor: pointer;
|
|
opacity: 0.5;
|
|
}
|
|
.selected {
|
|
outline: 3px solid;
|
|
opacity: 1;
|
|
}
|
|
.planned {
|
|
font-style: italic;
|
|
}
|
|
</style>
|