Pathing enemies is now working, next is combat.

master
Zed A. Shaw 5 days ago
parent dc2569f8e2
commit b4a3ba1165
  1. 51
      main.go

@ -20,7 +20,7 @@ const (
PATH_LIMIT = 1000 PATH_LIMIT = 1000
RENDER = true RENDER = true
SHOW_RENDER = false SHOW_RENDER = false
SHOW_PATHS = true SHOW_PATHS = false
HEARING_DISTANCE = 6 HEARING_DISTANCE = 6
) )
@ -63,20 +63,32 @@ func (game *Game) DrawEntity(symbol rune, pos Position, color tcell.Color) {
} }
func (game *Game) DrawMap() { func (game *Game) DrawMap() {
gray := tcell.StyleDefault.Foreground(tcell.ColorGray)
for y, line := range game.level { for y, line := range game.level {
for x, cell := range line { for x, cell := range line {
if cell == SPACE {
game.screen.SetContent(x, y, cell, nil, gray)
} else {
game.screen.SetContent(x, y, cell, nil, tcell.StyleDefault) game.screen.SetContent(x, y, cell, nil, tcell.StyleDefault)
} }
} }
}
} }
func (game *Game) DrawPaths() { func (game *Game) DrawPaths() {
for y, row := range game.paths { for y, row := range game.paths {
for x, path_num := range row { for x, path_num := range row {
if path_num <= 16 { if path_num == PATH_LIMIT { continue }
as_str := fmt.Sprintf("%x", path_num)
game.screen.SetContent(x, y, rune(as_str[0]), nil, tcell.StyleDefault) as_str := fmt.Sprintf("%x", path_num % 16)
style := tcell.StyleDefault.Foreground(tcell.ColorGray)
if path_num >= 0 && path_num <= 16 {
style = style.Reverse(true)
} }
game.screen.SetContent(x, y, rune(as_str[0]), nil, style)
} }
} }
} }
@ -344,7 +356,8 @@ func (game *Game) PathAddNeighbors(neighbors []Position, closed Map, near Positi
points := compass(near.x, near.y, 1) points := compass(near.x, near.y, 1)
for _, pos := range points { for _, pos := range points {
if !game.Occupied(pos.x, pos.y) && closed[pos.y][pos.x] == SPACE { // NOTE: if you also add !game.Occupied(pos.x, pos.y) it ????
if closed[pos.y][pos.x] == SPACE {
closed[pos.y][pos.x] = WALL closed[pos.y][pos.x] = WALL
neighbors = append(neighbors, pos) neighbors = append(neighbors, pos)
} }
@ -396,26 +409,28 @@ func (game *Game) PathEnemies() {
for _, pos := range open_pixels { for _, pos := range open_pixels {
game.paths[pos.y][pos.x] = counter game.paths[pos.y][pos.x] = counter
} }
dbg.Println("pathing is", game.paths)
} }
func (game *Game) EnemyMovement() { func (game *Game) EnemyMovement() {
for enemy_at, _ := range game.enemies {
// get the four directions
dirs := compass(enemy_at.x, enemy_at.y, 1)
for pos, _ := range game.enemies { // sort by closest path number
p_enemy := game.paths[pos.y][pos.x] slices.SortFunc(dirs, func(a Position, b Position) int {
return game.paths[a.y][a.x] - game.paths[b.y][b.x]
})
for _, path_at := range compass(pos.x, pos.y, 1) { // 0 dir is now the best direction
p_num := game.paths[path_at.y][path_at.x] move_to := dirs[0]
can_hear := p_num < HEARING_DISTANCE // can we hear the player? occupied?
is_open := !game.Occupied(path_at.x, path_at.y) can_hear := game.paths[move_to.y][move_to.x] < HEARING_DISTANCE
lower := p_num < p_enemy occupied := game.Occupied(move_to.x, move_to.y)
if can_hear && is_open && lower { if can_hear && !occupied {
game.MoveEnemy(pos, path_at) // move the enemy in the best direction
break game.MoveEnemy(enemy_at, move_to)
}
} }
} }
} }

Loading…
Cancel
Save