| 1 | import { Component } from "../ecs/components.js"; |
| 2 | import { SkillId, playerMaxHealth, playerMaxStamina } from "../../../shared/definitions/skills.js"; |
| 3 | import { combatHitXpPerSkill } from "../../../shared/skills/progression.js"; |
| 4 | import { applySkillXp, publicSkillsView, storedSkillXp } from "../../../shared/skills/state.js"; |
| 5 | |
| 6 | export function syncPlayerHealth (ecs, handle) |
| 7 | { |
| 8 | const character = ecs.get(handle, Component.character); |
| 9 | if (!character) |
| 10 | { return; } |
| 11 | const skills = ecs.get(handle, Component.skills); |
| 12 | const health = ecs.get(handle, Component.health); |
| 13 | if (!skills || !health) |
| 14 | { return; } |
| 15 | const max = playerMaxHealth(skills); |
| 16 | health.max = max; |
| 17 | health.current = Math.min(health.current | 0, max); |
| 18 | } |
| 19 | |
| 20 | export function syncPlayerStamina (ecs, handle) |
| 21 | { |
| 22 | const character = ecs.get(handle, Component.character); |
| 23 | if (!character) |
| 24 | { return; } |
| 25 | const skills = ecs.get(handle, Component.skills); |
| 26 | const stamina = ecs.get(handle, Component.stamina); |
| 27 | if (!skills || !stamina) |
| 28 | { return; } |
| 29 | const max = playerMaxStamina(skills); |
| 30 | stamina.max = max; |
| 31 | stamina.current = Math.min(Number(stamina.current) || 0, max); |
| 32 | } |
| 33 | |
| 34 | export function awardCombatHitXp (ecs, attackerHandle, profile) |
| 35 | { |
| 36 | const skills = ecs.get(attackerHandle, Component.skills); |
| 37 | if (!skills?.xp) |
| 38 | { return null; } |
| 39 | const perSkill = combatHitXpPerSkill(); |
| 40 | const levelUps = []; |
| 41 | const hit = applySkillXp(skills, profile.hitSkillId, perSkill); |
| 42 | if (hit.levelUp) |
| 43 | { levelUps.push(hit.levelUp); } |
| 44 | const damage = applySkillXp(skills, profile.damageSkillId, perSkill); |
| 45 | if (damage.levelUp) |
| 46 | { levelUps.push(damage.levelUp); } |
| 47 | if (!hit.changed && !damage.changed) |
| 48 | { return null; } |
| 49 | return { |
| 50 | view: publicSkillsView(skills), |
| 51 | stored: storedSkillXp(skills), |
| 52 | levelUps |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | export function awardDamageHealthXp (ecs, targetHandle, damage) |
| 57 | { |
| 58 | const amount = Math.max(0, Math.floor(Number(damage) || 0)); |
| 59 | if (!amount) |
| 60 | { return null; } |
| 61 | const character = ecs.get(targetHandle, Component.character); |
| 62 | if (!character) |
| 63 | { return null; } |
| 64 | const skills = ecs.get(targetHandle, Component.skills); |
| 65 | if (!skills?.xp) |
| 66 | { return null; } |
| 67 | const result = applySkillXp(skills, SkillId.health, amount); |
| 68 | if (!result.changed) |
| 69 | { return null; } |
| 70 | syncPlayerHealth(ecs, targetHandle); |
| 71 | return { |
| 72 | characterId: character.characterId, |
| 73 | view: publicSkillsView(skills), |
| 74 | stored: storedSkillXp(skills), |
| 75 | levelUps: result.levelUp ? [result.levelUp] : [] |
| 76 | }; |
| 77 | } |