BlueGit

timfa questward

Files

questward / server / src / spells / lights.js

lights.js

lights.js 4.2 KiB Download Raw
1import { Component } from "../ecs/components.js";
2import { tileKey } from "../world/coordinates.js";
3import {
4 LIGHT_FADE_MS,
5 LIGHT_MAX_DURATION_MS,
6 LIGHT_OWNER_DISTANCE_CAP_MS,
7 SPELL_LIGHT_INTENSITY,
8 SPELL_LIGHT_RANGE_TILES,
9 COMBAT_RANGE_TILES
10} from "../../../shared/spells/constants.js";
11
12/**
13 * Authoritative temporary spell lights keyed by tile.
14 * @typedef {object} SpellLight
15 * @property {number} tileX
16 * @property {number} tileY
17 * @property {number} ownerEntityId
18 * @property {number} expiresAt
19 * @property {number} maxDurationMs
20 * @property {number} baseRange
21 * @property {number} baseIntensity
22 */
23
24export class SpellLightRegistry
25{
26 constructor ()
27 {
28 /** @type {Map<string, SpellLight>} */
29 this.byTile = new Map();
30 this.nextLightId = 1;
31 }
32
33 getAt (tileX, tileY)
34 {
35 return this.byTile.get(tileKey(tileX, tileY)) ?? null;
36 }
37
38 /** @returns {{ light: SpellLight, extended: boolean }} */
39 placeOrExtend ({
40 tileX,
41 tileY,
42 ownerEntityId,
43 durationMs,
44 now = Date.now()
45 })
46 {
47 const key = tileKey(tileX, tileY);
48 const addMs = Math.max(0, Math.min(LIGHT_MAX_DURATION_MS, durationMs | 0));
49 const existing = this.byTile.get(key);
50 if (existing)
51 {
52 const remaining = Math.max(0, existing.expiresAt - now);
53 const next = Math.min(LIGHT_MAX_DURATION_MS, remaining + addMs);
54 existing.expiresAt = now + next;
55 existing.maxDurationMs = Math.max(existing.maxDurationMs, next);
56 existing.ownerEntityId = ownerEntityId;
57 return { light: existing, extended: true };
58 }
59 const light = {
60 lightId: this.nextLightId++,
61 tileX,
62 tileY,
63 ownerEntityId,
64 expiresAt: now + addMs,
65 maxDurationMs: addMs,
66 baseRange: SPELL_LIGHT_RANGE_TILES,
67 baseIntensity: SPELL_LIGHT_INTENSITY
68 };
69 this.byTile.set(key, light);
70 return { light, extended: false };
71 }
72
73 removeAt (tileX, tileY)
74 {
75 const key = tileKey(tileX, tileY);
76 const existing = this.byTile.get(key);
77 if (!existing)
78 { return null; }
79 this.byTile.delete(key);
80 return existing;
81 }
82
83 /** Tick ownership distance penalty and expiry. Returns replication events. */
84 tick (world, now = Date.now())
85 {
86 const events = [];
87 for (const [, light] of [...this.byTile.entries()])
88 {
89 if (now >= light.expiresAt)
90 {
91 this.byTile.delete(tileKey(light.tileX, light.tileY));
92 events.push({ type: "spell-light-remove", tileX: light.tileX, tileY: light.tileY });
93 continue;
94 }
95 const ownerHandle = world.ecs.handleOf(light.ownerEntityId);
96 const ownerPos = ownerHandle != null ? world.ecs.get(ownerHandle, Component.position) : null;
97 if (ownerPos)
98 {
99 const dist = Math.max(
100 Math.abs(ownerPos.tileX - light.tileX),
101 Math.abs(ownerPos.tileY - light.tileY)
102 );
103 if (dist > COMBAT_RANGE_TILES)
104 {
105 const remaining = light.expiresAt - now;
106 const capped = Math.min( remaining, LIGHT_OWNER_DISTANCE_CAP_MS);
107 if (capped !== remaining)
108 {
109 light.expiresAt = now + capped;
110 events.push({ type: "spell-light-update", ...publicSpellLight(light, now) });
111 }
112 }
113 }
114 }
115 return events;
116 }
117
118 listInBounds (minX, minY, maxX, maxY, now = Date.now())
119 {
120 const out = [];
121 for (const light of this.byTile.values())
122 {
123 if (light.tileX < minX || light.tileX > maxX || light.tileY < minY || light.tileY > maxY)
124 { continue; }
125 if (now >= light.expiresAt)
126 { continue; }
127 out.push(publicSpellLight(light, now));
128 }
129 return out;
130 }
131}
132
133export function publicSpellLight (light, now = Date.now())
134{
135 const remainingMs = Math.max(0, light.expiresAt - now);
136 const fadeT = remainingMs <= LIGHT_FADE_MS ? remainingMs / LIGHT_FADE_MS : 1;
137 const range = light.baseRange * fadeT;
138 const intensity = light.baseIntensity * fadeT;
139 return {
140 tileX: light.tileX,
141 tileY: light.tileY,
142 ownerEntityId: light.ownerEntityId,
143 remainingMs,
144 rangeTiles: range,
145 intensity,
146 expiresAt: light.expiresAt
147 };
148}
149
150export function spellLightsForDynamicLighting (registry, now = Date.now())
151{
152 const out = [];
153 for (const light of registry.byTile.values())
154 {
155 if (now >= light.expiresAt)
156 { continue; }
157 const pub = publicSpellLight(light, now);
158 if (pub.intensity <= 0 || pub.rangeTiles <= 0)
159 { continue; }
160 out.push({
161 tileX: light.tileX,
162 tileY: light.tileY,
163 range: pub.rangeTiles,
164 intensity: pub.intensity
165 });
166 }
167 return out;
168}