BlueGit

timfa questward

Files

questward / server / src / statusEffects / service.js

service.js

service.js 3.9 KiB Download Raw
1import { Component } from "../ecs/components.js";
2import {
3 activeStatusEffects,
4 pruneStatusEffects,
5 StatusEffectType,
6 publicStatusEffectsView,
7 hasStatusEffect
8} from "../../../shared/statusEffects/rules.js";
9import { publicEntityView } from "../ecs/serialize.js";
10import { syncPlayerHealth } from "../skills/service.js";
11
12/**
13 * Tick timed potion effects: health/stamina regen & drain.
14 * Returns replication events (health/stamina/appearance/status-effects).
15 */
16export function stepStatusEffects (world, now = Date.now(), dt = 1 / 20)
17{
18 const events = [];
19 for (const entity of world.ecs.query(Component.character, Component.statusEffects))
20 {
21 const { handle, entityId, character, statusEffects } = entity;
22 if (world.ecs.has(handle, Component.dead))
23 { continue; }
24
25 const beforeScale = statusEffects.effects?.some((e) => e.type === StatusEffectType.shrink);
26 const beforeGlow = statusEffects.effects?.some((e) => e.type === StatusEffectType.glow);
27 const beforeBlind = statusEffects.effects?.some((e) => e.type === StatusEffectType.blind);
28 pruneStatusEffects(statusEffects, now);
29 const afterScale = statusEffects.effects?.some((e) => e.type === StatusEffectType.shrink);
30 const afterGlow = statusEffects.effects?.some((e) => e.type === StatusEffectType.glow);
31 const afterBlind = statusEffects.effects?.some((e) => e.type === StatusEffectType.blind);
32
33 let healthChanged = false;
34 let staminaChanged = false;
35 const health = world.ecs.get(handle, Component.health);
36 const stamina = world.ecs.get(handle, Component.stamina);
37
38 for (const entry of activeStatusEffects(statusEffects, now))
39 {
40 const rate = Number(entry.amountPerSecond) || 0;
41 if (!rate)
42 { continue; }
43 if (entry.type === StatusEffectType.healthRegen && health)
44 {
45 const beforeInt = health.current | 0;
46 const next = Math.min(health.max | 0, (Number(health.current) || 0) + rate * dt);
47 health.current = next;
48 if ((next | 0) !== beforeInt)
49 { healthChanged = true; }
50 }
51 if (entry.type === StatusEffectType.staminaRegen && stamina)
52 {
53 const before = Number(stamina.current) || 0;
54 stamina.current = Math.min(stamina.max, before + rate * dt);
55 if (Math.ceil(before) !== Math.ceil(stamina.current))
56 { staminaChanged = true; }
57 }
58 if (entry.type === StatusEffectType.staminaDrain && stamina)
59 {
60 const before = Number(stamina.current) || 0;
61 stamina.current = Math.max(0, before - rate * dt);
62 if (Math.ceil(before) !== Math.ceil(stamina.current))
63 { staminaChanged = true; }
64 }
65 }
66
67 if (healthChanged)
68 {
69 syncPlayerHealth(world.ecs, handle);
70 const view = publicEntityView(world.ecs, entityId);
71 if (view)
72 {
73 events.push({
74 type: "entity-health",
75 entityId,
76 characterId: character.characterId,
77 health: view.health,
78 maxHealth: view.maxHealth,
79 tileX: view.tileX,
80 tileY: view.tileY
81 });
82 }
83 }
84
85 if (staminaChanged)
86 {
87 events.push({
88 type: "stamina",
89 characterId: character.characterId,
90 current: Number(stamina.current) || 0,
91 max: Math.max(1, stamina.max | 0)
92 });
93 }
94
95 const appearanceChanged = beforeScale !== afterScale || beforeGlow !== afterGlow;
96 const statusChanged = appearanceChanged || beforeBlind !== afterBlind;
97 if (appearanceChanged)
98 {
99 const view = publicEntityView(world.ecs, entityId);
100 if (view)
101 {
102 events.push({
103 type: "entity-appearance",
104 entityId: view.entityId,
105 tileX: view.tileX,
106 tileY: view.tileY,
107 sprite: view.sprite ?? null,
108 layers: view.layers ?? [],
109 lightRadiusTiles: view.lightRadiusTiles ?? 0,
110 visualScale: view.visualScale ?? 1
111 });
112 }
113 }
114 if (statusChanged || healthChanged)
115 {
116 events.push({
117 type: "status-effects",
118 characterId: character.characterId,
119 effects: publicStatusEffectsView(statusEffects, now),
120 blind: hasStatusEffect(statusEffects, StatusEffectType.blind, now)
121 });
122 }
123 }
124 return events;
125}