diff --git a/app/components/map/WorldMap.vue b/app/components/map/WorldMap.vue index d93b18d..1a84836 100644 --- a/app/components/map/WorldMap.vue +++ b/app/components/map/WorldMap.vue @@ -2,7 +2,16 @@ import type { MapVillage, MapView } from '~/types/map' const props = defineProps<{ - view: MapView + /** Loaded villages; fields outside it show plain land until the next load. */ + view: MapView | null + /** Visible area in fields: left/top edge (fractional) and how many fit. */ + left: number + top: number + cols: number + rows: number + /** Map size in px; the map fills it exactly. */ + width: number + height: number /** Field size in px. */ field: number selected: { x: number; y: number } | null @@ -10,14 +19,18 @@ const props = defineProps<{ const emit = defineEmits<{ select: [at: { x: number; y: number }] hover: [village: MapVillage | null] - /** Move the centre by whole fields (drag or arrow keys). */ + /** Pixel movement while dragging, for smooth panning. */ + drag: [dx: number, dy: number] + dragend: [] + /** Whole fields, from the arrow keys. */ pan: [dx: number, dy: number] }>() const { t } = useI18n() const RULER = 26 +const mapSize = computed(() => props.view?.size ?? 1000) -const villageAt = computed(() => new Map(props.view.villages.map((v) => [`${v.x}|${v.y}`, v]))) +const villageAt = computed(() => new Map((props.view?.villages ?? []).map((v) => [`${v.x}|${v.y}`, v]))) // Same field, same look: terrain comes from a hash of the coordinates. const hash = (x: number, y: number) => { @@ -27,11 +40,16 @@ const hash = (x: number, y: number) => { } const GRASS = ['#7f9645', '#78903f', '#86994b', '#73893c'] +// Whole fields touching the visible area. These only change when an edge crosses a +// field, so dragging moves one transform instead of re-rendering every field. +const x0 = computed(() => Math.max(0, Math.floor(props.left))) +const y0 = computed(() => Math.max(0, Math.floor(props.top))) +const x1 = computed(() => Math.min(mapSize.value, Math.ceil(props.left + props.cols))) +const y1 = computed(() => Math.min(mapSize.value, Math.ceil(props.top + props.rows))) + interface Field { x: number y: number - px: number - py: number grass: string village: MapVillage | null forest: number[] | null @@ -39,17 +57,13 @@ interface Field { const fields = computed(() => { const out: Field[] = [] - for (let row = 0; row < props.view.h; row++) { - for (let col = 0; col < props.view.w; col++) { - const x = props.view.x + col - const y = props.view.y + row + for (let y = y0.value; y < y1.value; y++) { + for (let x = x0.value; x < x1.value; x++) { const r = hash(x, y) const village = villageAt.value.get(`${x}|${y}`) ?? null out.push({ x, y, - px: col * props.field, - py: row * props.field, grass: GRASS[Math.floor(r * GRASS.length)]!, village, // About one empty field in five is forest: 2–3 trees at hashed spots. @@ -59,77 +73,82 @@ const fields = computed(() => { } return out }) - const villageFields = computed(() => fields.value.filter((f) => f.village)) const tier = (points: number): 1 | 2 | 3 => (points < 60 ? 1 : points < 200 ? 2 : 3) const relation = (v: MapVillage) => (v.own ? 'own' : v.player ? 'player' : 'barbarian') -const width = computed(() => RULER + props.view.w * props.field) -const height = computed(() => RULER + props.view.h * props.field) - // Sector lines every 10 fields, continent borders every 100. const lines = computed(() => { + const F = props.field const out: { x1: number; y1: number; x2: number; y2: number; kind: 'sector' | 'continent' }[] = [] - for (let col = 0; col <= props.view.w; col++) { - const x = props.view.x + col - if (x % 10 === 0) { - out.push({ x1: col * props.field, y1: 0, x2: col * props.field, y2: props.view.h * props.field, kind: x % 100 === 0 ? 'continent' : 'sector' }) - } + for (let x = x0.value; x <= x1.value; x++) { + if (x % 10 === 0) out.push({ x1: x * F, y1: y0.value * F, x2: x * F, y2: y1.value * F, kind: x % 100 === 0 ? 'continent' : 'sector' }) } - for (let row = 0; row <= props.view.h; row++) { - const y = props.view.y + row - if (y % 10 === 0) { - out.push({ x1: 0, y1: row * props.field, x2: props.view.w * props.field, y2: row * props.field, kind: y % 100 === 0 ? 'continent' : 'sector' }) + for (let y = y0.value; y <= y1.value; y++) { + if (y % 10 === 0) out.push({ x1: x0.value * F, y1: y * F, x2: x1.value * F, y2: y * F, kind: y % 100 === 0 ? 'continent' : 'sector' }) + } + return out +}) + +// One label per continent in view, at its top-left corner or the first fully visible field. +const continents = computed(() => { + const out: { label: string; x: number; y: number }[] = [] + const left = Math.ceil(props.left) + const top = Math.ceil(props.top) + for (let cy = Math.floor(y0.value / 100); cy * 100 < y1.value; cy++) { + for (let cx = Math.floor(x0.value / 100); cx * 100 < x1.value; cx++) { + out.push({ label: `K${cy}${cx}`, x: Math.max(cx * 100, left), y: Math.max(cy * 100, top) }) } } return out }) -// One label per continent in view, in its top-left visible corner. -const continents = computed(() => { - const seen = new Map() - for (const f of fields.value) { - const label = `K${Math.floor(f.y / 100)}${Math.floor(f.x / 100)}` - if (!seen.has(label)) seen.set(label, { label, px: f.px, py: f.py }) - } - return [...seen.values()] -}) - +const columns = computed(() => Array.from({ length: x1.value - x0.value }, (_, i) => x0.value + i)) +const rowsList = computed(() => Array.from({ length: y1.value - y0.value }, (_, i) => y0.value + i)) const rulerFont = computed(() => Math.max(9, Math.min(12, props.field / 4))) -// Dragging pans by whole fields when released; a click without movement selects. -const drag = ref<{ startX: number; startY: number; dx: number; dy: number; moved: boolean } | null>(null) +const shiftX = computed(() => RULER - props.left * props.field) +const shiftY = computed(() => RULER - props.top * props.field) + +// Dragging pans pixel by pixel; a click without movement selects a field. +let drag: { lastX: number; lastY: number; moved: boolean } | null = null +const dragging = ref(false) function onPointerDown(e: PointerEvent) { if (e.button !== 0) return - drag.value = { startX: e.clientX, startY: e.clientY, dx: 0, dy: 0, moved: false } + drag = { lastX: e.clientX, lastY: e.clientY, moved: false } ;(e.currentTarget as Element).setPointerCapture(e.pointerId) } function onPointerMove(e: PointerEvent) { - if (!drag.value) return - drag.value.dx = e.clientX - drag.value.startX - drag.value.dy = e.clientY - drag.value.startY - if (Math.abs(drag.value.dx) + Math.abs(drag.value.dy) > 5) drag.value.moved = true + if (!drag) return + const dx = e.clientX - drag.lastX + const dy = e.clientY - drag.lastY + if (!drag.moved && Math.abs(dx) + Math.abs(dy) < 4) return + drag.moved = true + dragging.value = true + drag.lastX = e.clientX + drag.lastY = e.clientY + emit('drag', dx, dy) } function onPointerUp(e: PointerEvent) { - const d = drag.value - drag.value = null + const d = drag + drag = null + dragging.value = false if (!d) return if (d.moved) { - const dx = -Math.round(d.dx / props.field) - const dy = -Math.round(d.dy / props.field) - if (dx || dy) emit('pan', dx, dy) + emit('dragend') return } const rect = (e.currentTarget as Element).getBoundingClientRect() - const col = Math.floor((e.clientX - rect.left - RULER) / props.field) - const row = Math.floor((e.clientY - rect.top - RULER) / props.field) - if (col >= 0 && row >= 0 && col < props.view.w && row < props.view.h) { - emit('select', { x: props.view.x + col, y: props.view.y + row }) - } + const px = e.clientX - rect.left - RULER + const py = e.clientY - rect.top - RULER + if (px < 0 || py < 0) return + const x = Math.floor(props.left + px / props.field) + const y = Math.floor(props.top + py / props.field) + if (x >= 0 && y >= 0 && x < mapSize.value && y < mapSize.value) emit('select', { x, y }) } function onKey(e: KeyboardEvent) { @@ -145,8 +164,6 @@ function onKey(e: KeyboardEvent) { e.preventDefault() emit('pan', move[0], move[1]) } - -const panOffset = computed(() => (drag.value?.moved ? `translate(${drag.value.dx} ${drag.value.dy})` : undefined)) diff --git a/app/pages/map/[worldId].vue b/app/pages/map/[worldId].vue index 637827c..b830758 100644 --- a/app/pages/map/[worldId].vue +++ b/app/pages/map/[worldId].vue @@ -3,38 +3,24 @@ import type { MapVillage } from '~/types/map' const route = useRoute() const router = useRouter() -const { view, error, busy, load } = useMap(Number(route.params.worldId)) -useCurrentWorld().enter(Number(route.params.worldId)) - -// The minimap: a 61 × 61 window around the same centre, refetched when the centre moves. -const overview = useMap(Number(route.params.worldId)) -watch( - () => view.value?.center, - (c, old) => { - if (c && (c.x !== old?.x || c.y !== old?.y)) overview.load(c, 61, 61) - }, -) +const worldId = Number(route.params.worldId) +const { view, error, busy, load } = useMap(worldId) +useCurrentWorld().enter(worldId) const ZOOM = [36, 52, 72] const zoom = ref(1) const field = computed(() => ZOOM[zoom.value]!) - -// The window fills the space the page gives it: odd sizes keep the centre field in the middle. -const box = ref(null) -const size = ref({ w: 15, h: 11 }) -const odd = (n: number) => (n % 2 ? n : n - 1) +const RULER = 26 const clamp = (n: number, lo: number, hi: number) => Math.max(lo, Math.min(hi, n)) +// The map fills its box exactly: full width, and the height left on screen. +const box = ref(null) +const area = ref({ w: 800, h: 520 }) function measure() { if (!box.value) return const rect = box.value.getBoundingClientRect() - const height = Math.max(360, window.innerHeight - rect.top - 24) - size.value = { - w: clamp(odd(Math.floor((rect.width - 26) / field.value)), 5, 41), - h: clamp(odd(Math.floor((height - 26) / field.value)), 5, 41), - } + area.value = { w: Math.floor(rect.width) - 8, h: Math.max(360, Math.floor(window.innerHeight - rect.top - 32)) } } - let observer: ResizeObserver | undefined onMounted(() => { measure() @@ -42,39 +28,101 @@ onMounted(() => { if (box.value) observer.observe(box.value) }) onBeforeUnmount(() => observer?.disconnect()) -watch(field, measure) -/** Centre from the URL (?x=&y=), or null to open on the player's village. */ -const center = computed(() => { - const x = Number(route.query.x) - const y = Number(route.query.y) - return Number.isFinite(x) && Number.isFinite(y) && route.query.x !== undefined ? { x, y } : null +/** + * The field in the middle of the map, fractional while dragging. Null until known: + * without ?x=&y= the server opens on the player's village. + */ +const center = ref<{ x: number; y: number } | null>(null) +const mapSize = computed(() => view.value?.size ?? 1000) +const rounded = computed(() => (center.value ? { x: Math.round(center.value.x), y: Math.round(center.value.y) } : null)) + +/** The visible area in fields. */ +const visible = computed(() => { + if (!center.value) return null + const cols = (area.value.w - RULER) / field.value + const rows = (area.value.h - RULER) / field.value + return { left: center.value.x + 0.5 - cols / 2, top: center.value.y + 0.5 - rows / 2, cols, rows } }) -watch([center, size], () => load(center.value, size.value.w, size.value.h), { immediate: true, deep: true }) +// Load a window with some margin around what is visible, whenever the visible fields +// leave what is loaded (or already on its way). +const MARGIN = 6 +let requested: { x0: number; y0: number; x1: number; y1: number } | null = null +function ensureLoaded() { + const v = visible.value + if (!v || !rounded.value) return + const need = { x0: Math.floor(v.left) - 1, y0: Math.floor(v.top) - 1, x1: Math.ceil(v.left + v.cols) + 1, y1: Math.ceil(v.top + v.rows) + 1 } + const covers = (r: typeof requested) => !!r && need.x0 >= r.x0 && need.y0 >= r.y0 && need.x1 <= r.x1 && need.y1 <= r.y1 + const have = view.value + if (have && covers({ x0: have.x, y0: have.y, x1: have.x + have.w, y1: have.y + have.h })) return + if (covers(requested)) return + const w = Math.min(61, need.x1 - need.x0 + MARGIN * 2) + const h = Math.min(61, need.y1 - need.y0 + MARGIN * 2) + const { x, y } = rounded.value + requested = { x0: x - Math.floor(w / 2), y0: y - Math.floor(h / 2), x1: x - Math.floor(w / 2) + w, y1: y - Math.floor(h / 2) + h } + load({ x, y }, w, h) +} +watch(visible, ensureLoaded) + +// ?x=&y= is where the map is; it changes on jumps, the minimap and after a drag. +const queryCenter = computed(() => { + const x = Number(route.query.x) + const y = Number(route.query.y) + return route.query.x !== undefined && Number.isFinite(x) && Number.isFinite(y) ? { x, y } : null +}) +watch( + queryCenter, + (q) => { + if (!q) { + // Home: let the server centre on the player's village. + center.value = null + requested = null + load(null, 41, 31) + return + } + if (!rounded.value || rounded.value.x !== q.x || rounded.value.y !== q.y) center.value = { ...q } + }, + { immediate: true }, +) +watch(view, (v) => { + if (v && !center.value) center.value = { ...v.center } +}) function goTo(at: { x: number; y: number }) { - const max = (view.value?.size ?? 1000) - 1 + const max = mapSize.value - 1 router.replace({ query: { x: clamp(Math.round(at.x), 0, max), y: clamp(Math.round(at.y), 0, max) } }) } -function pan(dx: number, dy: number) { - const from = view.value?.center - if (from) goTo({ x: from.x + dx, y: from.y + dy }) +function onDrag(dx: number, dy: number) { + if (!center.value) return + const max = mapSize.value - 1 + center.value = { + x: clamp(center.value.x - dx / field.value, 0, max), + y: clamp(center.value.y - dy / field.value, 0, max), + } } +function pan(dx: number, dy: number) { + if (rounded.value) goTo({ x: rounded.value.x + dx, y: rounded.value.y + dy }) +} + +// The minimap: a 61 × 61 window, refetched once the map has moved a few fields from it. +const overview = useMap(worldId) +watch(rounded, (c) => { + const o = overview.view.value?.center + if (c && (!o || Math.abs(o.x - c.x) > 8 || Math.abs(o.y - c.y) > 8)) overview.load(c, 61, 61) +}) + // Jump-to form, prefilled with the centre. const jumpX = ref(null) const jumpY = ref(null) -watch( - () => view.value?.center, - (c) => { - if (c) { - jumpX.value = c.x - jumpY.value = c.y - } - }, -) +watch(rounded, (c) => { + if (c) { + jumpX.value = c.x + jumpY.value = c.y + } +}) function jump() { if (jumpX.value !== null && jumpY.value !== null) goTo({ x: jumpX.value, y: jumpY.value }) } @@ -86,8 +134,12 @@ const selectedVillage = computed(() => ) const shown = computed(() => hovered.value ?? selectedVillage.value) +/** Villages in the visible area, for the legend. */ const counts = computed(() => { - const villages = view.value?.villages ?? [] + const v = visible.value + const villages = (view.value?.villages ?? []).filter( + (village) => v && village.x >= v.left - 1 && village.x < v.left + v.cols && village.y >= v.top - 1 && village.y < v.top + v.rows, + ) return { own: villages.filter((v) => v.own).length, players: villages.filter((v) => v.player && !v.own).length, @@ -95,7 +147,7 @@ const counts = computed(() => { } }) -const continent = computed(() => (view.value ? `K${Math.floor(view.value.center.y / 100)}${Math.floor(view.value.center.x / 100)}` : '')) +const continent = computed(() => (rounded.value ? `K${Math.floor(rounded.value.y / 100)}${Math.floor(rounded.value.x / 100)}` : ''))