| 1 | import { Component } from "../ecs/components.js"; |
| 2 | import { tileToChunk } from "../world/coordinates.js"; |
| 3 | import { OVERWORLD_ID } from "../../../shared/world/dimensions.js"; |
| 4 | import { updateChunkPresence } from "../../../shared/navigation/chunkPresence.js"; |
| 5 | |
| 6 | /** Track which chunk a player has occupied since `sinceMs` (for aggro peace). */ |
| 7 | export function syncCharacterChunkPresence (ecs, handle, now = Date.now()) |
| 8 | { |
| 9 | if (!ecs.get(handle, Component.character)) |
| 10 | { return; } |
| 11 | const position = ecs.get(handle, Component.position); |
| 12 | if (!position) |
| 13 | { return; } |
| 14 | const dimensionId = position.dimensionId ?? OVERWORLD_ID; |
| 15 | let presence = ecs.get(handle, Component.chunkPresence); |
| 16 | if (!presence) |
| 17 | { |
| 18 | presence = updateChunkPresence( |
| 19 | null, |
| 20 | position.tileX, |
| 21 | position.tileY, |
| 22 | dimensionId, |
| 23 | now, |
| 24 | tileToChunk |
| 25 | ); |
| 26 | ecs.add(handle, Component.chunkPresence, presence); |
| 27 | return; |
| 28 | } |
| 29 | updateChunkPresence( |
| 30 | presence, |
| 31 | position.tileX, |
| 32 | position.tileY, |
| 33 | dimensionId, |
| 34 | now, |
| 35 | tileToChunk |
| 36 | ); |
| 37 | } |