BlueGit

timfa questward

Files

questward / server / src / statusEffects / service.js

service.js

service.js 3.8 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 before = health.current | 0;
46 health.current = Math.min(health.max | 0, (health.current | 0) + rate * dt);
47 if ((health.current | 0) !== before)
48 { healthChanged = true; }
49 }
50 if (entry.type === StatusEffectType.staminaRegen && stamina)
51 {
52 const before = Number(stamina.current) || 0;
53 stamina.current = Math.min(stamina.max, before + rate * dt);
54 if (Math.ceil(before) !== Math.ceil(stamina.current))
55 { staminaChanged = true; }
56 }
57 if (entry.type === StatusEffectType.staminaDrain && stamina)
58 {
59 const before = Number(stamina.current) || 0;
60 stamina.current = Math.max(0, before - rate * dt);
61 if (Math.ceil(before) !== Math.ceil(stamina.current))
62 { staminaChanged = true; }
63 }
64 }
65
66 if (healthChanged)
67 {
68 syncPlayerHealth(world.ecs, handle);
69 const view = publicEntityView(world.ecs, entityId);
70 if (view)
71 {
72 events.push({
73 type: "entity-health",
74 entityId,
75 characterId: character.characterId,
76 health: view.health,
77 maxHealth: view.maxHealth,
78 tileX: view.tileX,
79 tileY: view.tileY
80 });
81 }
82 }
83
84 if (staminaChanged)
85 {
86 events.push({
87 type: "stamina",
88 characterId: character.characterId,
89 current: Number(stamina.current) || 0,
90 max: Math.max(1, stamina.max | 0)
91 });
92 }
93
94 const appearanceChanged = beforeScale !== afterScale || beforeGlow !== afterGlow;
95 const statusChanged = appearanceChanged || beforeBlind !== afterBlind;
96 if (appearanceChanged)
97 {
98 const view = publicEntityView(world.ecs, entityId);
99 if (view)
100 {
101 events.push({
102 type: "entity-appearance",
103 entityId: view.entityId,
104 tileX: view.tileX,
105 tileY: view.tileY,
106 sprite: view.sprite ?? null,
107 layers: view.layers ?? [],
108 lightRadiusTiles: view.lightRadiusTiles ?? 0,
109 visualScale: view.visualScale ?? 1
110 });
111 }
112 }
113 if (statusChanged || healthChanged)
114 {
115 events.push({
116 type: "status-effects",
117 characterId: character.characterId,
118 effects: publicStatusEffectsView(statusEffects, now),
119 blind: hasStatusEffect(statusEffects, StatusEffectType.blind, now)
120 });
121 }
122 }
123 return events;
124}