| 1 | import { Component } from "../ecs/components.js"; |
| 2 | import { SkillId, playerMaxHealth } from "../../../shared/definitions/skills.js"; |
| 3 | import { combatHitXpPerSkill } from "../../../shared/skills/progression.js"; |
| 4 | import { addSkillXp, 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 awardCombatHitXp (ecs, attackerHandle, profile) |
| 21 | { |
| 22 | const skills = ecs.get(attackerHandle, Component.skills); |
| 23 | if (!skills?.xp) |
| 24 | { return null; } |
| 25 | const perSkill = combatHitXpPerSkill(); |
| 26 | const hit = addSkillXp(skills, profile.hitSkillId, perSkill); |
| 27 | const damage = addSkillXp(skills, profile.damageSkillId, perSkill); |
| 28 | if (!hit && !damage) |
| 29 | { return null; } |
| 30 | return { |
| 31 | view: publicSkillsView(skills), |
| 32 | stored: storedSkillXp(skills) |
| 33 | }; |
| 34 | } |
| 35 | |
| 36 | export function awardDamageHealthXp (ecs, targetHandle, damage) |
| 37 | { |
| 38 | const amount = Math.max(0, Math.floor(Number(damage) || 0)); |
| 39 | if (!amount) |
| 40 | { return null; } |
| 41 | const character = ecs.get(targetHandle, Component.character); |
| 42 | if (!character) |
| 43 | { return null; } |
| 44 | const skills = ecs.get(targetHandle, Component.skills); |
| 45 | if (!skills?.xp) |
| 46 | { return null; } |
| 47 | if (!addSkillXp(skills, SkillId.health, amount)) |
| 48 | { return null; } |
| 49 | syncPlayerHealth(ecs, targetHandle); |
| 50 | return { |
| 51 | characterId: character.characterId, |
| 52 | view: publicSkillsView(skills), |
| 53 | stored: storedSkillXp(skills) |
| 54 | }; |
| 55 | } |