| 1 | import { Component } from "../ecs/components.js"; |
| 2 | import { |
| 3 | activeStatusEffects, |
| 4 | pruneStatusEffects, |
| 5 | StatusEffectType, |
| 6 | publicStatusEffectsView, |
| 7 | hasStatusEffect |
| 8 | } from "../../../shared/statusEffects/rules.js"; |
| 9 | import { markHealthDamaged } from "../../../shared/combat/passiveRegen.js"; |
| 10 | import { publicEntityView } from "../ecs/serialize.js"; |
| 11 | import { 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 | */ |
| 17 | export 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.staminaRegen && stamina) |
| 65 | { |
| 66 | const before = Number(stamina.current) || 0; |
| 67 | stamina.current = Math.min(stamina.max, before + rate * dt); |
| 68 | if (Math.ceil(before) !== Math.ceil(stamina.current)) |
| 69 | { staminaChanged = true; } |
| 70 | } |
| 71 | if (entry.type === StatusEffectType.staminaDrain && stamina) |
| 72 | { |
| 73 | const before = Number(stamina.current) || 0; |
| 74 | stamina.current = Math.max(0, before - rate * dt); |
| 75 | if (Math.ceil(before) !== Math.ceil(stamina.current)) |
| 76 | { staminaChanged = true; } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | if (healthChanged && character) |
| 81 | { |
| 82 | syncPlayerHealth(world.ecs, handle); |
| 83 | const view = publicEntityView(world.ecs, entityId); |
| 84 | if (view) |
| 85 | { |
| 86 | events.push({ |
| 87 | type: "entity-health", |
| 88 | entityId, |
| 89 | characterId: character.characterId, |
| 90 | health: view.health, |
| 91 | maxHealth: view.maxHealth, |
| 92 | tileX: view.tileX, |
| 93 | tileY: view.tileY |
| 94 | }); |
| 95 | } |
| 96 | } |
| 97 | else if (healthChanged) |
| 98 | { |
| 99 | const view = publicEntityView(world.ecs, entityId); |
| 100 | if (view) |
| 101 | { |
| 102 | events.push({ |
| 103 | type: "entity-health", |
| 104 | entityId, |
| 105 | health: view.health, |
| 106 | maxHealth: view.maxHealth, |
| 107 | tileX: view.tileX, |
| 108 | tileY: view.tileY |
| 109 | }); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if (staminaChanged && character && stamina) |
| 114 | { |
| 115 | events.push({ |
| 116 | type: "stamina", |
| 117 | characterId: character.characterId, |
| 118 | current: Number(stamina.current) || 0, |
| 119 | max: Math.max(1, stamina.max | 0) |
| 120 | }); |
| 121 | } |
| 122 | |
| 123 | const appearanceChanged = beforeScale !== afterScale || beforeGlow !== afterGlow; |
| 124 | const statusChanged = appearanceChanged || beforeBlind !== afterBlind; |
| 125 | if (appearanceChanged) |
| 126 | { |
| 127 | const view = publicEntityView(world.ecs, entityId); |
| 128 | if (view) |
| 129 | { |
| 130 | events.push({ |
| 131 | type: "entity-appearance", |
| 132 | entityId: view.entityId, |
| 133 | tileX: view.tileX, |
| 134 | tileY: view.tileY, |
| 135 | sprite: view.sprite ?? null, |
| 136 | layers: view.layers ?? [], |
| 137 | lightRadiusTiles: view.lightRadiusTiles ?? 0, |
| 138 | visualScale: view.visualScale ?? 1 |
| 139 | }); |
| 140 | } |
| 141 | } |
| 142 | if (character && (statusChanged || healthChanged)) |
| 143 | { |
| 144 | events.push({ |
| 145 | type: "status-effects", |
| 146 | characterId: character.characterId, |
| 147 | effects: publicStatusEffectsView(statusEffects, now), |
| 148 | blind: hasStatusEffect(statusEffects, StatusEffectType.blind, now) |
| 149 | }); |
| 150 | } |
| 151 | } |
| 152 | return events; |
| 153 | } |