From cac20e21adef6d6cefb09b580428ee9a34753258 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Fri, 16 Feb 2024 01:03:12 +0800 Subject: [PATCH] :sparkles: Attackers cause damage --- src/stage/objects/crystal.tsx | 50 ++++++++++++++++++++++++-- src/stage/objects/enemies/attacker.tsx | 6 +++- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/stage/objects/crystal.tsx b/src/stage/objects/crystal.tsx index 3810ff0..586fa2e 100644 --- a/src/stage/objects/crystal.tsx +++ b/src/stage/objects/crystal.tsx @@ -18,6 +18,45 @@ export default function Crystal(props: { size?: number; position?: vector2d }) { props.position?.y ? props.position?.y - size / 2 : 0 ); + // Health + + const [health, setHealth] = createSignal(100); + const [maxHealth, setMaxHealth] = createSignal(100); + + const regenerationProgress = createMemo(() => (health() / maxHealth()) * 100); + + const isAlive = createMemo(() => health() > 0); + + function calcHostileCauseDamage() { + const units = document.querySelectorAll(".hostile"); + const crystal = document.querySelector(".crystal"); + + let damage = 0; + for (const unit of units) { + if (crystal && checkElementOverlap(crystal, unit)) { + damage += parseInt(unit.getAttribute("data-damage") ?? "0"); + unit.setAttribute("data-need-dispose", "yes"); + } + } + return damage; + } + + function dealHostileUnderCrystal() { + const units = document.querySelectorAll( + ".attackers[data-need-dispose=yes]" + ); + units.forEach((unit) => unit.remove()); + } + + function updateHealth() { + const damage = calcHostileCauseDamage(); + if (damage && isAlive()) { + setHealth(health() - damage); + console.log("health: ", health()); + } + dealHostileUnderCrystal(); + } + // Energy const [energy, setEnergy] = createSignal(100); @@ -25,7 +64,7 @@ export default function Crystal(props: { size?: number; position?: vector2d }) { const rechargeProgress = createMemo(() => (energy() / maxEnergy()) * 100); - const isAlive = createMemo(() => energy() > 0); + const isWorking = createMemo(() => energy() > 0); function checkAnyPlayerUnderCrystal() { const players = document.querySelectorAll(".player"); @@ -53,7 +92,10 @@ export default function Crystal(props: { size?: number; position?: vector2d }) { } const [{ subscribe }] = useTimer(); - subscribe(() => updateEnergy()); + subscribe(() => { + updateHealth(); + updateEnergy(); + }); // Renderer @@ -104,7 +146,9 @@ export default function Crystal(props: { size?: number; position?: vector2d }) { return (