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

49 lines
2.1 KiB
Vue
Raw Normal View History

<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, everything about it side by side: the amount against
the storage limit, what comes in per hour, and a bar of how full the storage is.
-->
<template>
<span class="inline-flex min-w-0 items-center gap-1 sm:gap-1.5" :title="title">
<IconResource :resource="resource" class="size-4 shrink-0 sm:size-5" />
<span class="flex min-w-0 flex-col leading-tight">
<span class="whitespace-nowrap tabular-nums">
<span class="font-semibold" :class="full ? 'text-blood-700' : 'text-ink-900'">{{ formatAmount(amount) }}</span>
<span class="hidden text-xs text-ink-500 sm:inline">/{{ cap }}</span>
</span>
<span class="text-xs whitespace-nowrap tabular-nums" :class="full || rate < 0 ? 'font-semibold text-blood-700' : 'text-moss-700'">
{{ full ? $t('village.resources.full_short') : $t('village.resources.per_hour', { rate: formatRate(rate) }) }}
</span>
<span class="mt-0.5 h-1 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>