Skip to content

Commit

Permalink
add health bar behavior for reference
Browse files Browse the repository at this point in the history
  • Loading branch information
CodedRedGIT committed Aug 21, 2024
1 parent 498a1da commit 07bca2b
Showing 1 changed file with 88 additions and 13 deletions.
101 changes: 88 additions & 13 deletions ingredients/Interacting with Behaviors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Behavior, Entity, EntityCollision } from "@dreamlab/engine";
import HealthBar from "./health-bar.ts";
import PlayerBehavior from "./player.ts";
import { Behavior, Entity, EntityCollision } from '@dreamlab/engine'
import PlayerBehavior from './player.ts'

/*
Example: Using `getBehavior()` to Access Behaviors
Expand All @@ -19,32 +18,108 @@ import PlayerBehavior from "./player.ts";
*/

export default class AsteroidBehavior extends Behavior {

onInitialize(): void {
this.listen(this.entity, EntityCollision, e => {
if (e.started) this.onCollide(e.other);
});
if (e.started) this.onCollide(e.other)
})
}

onCollide(other: Entity) {
if (!other.name.startsWith("Bullet")) return;
other.destroy();
if (!other.name.startsWith('Bullet')) return
other.destroy()

// Retrieve the HealthBar behavior for this entity
const healthBar = this.entity.getBehavior(HealthBar);
const healthBar = this.entity.getBehavior(HealthBar)

// Reduce the asteroid's health by 1
healthBar.takeDamage(1);
healthBar.takeDamage(1)

// If health reaches zero, update the player's score and destroy the asteroid
if (healthBar.currentHealth <= 0) {
const player = this.game.world._.Player;
const player = this.game.world._.Player

// Use getBehavior to access the PlayerBehavior and update the score
player.getBehavior(PlayerBehavior).score += 50;
player.getBehavior(PlayerBehavior).score += 50

// Destroy the asteroid entity (healthBar destruction is handled by takeDamage)
this.entity.destroy();
this.entity.destroy()
}
}
}

// Health bar behavior for reference
import {
Behavior,
BehaviorContext,
Entity,
GamePostTick,
Rigidbody2D,
Sprite2D,
Vector2,
} from '@dreamlab/engine'

export default class HealthBar extends Behavior {
maxHealth: number = 100
currentHealth: number = 100
healthBarEntity!: Entity

constructor(ctx: BehaviorContext) {
super(ctx)
this.defineValues(HealthBar, 'maxHealth', 'currentHealth')
}

onInitialize(): void {
this.healthBarEntity = this.game.world.spawn({
type: Sprite2D,
name: 'HealthBar',
transform: { position: { x: 0, y: 1 }, scale: { x: 1, y: 0.1 } },
values: { texture: 'res://assets/healthbar.png' },
})

this.game.on(GamePostTick, () => {
this.healthBarEntity.pos = this.entity.pos.add(new Vector2(0, 1))
this.updateHealthBar()
})
}

updateHealthBar(): void {
const healthRatio = this.currentHealth / this.maxHealth
this.healthBarEntity.transform.scale.x = healthRatio
}

takeDamage(damage: number): void {
this.currentHealth -= damage
if (this.currentHealth <= 0) {
this.currentHealth = 0
this.entity.destroy()
this.healthBarEntity.destroy()
this.spawnExplosionPieces()
}

this.updateHealthBar()
}

spawnExplosionPieces(): void {
const pieceCount = Math.random() * 5 + 3
const pieceSize = { x: 0.15, y: 0.15 }

for (let i = 0; i < pieceCount; i++) {
this.entity.game.world.spawn({
type: Rigidbody2D,
name: 'ExplosionPiece',
transform: {
position: this.entity.transform.position.clone(),
scale: pieceSize,
},
behaviors: [],
children: [
{
type: Sprite2D,
name: 'PieceSprite',
values: { texture: 'res://assets/asteroid.png' },
},
],
})
}
}
}

0 comments on commit 07bca2b

Please sign in to comment.