~johanvandegriff/ruegolike

d88bd6e38831cd898c9ec7dc76745a56d714da93 — Johan Vandegriff 1 year, 5 months ago 44d4626
moved level gen to its own function
2 files changed, 34 insertions(+), 24 deletions(-)

A generate.go
M main.go
A generate.go => generate.go +32 -0
@@ 0,0 1,32 @@
package main

import "math/rand"

func generate() ([width][height]int32, [width][height]bool, int, int) {
	var level [width][height]int32
	var explored [width][height]bool

	//simple terrain generation
	for x := 0; x < width; x++ {
		for y := 0; y < height; y++ {
			if rand.Intn(100) < 40 {
				level[x][y] = '#' //wall, 40%
			} else {
				level[x][y] = '.' //empty, 60%
			}
		}
	}
	// level[5][4] = '£'
	// level[5][6] = '#'
	// level[5][6] = '@'

	//start the player on an empty square
	var playerX, playerY int
	//do while
	for ok := true; ok; ok = level[playerX][playerY] != '.' {
		playerX = rand.Intn(width)
		playerY = rand.Intn(height)
	}

	return level, explored, playerX, playerY
}

M main.go => main.go +2 -24
@@ 36,31 36,9 @@ func main() {

	// invert := tcell.StyleDefault.Foreground(tcell.ColorBlack).Background(tcell.ColorWhite)

	var level [width][height]int32
	var visible, explored [width][height]bool

	//simple terrain generation
	for x := 0; x < width; x++ {
		for y := 0; y < height; y++ {
			if rand.Intn(100) < 40 {
				level[x][y] = '#' //wall, 40%
			} else {
				level[x][y] = '.' //empty, 60%
			}
		}
	}
	// level[5][4] = '£'
	// level[5][6] = '#'
	// level[5][6] = '@'

	//start the player on an empty square
	var playerX, playerY int
	//do while
	for ok := true; ok; ok = level[playerX][playerY] != '.' {
		playerX = rand.Intn(width)
		playerY = rand.Intn(height)
	}
	level, explored, playerX, playerY := generate()

	var visible [width][height]bool
	s.Clear()
	for {
		//player movement