More cleanup and now placing enemies at dead ends.

master
Zed A. Shaw 6 days ago
parent 173e136a2d
commit 0a471b37db
  1. 39
      main.go

@ -31,6 +31,7 @@ type Game struct {
status string status string
width int width int
height int height int
enemies []Position
} }
func (game *Game) Text(msg string) { func (game *Game) Text(msg string) {
@ -44,18 +45,32 @@ func (game *Game) Status(msg string) {
game.status = msg game.status = msg
} }
func (game *Game) Render() { func (game *Game) DrawEntity(symbol rune, pos Position) {
var comb []rune var comb []rune
game.screen.Clear() game.screen.SetContent(pos.x, pos.y, symbol, comb, tcell.StyleDefault)
}
func (game *Game) DrawMap() {
var comb []rune
for y, line := range game.level { for y, line := range game.level {
for x, cell := range line { for x, cell := range line {
game.screen.SetContent(x, y, cell, comb, tcell.StyleDefault) game.screen.SetContent(x, y, cell, comb, tcell.StyleDefault)
} }
} }
}
func (game *Game) Render() {
game.screen.Clear()
game.DrawMap()
game.DrawEntity('@', game.player)
for _, pos := range game.enemies {
game.DrawEntity('G', pos)
}
game.Text(game.status) game.Text(game.status)
game.screen.SetContent(game.player.x, game.player.y, '@', comb, tcell.StyleDefault)
game.screen.Show() game.screen.Show()
} }
@ -261,13 +276,24 @@ func (game *Game) AddRooms(dead_ends []Position, size int) {
} }
} }
func (game *Game) MakeMap() { func (game *Game) MakeMap() []Position {
game.ClearMap() game.ClearMap()
dead_ends := game.HuntAndKill() dead_ends := game.HuntAndKill()
game.ClearMap() game.ClearMap()
game.AddRooms(dead_ends, 3) game.AddRooms(dead_ends, 3)
game.HuntAndKill() dead_ends = game.HuntAndKill()
game.Status("FINISHED") game.Status("FINISHED")
return dead_ends
}
func (game *Game) PlaceEnemies(places []Position) {
for _, pos := range places {
if rand.Int() % 2 == 0 {
game.enemies = append(game.enemies, pos)
}
}
} }
// This program just prints "Hello, World!". Press ESC to exit. // This program just prints "Hello, World!". Press ESC to exit.
@ -277,7 +303,8 @@ func main() {
dbg = log.New(out, "", log.LstdFlags) dbg = log.New(out, "", log.LstdFlags)
game := MakeGame(27, 17) game := MakeGame(27, 17)
game.MakeMap() dead_ends := game.MakeMap()
game.PlaceEnemies(dead_ends)
game.Render() game.Render()
for { for {

Loading…
Cancel
Save