Skip to content

Commit

Permalink
enemy btree interface update
Browse files Browse the repository at this point in the history
  • Loading branch information
Acepie committed May 29, 2024
1 parent 70c1c90 commit 483eece
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
16 changes: 10 additions & 6 deletions src/bullet_heck_gleam.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -333,17 +333,20 @@ fn on_tick(state: WorldState) -> WorldState {
}
})

let #(enemies, player, score) =
list.fold(enemies, #([], player, score), fn(acc, e) {
let #(enemies, player, score) = acc
let #(enemies, bullets, player, score) =
list.fold(enemies, #([], bullets, player, score), fn(acc, e) {
let #(enemies, bullets, player, score) = acc
// Apply behavior to enemy
let enemy = e.btree(e, dungeon, player, bullets)
let enemy.BehaviorResult(_, enemy, new_bullets) =
e.btree(enemy.BehaviorInput(e, enemies, dungeon, player))

let bullets = list.append(new_bullets, bullets)

// Kill enemy if they have fallen into pit
use <- bool.guard(
enemy.position.z <=. 0.0
&& dungeon.is_over_pit(dungeon, enemy.position),
#(enemies, player, score + enemy.value),
#(enemies, bullets, player, score + enemy.value),
)

// Check player collides with the enemy
Expand All @@ -367,11 +370,12 @@ fn on_tick(state: WorldState) -> WorldState {
// Kill enemy if they have died
use <- bool.guard(enemy.is_enemy_dead(enemy), #(
enemies,
bullets,
player,
score + enemy.value,
))

#([enemy, ..enemies], player, score)
#([enemy, ..enemies], bullets, player, score)
})

let bullets =
Expand Down
35 changes: 33 additions & 2 deletions src/enemy.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,37 @@ pub const value = 100

const max_enemy_health = 80

/// Represents the input data needed for an enemy to update.
pub type BehaviorInput {
/// Represents the input data needed for an enemy to update.
BehaviorInput(
/// The enemy being updated.
enemy: Enemy,
/// All enemies in the game.
enemies: List(Enemy),
/// The dungeon the enemy is in.
dungeon: Dungeon,
/// The player character.
player: Player,
)
}

/// Represents the result of an enemy update.
pub type BehaviorResult {
/// Represents the result of an enemy update.
BehaviorResult(
/// Whether the action can continue
success: Bool,
/// The updated enemy.
enemy: Enemy,
/// Any bullets that were fired by the enemy.
bullets: List(Bullet),
)
}

/// Represents a function that given world information and an enemy updates the enemy.
pub type BehaviorTree =
fn(Enemy, Dungeon, Player, List(Bullet)) -> Enemy
fn(BehaviorInput) -> BehaviorResult

/// Represents an enemy to defeat.
pub type Enemy {
Expand Down Expand Up @@ -50,7 +78,10 @@ pub fn new_enemy(initial_position: Vector) -> Enemy {
current_health: max_enemy_health,
max_health: max_enemy_health,
// TODO: actually make a behavior
btree: fn(e, _, _, _) { e },
btree: fn(i) {
let BehaviorInput(e, _, _, _) = i
BehaviorResult(True, e, [])
},
)
}

Expand Down
19 changes: 12 additions & 7 deletions test/enemy_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import startest.{describe, it}
import startest/expect
import vector.{Vector}

fn dummy_btree(i: enemy.BehaviorInput) -> enemy.BehaviorResult {
let enemy.BehaviorInput(e, _, _, _) = i
enemy.BehaviorResult(True, e, [])
}

pub fn enemy_tests() {
describe("enemy", [
it("new_enemy", fn() {
Expand All @@ -20,7 +25,7 @@ pub fn enemy_tests() {
rotation: 0.0,
current_health: 100,
max_health: 100,
btree: fn(e, _, _, _) { e },
btree: dummy_btree,
)
|> enemy.apply_gravity
expect.to_equal(enemy.velocity, vector.Vector(0.0, 0.0, 0.98))
Expand All @@ -32,7 +37,7 @@ pub fn enemy_tests() {
rotation: 0.0,
current_health: 100,
max_health: 100,
btree: fn(e, _, _, _) { e },
btree: dummy_btree,
)
|> enemy.apply_gravity
expect.to_equal(enemy.position, vector.Vector(0.0, 0.0, 0.0))
Expand All @@ -46,7 +51,7 @@ pub fn enemy_tests() {
rotation: 0.0,
current_health: 100,
max_health: 100,
btree: fn(e, _, _, _) { e },
btree: dummy_btree,
)
|> enemy.apply_damage(20)
expect.to_equal(enemy.current_health, 80)
Expand All @@ -59,7 +64,7 @@ pub fn enemy_tests() {
rotation: 0.0,
current_health: 100,
max_health: 100,
btree: fn(e, _, _, _) { e },
btree: dummy_btree,
)
|> enemy.is_enemy_dead,
)
Expand All @@ -70,7 +75,7 @@ pub fn enemy_tests() {
rotation: 0.0,
current_health: 0,
max_health: 100,
btree: fn(e, _, _, _) { e },
btree: dummy_btree,
)
|> enemy.is_enemy_dead,
)
Expand All @@ -83,7 +88,7 @@ pub fn enemy_tests() {
rotation: 0.0,
current_health: 100,
max_health: 100,
btree: fn(e, _, _, _) { e },
btree: dummy_btree,
),
Vector(10.0, 0.0, 0.0),
1.0,
Expand All @@ -95,7 +100,7 @@ pub fn enemy_tests() {
rotation: 0.0,
current_health: 100,
max_health: 100,
btree: fn(e, _, _, _) { e },
btree: dummy_btree,
),
Vector(0.0, 0.0, 0.0),
1.0,
Expand Down

0 comments on commit 483eece

Please sign in to comment.