BlueGit

timfa questward

Files

questward / server / src / statusEffects / service.js

service.js

magics
timfa Tim Falken · pushed by timfa · 2 months ago
8b1a158b History
service.js 4.5 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 / spell 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.statusEffects, Component.health))
20 {
21 const { handle, entityId, statusEffects } = entity;
22 if (world.ecs.has(handle, Component.dead))
23 { continue; }
24 const character = world.ecs.get(handle, Component.character);
25
26 const beforeScale = statusEffects.effects?.some((e) => e.type === StatusEffectType.shrink);
27 const beforeGlow = statusEffects.effects?.some((e) => e.type === StatusEffectType.glow);
28 const beforeBlind = statusEffects.effects?.some((e) => e.type === StatusEffectType.blind);
29 pruneStatusEffects(statusEffects, now);
30 const afterScale = statusEffects.effects?.some((e) => e.type === StatusEffectType.shrink);
31 const afterGlow = statusEffects.effects?.some((e) => e.type === StatusEffectType.glow);
32 const afterBlind = statusEffects.effects?.some((e) => e.type === StatusEffectType.blind);
33
34 let healthChanged = false;
35 let staminaChanged = false;
36 const health = world.ecs.get(handle, Component.health);
37 const stamina = world.ecs.get(handle, Component.stamina);
38
39 for (const entry of activeStatusEffects(statusEffects, now))
40 {
41 const rate = Number(entry.amountPerSecond) || 0;
42 if (!rate)
43 { continue; }
44 if (entry.type === StatusEffectType.healthRegen && health)
45 {
46 const beforeInt = health.current | 0;
47 const next = Math.min(health.max | 0, (Number(health.current) || 0) + rate * dt);
48 health.current = next;
49 if ((next | 0) !== beforeInt)
50 { healthChanged = true; }
51 }
52 if (entry.type === StatusEffectType.healthDrain && health)
53 {
54 const beforeInt = health.current | 0;
55 const next = Math.max(0, (Number(health.current) || 0) - rate * dt);
56 health.current = next;
57 if ((next | 0) !== beforeInt)
58 { healthChanged = true; }
59 }
60 if (entry.type === StatusEffectType.staminaRegen && stamina)
61 {
62 const before = Number(stamina.current) || 0;
63 stamina.current = Math.min(stamina.max, before + rate * dt);
64 if (Math.ceil(before) !== Math.ceil(stamina.current))
65 { staminaChanged = true; }
66 }
67 if (entry.type === StatusEffectType.staminaDrain && stamina)
68 {
69 const before = Number(stamina.current) || 0;
70 stamina.current = Math.max(0, before - rate * dt);
71 if (Math.ceil(before) !== Math.ceil(stamina.current))
72 { staminaChanged = true; }
73 }
74 }
75
76 if (healthChanged && character)
77 {
78 syncPlayerHealth(world.ecs, handle);
79 const view = publicEntityView(world.ecs, entityId);
80 if (view)
81 {
82 events.push({
83 type: "entity-health",
84 entityId,
85 characterId: character.characterId,
86 health: view.health,
87 maxHealth: view.maxHealth,
88 tileX: view.tileX,
89 tileY: view.tileY
90 });
91 }
92 }
93 else if (healthChanged)
94 {
95 const view = publicEntityView(world.ecs, entityId);
96 if (view)
97 {
98 events.push({
99 type: "entity-health",
100 entityId,
101 health: view.health,
102 maxHealth: view.maxHealth,
103 tileX: view.tileX,
104 tileY: view.tileY
105 });
106 }
107 }
108
109 if (staminaChanged && character && stamina)
110 {
111 events.push({
112 type: "stamina",
113 characterId: character.characterId,
114 current: Number(stamina.current) || 0,
115 max: Math.max(1, stamina.max | 0)
116 });
117 }
118
119 const appearanceChanged = beforeScale !== afterScale || beforeGlow !== afterGlow;
120 const statusChanged = appearanceChanged || beforeBlind !== afterBlind;
121 if (appearanceChanged)
122 {
123 const view = publicEntityView(world.ecs, entityId);
124 if (view)
125 {
126 events.push({
127 type: "entity-appearance",
128 entityId: view.entityId,
129 tileX: view.tileX,
130 tileY: view.tileY,
131 sprite: view.sprite ?? null,
132 layers: view.layers ?? [],
133 lightRadiusTiles: view.lightRadiusTiles ?? 0,
134 visualScale: view.visualScale ?? 1
135 });
136 }
137 }
138 if (character && (statusChanged || healthChanged))
139 {
140 events.push({
141 type: "status-effects",
142 characterId: character.characterId,
143 effects: publicStatusEffectsView(statusEffects, now),
144 blind: hasStatusEffect(statusEffects, StatusEffectType.blind, now)
145 });
146 }
147 }
148 return events;
149}