BlueGit

timfa questward

Files

questward / server / src / spells / lights.js

lights.js

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