BlueGit

timfa questward

Files

questward / server / src / statusEffects / service.js

service.js

service.js 5.0 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 { markHealthDamaged } from "../../../shared/combat/passiveRegen.js";
10import { publicEntityView } from "../ecs/serialize.js";
11import { syncPlayerHealth } from "../skills/service.js";
12
13/**
14 * Tick timed potion / spell effects: health/stamina regen & drain.
15 * Returns replication events (health/stamina/appearance/status-effects).
16 */
17export function stepStatusEffects (world, now = Date.now(), dt = 1 / 20)
18{
19 const events = [];
20 for (const entity of world.ecs.query(Component.statusEffects, Component.health))
21 {
22 const { handle, entityId, statusEffects } = entity;
23 if (world.ecs.has(handle, Component.dead))
24 { continue; }
25 const character = world.ecs.get(handle, Component.character);
26
27 const beforeScale = statusEffects.effects?.some((e) => e.type === StatusEffectType.shrink);
28 const beforeGlow = statusEffects.effects?.some((e) => e.type === StatusEffectType.glow);
29 const beforeBlind = statusEffects.effects?.some((e) => e.type === StatusEffectType.blind);
30 pruneStatusEffects(statusEffects, now);
31 const afterScale = statusEffects.effects?.some((e) => e.type === StatusEffectType.shrink);
32 const afterGlow = statusEffects.effects?.some((e) => e.type === StatusEffectType.glow);
33 const afterBlind = statusEffects.effects?.some((e) => e.type === StatusEffectType.blind);
34
35 let healthChanged = false;
36 let staminaChanged = false;
37 const health = world.ecs.get(handle, Component.health);
38 const stamina = world.ecs.get(handle, Component.stamina);
39
40 for (const entry of activeStatusEffects(statusEffects, now))
41 {
42 const rate = Number(entry.amountPerSecond) || 0;
43 if (!rate)
44 { continue; }
45 if (entry.type === StatusEffectType.healthRegen && health)
46 {
47 const beforeInt = health.current | 0;
48 const next = Math.min(health.max | 0, (Number(health.current) || 0) + rate * dt);
49 health.current = next;
50 if ((next | 0) !== beforeInt)
51 { healthChanged = true; }
52 }
53 if (entry.type === StatusEffectType.healthDrain && health)
54 {
55 const beforeInt = health.current | 0;
56 const next = Math.max(0, (Number(health.current) || 0) - rate * dt);
57 health.current = next;
58 if ((next | 0) !== beforeInt)
59 {
60 markHealthDamaged(health, now);
61 healthChanged = true;
62 }
63 }
64 if (entry.type === StatusEffectType.foodPoisoning && health)
65 {
66 const beforeInt = health.current | 0;
67 const next = Math.max(0, (Number(health.current) || 0) - rate * dt);
68 health.current = next;
69 if ((next | 0) !== beforeInt)
70 {
71 markHealthDamaged(health, now);
72 healthChanged = true;
73 }
74 }
75 if (entry.type === StatusEffectType.staminaRegen && stamina)
76 {
77 const before = Number(stamina.current) || 0;
78 stamina.current = Math.min(stamina.max, before + rate * dt);
79 if (Math.ceil(before) !== Math.ceil(stamina.current))
80 { staminaChanged = true; }
81 }
82 if (entry.type === StatusEffectType.staminaDrain && stamina)
83 {
84 const before = Number(stamina.current) || 0;
85 stamina.current = Math.max(0, before - rate * dt);
86 if (Math.ceil(before) !== Math.ceil(stamina.current))
87 { staminaChanged = true; }
88 }
89 }
90
91 if (healthChanged && character)
92 {
93 syncPlayerHealth(world.ecs, handle);
94 const view = publicEntityView(world.ecs, entityId);
95 if (view)
96 {
97 events.push({
98 type: "entity-health",
99 entityId,
100 characterId: character.characterId,
101 health: view.health,
102 maxHealth: view.maxHealth,
103 tileX: view.tileX,
104 tileY: view.tileY
105 });
106 }
107 }
108 else if (healthChanged)
109 {
110 const view = publicEntityView(world.ecs, entityId);
111 if (view)
112 {
113 events.push({
114 type: "entity-health",
115 entityId,
116 health: view.health,
117 maxHealth: view.maxHealth,
118 tileX: view.tileX,
119 tileY: view.tileY
120 });
121 }
122 }
123
124 if (staminaChanged && character && stamina)
125 {
126 events.push({
127 type: "stamina",
128 characterId: character.characterId,
129 current: Number(stamina.current) || 0,
130 max: Math.max(1, stamina.max | 0)
131 });
132 }
133
134 const appearanceChanged = beforeScale !== afterScale || beforeGlow !== afterGlow;
135 const statusChanged = appearanceChanged || beforeBlind !== afterBlind;
136 if (appearanceChanged)
137 {
138 const view = publicEntityView(world.ecs, entityId);
139 if (view)
140 {
141 events.push({
142 type: "entity-appearance",
143 entityId: view.entityId,
144 tileX: view.tileX,
145 tileY: view.tileY,
146 sprite: view.sprite ?? null,
147 layers: view.layers ?? [],
148 lightRadiusTiles: view.lightRadiusTiles ?? 0,
149 visualScale: view.visualScale ?? 1
150 });
151 }
152 }
153 if (character && (statusChanged || healthChanged))
154 {
155 events.push({
156 type: "status-effects",
157 characterId: character.characterId,
158 effects: publicStatusEffectsView(statusEffects, now),
159 blind: hasStatusEffect(statusEffects, StatusEffectType.blind, now)
160 });
161 }
162 }
163 return events;
164}