From feffe21c9e090b436730441476b64e5a103fb778 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Fri, 3 Oct 2025 01:43:36 -0400 Subject: [PATCH] A quick little random movement for enemies to work out collision. --- main.go | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index a13adc2..bba2f86 100644 --- a/main.go +++ b/main.go @@ -18,7 +18,7 @@ const ( SPACE = '.' PATH_LIMIT = 1000 RENDER = true - SHOW_RENDER = true + SHOW_RENDER = false ) type Position struct { @@ -91,11 +91,13 @@ func (game *Game) Exit() { func (game *Game) Occupied(x int, y int) bool { pos := Position{x, y} _, is_enemy := game.enemies[pos] + is_player := pos == game.player // Inbounds comes first to prevent accessing level with bad x,y return !game.Inbounds(pos, 1) || game.level[y][x] == WALL || - is_enemy + is_enemy || + is_player } func (game *Game) MovePlayer(x_delta int, y_delta int) { @@ -291,6 +293,24 @@ func (game *Game) CarveRoom(pos Position, size int) { } } +func (game *Game) MoveEnemy(from Position, to Position) { + enemy, ok := game.enemies[from] + if !ok { log.Fatal("no enemy at", from, "wtf") } + + delete(game.enemies, from) + game.enemies[to] = enemy +} + +func (game *Game) PathEnemies() { + for pos, _ := range game.enemies { + possible := compass(pos.x, pos.y, 1) + move_to := possible[rand.Int() % len(possible)] + if !game.Occupied(move_to.x, move_to.y) { + game.MoveEnemy(pos, move_to) + } + } +} + func (game *Game) AddRooms(dead_ends []Position, size int) { rand.Shuffle(len(dead_ends), func(i, j int) { dead_ends[i], dead_ends[j] = dead_ends[j], dead_ends[i] @@ -304,16 +324,16 @@ func (game *Game) AddRooms(dead_ends []Position, size int) { func (game *Game) MakeMap() []Position { game.ClearMap() - dead_ends := game.HuntAndKill() game.Status("FIRST MAZE") + dead_ends := game.HuntAndKill() game.ClearMap() game.AddRooms(dead_ends, game.height / 8) - game.Status("SECOND MAZE") + game.Status("SECOND MAZE") dead_ends = game.HuntAndKill() - game.Status("FINISHED") + game.Status("FINISHED") return dead_ends } @@ -336,6 +356,7 @@ func main() { game.Render() for game.HandleEvents() { + game.PathEnemies() game.Render() }