Now have rooms working and cleaned up the code more.

master
Zed A. Shaw 6 days ago
parent 65d390af57
commit 173e136a2d
  1. 2
      Makefile
  2. 95
      main.go

@ -2,5 +2,5 @@
build:
go build .
run: build
run:
./gorogue

@ -5,6 +5,7 @@ import (
"os"
"log"
"math/rand"
"time"
"strings"
"github.com/gdamore/tcell/v2"
"github.com/gdamore/tcell/v2/encoding"
@ -39,6 +40,10 @@ func (game *Game) Text(msg string) {
}
}
func (game *Game) Status(msg string) {
game.status = msg
}
func (game *Game) Render() {
var comb []rune
game.screen.Clear()
@ -60,7 +65,7 @@ func (game *Game) Exit() {
}
func (game *Game) Occupied(x int, y int) bool {
return game.level[y][x] != '.'
return game.level[y][x] != '.' || !game.Inbounds(Position{x, y}, 1)
}
func (game *Game) MovePlayer(x_delta int, y_delta int) {
@ -99,17 +104,23 @@ func (game *Game) HandleEvents() {
}
}
func MakeGame() (*Game) {
func MakeGame(width int, height int) (*Game) {
var game Game
var err error
encoding.Register()
game.width = width
game.height = height
game.level = make([][]rune, height, height)
game.screen, err = tcell.NewScreen()
if err != nil { log.Fatal(err) }
err = game.screen.Init()
if err != nil { log.Fatal(err) }
game.player = Position{1,1}
return &game
@ -124,8 +135,11 @@ func compass(x int, y int, offset int) []Position {
}
}
func (game *Game) Inbounds(pos Position) bool {
return pos.x >= 0 && pos.x < game.width && pos.y >= 0 && pos.y < game.height
func (game *Game) Inbounds(pos Position, offset int) bool {
return pos.x >= offset &&
pos.x < game.width - offset &&
pos.y >= offset &&
pos.y < game.height - offset
}
func (game *Game) Neighbors(near Position) []Position {
@ -133,7 +147,7 @@ func (game *Game) Neighbors(near Position) []Position {
points := compass(near.x, near.y, 2)
for _, pos := range points {
if game.Inbounds(pos) {
if game.Inbounds(pos, 0) {
result = append(result, pos)
}
}
@ -178,6 +192,13 @@ func (game *Game) FindCoord(on *Position, found *Position) bool {
return false
}
func (game *Game) HAKStep(from Position, to Position) {
game.level[from.y][from.x] = SPACE
row := (from.y + to.y) / 2
col := (from.x + to.x) / 2
game.level[row][col] = SPACE
}
func (game *Game) HuntAndKill() []Position {
on := Position{1, 1}
found := Position{1,1}
@ -194,43 +215,59 @@ func (game *Game) HuntAndKill() []Position {
break
}
game.level[on.y][on.x] = SPACE
row := (on.y + found.y) / 2
col := (on.x + found.x) / 2
game.level[row][col] = SPACE
game.HAKStep(on, found)
} else {
rand_neighbor := rand.Int() % len(neighbors)
nb := neighbors[rand_neighbor]
game.level[nb.y][nb.x] = SPACE
row := (nb.y + on.y) / 2
col := (nb.x + on.x) / 2
game.level[row][col] = SPACE
game.HAKStep(nb, on)
on = nb
}
}
game.status = "FINISHED"
game.Render()
time.Sleep(50 * time.Millisecond)
}
return dead_ends
}
func (game *Game) MakeMap(width int, height int) {
game.width = width
game.height = height
row := strings.Repeat("#", width)
func (game *Game) ClearMap() {
row := strings.Repeat("#", game.width)
for i := 0 ; i < height; i++ {
for y := 0 ; y < game.height; y++ {
as_runes := []rune(strings.Clone(row))
game.level = append(game.level, as_runes)
game.level[y] = as_runes
}
}
func (game *Game) CarveRoom(pos Position, size int) {
// only use ones far enough inside
for y := pos.y - size; y < pos.y + size; y++ {
for x := pos.x - size; x < pos.x + size; x++ {
if game.Inbounds(Position{x, y}, 1) {
game.level[y][x] = SPACE
}
}
}
}
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]
})
for _, pos := range dead_ends[0:4] {
rs := rand.Int() % size + 1
game.CarveRoom(pos, rs)
}
}
// this returns dead_ends
func (game *Game) MakeMap() {
game.ClearMap()
dead_ends := game.HuntAndKill()
game.ClearMap()
game.AddRooms(dead_ends, 3)
game.HuntAndKill()
game.Status("FINISHED")
}
// This program just prints "Hello, World!". Press ESC to exit.
@ -239,8 +276,8 @@ func main() {
if err != nil { log.Fatal(err) }
dbg = log.New(out, "", log.LstdFlags)
game := MakeGame()
game.MakeMap(27,17)
game := MakeGame(27, 17)
game.MakeMap()
game.Render()
for {

Loading…
Cancel
Save