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

42 lines
1.7 KiB
Vue

<script setup lang="ts">
import type { Resource } from '~/types/game'
const props = defineProps<{
resource: Resource
amount: number
cap: number
/** Per hour; food can be negative. */
rate: number
}>()
const { t } = useI18n()
const share = computed(() => Math.max(0, Math.min(1, props.amount / props.cap)))
const full = computed(() => props.amount >= props.cap)
/** Seconds until the storage is full at this rate; null when it won't fill. */
const fullIn = computed(() => (props.rate > 0 && !full.value ? ((props.cap - props.amount) / props.rate) * 3600 : null))
const title = computed(() =>
[
`${t(`resources.${props.resource}`)}: ${formatAmount(props.amount)} / ${props.cap}`,
t('village.resources.per_hour', { rate: formatRate(props.rate) }),
full.value ? t('village.resources.full') : fullIn.value !== null ? t('village.resources.full_in', { time: formatDuration(fullIn.value) }) : null,
]
.filter(Boolean)
.join(' · '),
)
</script>
<!-- One resource in the village row: icon and amount, with a hairline of how full the storage is. -->
<template>
<span class="inline-flex min-w-0 items-center gap-1" :title="title">
<IconResource :resource="resource" class="size-4 shrink-0" />
<span class="flex min-w-0 flex-col">
<span class="leading-tight font-semibold tabular-nums" :class="full || rate < 0 ? 'text-blood-700' : 'text-ink-900'">
{{ formatAmount(amount) }}
</span>
<span class="h-0.5 w-full min-w-8 bg-parchment-300" aria-hidden="true">
<span class="block h-full" :class="full ? 'bg-blood-700' : share > 0.85 ? 'bg-gold-500' : 'bg-moss-600'" :style="{ width: `${share * 100}%` }" />
</span>
</span>
</span>
</template>