finished chapter 3!

This commit is contained in:
gmzar 2019-07-16 18:52:08 -07:00
parent 9fb3b65800
commit 643bcfbf41
4 changed files with 62 additions and 17 deletions

2
go.mod
View File

@ -3,6 +3,6 @@ module steel
go 1.12 go 1.12
require ( require (
github.com/kettek/goro v0.0.0-20190713012409-1029522674ba github.com/kettek/goro v0.0.0-20190716230418-ed743d8603f6
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb // indirect golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb // indirect
) )

4
go.sum
View File

@ -34,6 +34,10 @@ github.com/kettek/goro v0.0.0-20190713012210-db6dc3381a1e h1:8UPf9Di15NxRSHdPi0Z
github.com/kettek/goro v0.0.0-20190713012210-db6dc3381a1e/go.mod h1:fO57DVF4MXRm+AOQWrFN0CFCbihOO36HUn42vjr00fU= github.com/kettek/goro v0.0.0-20190713012210-db6dc3381a1e/go.mod h1:fO57DVF4MXRm+AOQWrFN0CFCbihOO36HUn42vjr00fU=
github.com/kettek/goro v0.0.0-20190713012409-1029522674ba h1:HFhcgGAbvE1IhToLKr6IDfI7B2isbYY2yzB6rxV/mwQ= github.com/kettek/goro v0.0.0-20190713012409-1029522674ba h1:HFhcgGAbvE1IhToLKr6IDfI7B2isbYY2yzB6rxV/mwQ=
github.com/kettek/goro v0.0.0-20190713012409-1029522674ba/go.mod h1:fO57DVF4MXRm+AOQWrFN0CFCbihOO36HUn42vjr00fU= github.com/kettek/goro v0.0.0-20190713012409-1029522674ba/go.mod h1:fO57DVF4MXRm+AOQWrFN0CFCbihOO36HUn42vjr00fU=
github.com/kettek/goro v0.0.0-20190716001741-2ca1aaeebd5b h1:CMDJNDNpm6/ThhLQDxUSR475u49Bu5W4dVaVMxBCk0Q=
github.com/kettek/goro v0.0.0-20190716001741-2ca1aaeebd5b/go.mod h1:fO57DVF4MXRm+AOQWrFN0CFCbihOO36HUn42vjr00fU=
github.com/kettek/goro v0.0.0-20190716230418-ed743d8603f6 h1:+0W0fQIvYWnyS/tHViIQJjUBZ4Cm+8Bl6XZTR8jjrwU=
github.com/kettek/goro v0.0.0-20190716230418-ed743d8603f6/go.mod h1:fO57DVF4MXRm+AOQWrFN0CFCbihOO36HUn42vjr00fU=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=

12
main.go
View File

@ -1,9 +1,8 @@
package main package main
import ( import (
"log"
"github.com/kettek/goro" "github.com/kettek/goro"
"log"
"steel/entity" "steel/entity"
"steel/mapping" "steel/mapping"
@ -19,8 +18,12 @@ func main() {
screen.SetTitle("Steel Lord") screen.SetTitle("Steel Lord")
screen.SetSize(80, 40) screen.SetSize(80, 40)
// Randomize our seed so the map is randomized per run.
goro.SetSeed(goro.RandomSeed())
// Our initial variables. // Our initial variables.
mapWidth, mapHeight := 80, 40 mapWidth, mapHeight := 80, 40
maxRooms, roomMinSize, roomMaxSize := 30, 6, 10
colors := map[string]goro.Color{ colors := map[string]goro.Color{
"darkWall": goro.ColorGray, "darkWall": goro.ColorGray,
@ -34,8 +37,6 @@ func main() {
gameMap.Initialize() gameMap.Initialize()
gameMap.MakeMap()
player := entity.NewEntity(screen.Columns/2, screen.Rows/2+5, '@', goro.Style{Foreground: goro.ColorWhite}) player := entity.NewEntity(screen.Columns/2, screen.Rows/2+5, '@', goro.Style{Foreground: goro.ColorWhite})
npc := entity.NewEntity(screen.Columns/2-5, screen. Rows/2, '@', goro.Style{Foreground: goro.ColorYellow}) npc := entity.NewEntity(screen.Columns/2-5, screen. Rows/2, '@', goro.Style{Foreground: goro.ColorYellow})
@ -44,9 +45,8 @@ func main() {
npc, npc,
} }
gameMap.MakeMap(maxRooms, roomMinSize, roomMaxSize, player)
// The game loop.
for { for {
// Draw screen. // Draw screen.
DrawAll(screen, entities, gameMap, colors) DrawAll(screen, entities, gameMap, colors)

View File

@ -2,6 +2,8 @@ package mapping
import ( import (
"github.com/kettek/goro" "github.com/kettek/goro"
"steel/entity"
) )
// GameMap is our map data type. // GameMap is our map data type.
@ -25,14 +27,53 @@ func (g *GameMap) Initialize() {
} }
} }
// MakeMap populates our GameMap with rooms. // MakeMap creates a new randomized map. This is built according to the passed arguments.
func (g *GameMap) MakeMap() { func (g *GameMap) MakeMap(maxRooms, roomMinSize, roomMaxSize int, player *entity.Entity) {
room1 := NewRect(20, 15, 10, 15) var rooms []*Rect
room2 := NewRect(35, 15, 10, 15)
g.CreateRoom(room1) for r := 0; r < maxRooms; r++ {
g.CreateRoom(room2) // Generate a random width and height.
g.CreateHTunnel(25, 40, 23) width := roomMinSize + goro.Random.Intn(roomMaxSize)
height := roomMinSize + goro.Random.Intn(roomMaxSize)
// Generate a random position within the map boundaries.
x := goro.Random.Intn(g.Width - width - 1)
y := goro.Random.Intn(g.Height - height - 1)
// Create a Rect according to our generated sizes.
room := NewRect(x, y, width, height)
// Iterate through our existing rooms to check for intersection with our new room.
intersects := false
for _, otherRoom := range rooms {
if room.Intersect(otherRoom) {
intersects = true
break
}
}
// Add the room if there is no intersection found.
if !intersects {
g.CreateRoom(room)
roomCenterX, roomCenterY := room.Center()
// Always place the player in the center of the first room.
if len(rooms) == 0 {
player.X = roomCenterX
player.Y = roomCenterY
} else {
prevCenterX, prevCenterY := rooms[len(rooms)-1].Center()
// Flip a coin if we should tunnel horizontally or vertically first.
if goro.Random.Intn(1) == 1 {
g.CreateHTunnel(prevCenterX, roomCenterX, prevCenterY)
g.CreateVTunnel(prevCenterY, roomCenterY, roomCenterX)
} else {
g.CreateVTunnel(prevCenterY, roomCenterY, prevCenterX)
g.CreateHTunnel(prevCenterX, roomCenterX, roomCenterY)
}
}
// Append our new room to our rooms list.
rooms = append(rooms, room)
}
}
} }
// CreateRoom creates a room from a provided rect. // CreateRoom creates a room from a provided rect.
@ -67,11 +108,11 @@ func (g *GameMap) CreateVTunnel(y1, y2, x int) {
// IsBlocked returns if the given coordinates are blocking movement. // IsBlocked returns if the given coordinates are blocking movement.
func (g *GameMap) IsBlocked(x, y int) bool { func (g *GameMap) IsBlocked(x, y int) bool {
// Always block if ourside our GameMap's bounds. // Always block if ourside our GameMap's bounds.
if x < 0 || x >= g.Width || y < 0 || y >= g.Height { if !g.InBounds(x, y) {
return true return true
}
return g.Tiles[x][y].BlockMovement
} }
return g.Tiles[x][y].BlockMovement
}
// InBounds returns if the given coordinates are within the map's bounds. // InBounds returns if the given coordinates are within the map's bounds.
func (g *GameMap) InBounds (x, y int) bool { func (g *GameMap) InBounds (x, y int) bool {