From bee0a4264a3e0755a74d3ef1180d5a6962e82033 Mon Sep 17 00:00:00 2001 From: Renge Date: Wed, 20 Apr 2022 18:31:53 -0400 Subject: [PATCH] feat: implemented healthbar for enemies --- src/shattered_sword/AI/EnemyAI.ts | 17 +++++++++++++++++ src/shattered_sword/Scenes/GameLevel.ts | 4 ++++ 2 files changed, 21 insertions(+) diff --git a/src/shattered_sword/AI/EnemyAI.ts b/src/shattered_sword/AI/EnemyAI.ts index edef365..bfb47a8 100644 --- a/src/shattered_sword/AI/EnemyAI.ts +++ b/src/shattered_sword/AI/EnemyAI.ts @@ -25,6 +25,8 @@ import { Player_Events } from "../sword_enums"; import InputWrapper from "../Tools/InputWrapper"; import Timer from "../../Wolfie2D/Timing/Timer"; import PlayerController from "../Player/PlayerController"; +import Rect from "../../Wolfie2D/Nodes/Graphics/Rect"; +import Color from "../../Wolfie2D/Utils/Color"; export default class EnemyAI extends StateMachineGoapAI implements BattlerAI { /** The owner of this AI */ owner: AnimatedSprite; @@ -76,6 +78,8 @@ export default class EnemyAI extends StateMachineGoapAI implements BattlerAI { bleedTimer : Timer; bleedCounter :number = 0; + healthBar: Rect; + initializeAI(owner: AnimatedSprite, options: Record): void { this.owner = owner; @@ -146,6 +150,7 @@ export default class EnemyAI extends StateMachineGoapAI implements BattlerAI { this.owner.setAIActive(false, {}); this.owner.isCollidable = false; this.owner.visible = false; + this.healthBar.destroy(); this.emitter.fireEvent(Player_Events.ENEMY_KILLED, {owner: this.owner.id, ai:this}); @@ -237,6 +242,18 @@ export default class EnemyAI extends StateMachineGoapAI implements BattlerAI { this.bleedTimer.start(); this.damage(5 + (this.player._ai).extraDotDmg + (this.player._ai).CURRENT_ATK * .08); } + + this.healthBar.position = this.owner.collisionShape.center.clone().add(new Vec2(0, -((this.owner.collisionShape).hh+5))); + this.healthBar.fillWidth = this.CURRENT_HP/this.maxHealth * this.owner.collisionShape.hw * 3; + if (this.CURRENT_HP/this.maxHealth >= 2/3) { + this.healthBar.color = Color.GREEN; + } + else if (this.CURRENT_HP/this.maxHealth >= 1/3) { + this.healthBar.color = Color.YELLOW; + } + else { + this.healthBar.color = Color.RED; + } } } diff --git a/src/shattered_sword/Scenes/GameLevel.ts b/src/shattered_sword/Scenes/GameLevel.ts index 4b2c6a0..dbf15fc 100644 --- a/src/shattered_sword/Scenes/GameLevel.ts +++ b/src/shattered_sword/Scenes/GameLevel.ts @@ -739,6 +739,10 @@ export default class GameLevel extends Scene { } enemy.addAI(EnemyAI, aiOptions); //TODO - add individual enemy AI + (enemy._ai).healthBar = this.add.graphic(GraphicType.RECT, "primary", {position: enemy.collisionShape.center.clone().add(new Vec2(0, -((enemy.collisionShape).hh+5))), size: new Vec2((enemy.collisionShape).hw*3, 5)}); + (enemy._ai).healthBar.borderColor = Color.BLACK; + (enemy._ai).healthBar.borderWidth = 1; + (enemy._ai).healthBar.color = Color.GREEN; enemy.setGroup("Enemy"); enemy.setTrigger("player", Player_Events.PLAYER_COLLIDE, null);