Part 4 6/6 need help

This commit is contained in:
gmzar 2019-07-19 16:04:18 -07:00
parent 085b1078d4
commit f13438951a
3 changed files with 77 additions and 34 deletions

View File

@ -6,38 +6,41 @@ import (
"steel/interfaces" "steel/interfaces"
) )
// GameMap is our map data type. // GameMap is our map type for holding our tiles and dimensions.
type GameMap struct { type GameMap struct {
Width, Height int width, height int
Tiles [][]Tile tiles [][]Tile
} }
// Initialize initializes a GameMap's Tiles to match its Width and Height. It also sets up some coordinates to block movement and sight. // NewGameMap initializes a GameMap's Tiles to match provided width and height and sets up a few tiles to block movement and sight. Returns a GameMap interface.
func (g *GameMap) Initialize() { func NewGameMap(width, height int) interfaces.GameMap {
g.Tiles = make([][]Tile, g.Width) g := &GameMapmake{
width: width,
height: height,
}
g.tiles = make([][]Tile, g.width)
for x := range g.Tiles { for x := range g.tiles {
g.Tiles[x] = make([]Tile, g.Height) g.tiles[x] = make([]Tile, g.height)
for y := range g.Tiles[x] { for y := range g.tiles[x] {
g.Tiles[x][y] = Tile{ g.tiles[x][y].Flags = BlockMovement | BlockSight
BlockSight: true,
BlockMovement: true,
}
} }
} }
return g
} }
// MakeMap creates a new randomized map. This is built according to the passed arguments. // MakeMap creates a new randomized map. This is built according to the passed arguments.
func (g *GameMap) MakeMap(maxRooms, roomMinSize, roomMaxSize int, player interfaces.Entity) { func (g *GameMap) MakeMap(maxRooms, roomMinSize, roomMaxSize int, player interfaces.Entity) {
var rooms []*Rect var rooms []Rect
for r := 0; r < maxRooms; r++ { for r := 0; r < maxRooms; r++ {
// Generate a random width and height. // Generate a random width and height.
width := roomMinSize + goro.Random.Intn(roomMaxSize) width := roomMinSize + goro.Random.Intn(roomMaxSize)
height := roomMinSize + goro.Random.Intn(roomMaxSize) height := roomMinSize + goro.Random.Intn(roomMaxSize)
// Generate a random position within the map boundaries. // Generate a random position within the map boundaries.
x := goro.Random.Intn(g.Width - width - 1) x := goro.Random.Intn(g.width - width - 1)
y := goro.Random.Intn(g.Height - height - 1) y := goro.Random.Intn(g.height - height - 1)
// Create a Rect according to our generated sizes. // Create a Rect according to our generated sizes.
room := NewRect(x, y, width, height) room := NewRect(x, y, width, height)
@ -77,7 +80,7 @@ func (g *GameMap) MakeMap(maxRooms, roomMinSize, roomMaxSize int, player interfa
} }
// CreateRoom creates a room from a provided rect. // CreateRoom creates a room from a provided rect.
func (g *GameMap) CreateRoom(r *Rect) { func (g *GameMap) CreateRoom(r Rect) {
for x := r.X1 + 1; x < r.X2; x++ { for x := r.X1 + 1; x < r.X2; x++ {
for y := r.Y1 + 1; y < r.Y2; y++ { for y := r.Y1 + 1; y < r.Y2; y++ {
if g.InBounds(x, y) { if g.InBounds(x, y) {
@ -105,18 +108,45 @@ func (g *GameMap) CreateVTunnel(y1, y2, x int) {
} }
} }
// Explored returns if the tile at x by y has been explored.
func (g *GameMap) Explored(x, y int) bool {
if g.InBounds(x, y) {
return g.tiles[x][y].Flags&Explored != 0
}
return false
}
// SetExplored sets the explored state of the tile at x and y to the passed explored bool.
func (g *GameMap) SetExplored(x, y int, explored bool) {
if g.InBounds(x, y) {
if explored {
g.tiles[x][y].Flags = g.tiles[x][y].Flags | Explored
} else {
g.tiles[x][y].Flags = g.tiles[x][y].Flags &^ Explored
}
}
}
// 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 !g.InBounds(x, y) { if !g.InBounds(x, y) {
return true return true
} }
return g.Tiles[x][y].BlockMovement return g.Tiles[x][y].Flags&BlockMovement != 0
}
// IsOpaque returns if the given coordinates are blocking sight.
func (g *GameMap) IsOpaque(x, y int) bool {
if !g.InBounds(x, y) {
return true
}
return g.tiles[x][y].Flags&BlockSight != 0
} }
// 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 {
if x < 0 || x >= g.Width || y < 0 || y >= g.Height { if x < 0 || x >= g.width || y < 0 || y >= g.height {
return false return false
} }
return true return true

View File

@ -1,6 +1,13 @@
package mapping package mapping
// Tile represents the state of a given location in a GameMap. // Tile represents the state of a given location in a GameMap.
type Tile struct { type Tile struct {
BlockMovement bool Flags uint
BlockSight bool
} }
// Our Tile's flags.
const (
BlockMovement = 1 << iota
BlockSight
Explored
)

View File

@ -11,19 +11,20 @@ import (
// DrawAll draws all entities and the gameMap to the screen and flushes it. // DrawAll draws all entities and the gameMap to the screen and flushes it.
func DrawAll(screen *goro.Screen, entities []interfaces.Entity, gameMap mapping.GameMap, fovMap fov.Map, fovRecompute bool, colors map[string]goro.Color) { func DrawAll(screen *goro.Screen, entities []interfaces.Entity, gameMap mapping.GameMap, fovMap fov.Map, fovRecompute bool, colors map[string]goro.Color) {
if fovRecompute { if fovRecompute {
// Draw all the riles in the game map. // Draw all the tiles in the game map.
for x, column := range gameMap.Tiles { for x := 0; x < gameMap.Width(); x++ {
for y, tile := range column { for y := 0; y < gameMap.Height(); y++ {
visible := fovMap.Visible(x, y) visible := fovMap.Visible(x, y)
if visible { if visible {
if tile.BlockSight { if gameMap.IsBlocked(x, y) {
screen.SetBackground(x, y, colors["lightWall"]) screen.SetBackground(x, y, colors["lightWall"])
} else { } else {
screen.SetBackground(x, y, colors["lightGround"]) screen.SetBackground(x, y, colors["lightGround"])
} }
} else { gameMap.SetExplored(x, y, true)
if tile.BlockSight { } else if gameMap.Explored(x, y) {
if gameMap.IsBlocked(x, y) {
screen.SetBackground(x, y, colors["darkWall"]) screen.SetBackground(x, y, colors["darkWall"])
} else { } else {
screen.SetBackground(x, y, colors["darkGround"]) screen.SetBackground(x, y, colors["darkGround"])
@ -35,24 +36,29 @@ func DrawAll(screen *goro.Screen, entities []interfaces.Entity, gameMap mapping.
// Draw all the entities in the game map. // Draw all the entities in the game map.
for _, entity := range entities { for _, entity := range entities {
DrawEntity(screen, entity) DrawEntity(screen, entity, fovMap)
} }
screen.Flush() screen.Flush()
} }
// ClearAll clears all entities from the screen. // ClearAll clears all entities from the screen.
func ClearAll(screen *goro.Screen, entities []interfaces.Entity) { func ClearAll(screen *goro.Screen, entities []interfaces.Entity, fovMap fov.Map) {
for _, entity := range entities { for _, entity := range entities {
ClearEntity(screen, entity) ClearEntity(screen, entity, fovMap)
} }
} }
// DrawEntity draws a given entity to the screen. // DrawEntity draws a given entity to the screen.
func DrawEntity(screen *goro.Screen, e interfaces.Entity) { func DrawEntity(screen *goro.Screen, e interfaces.Entity, fovMap fov.Map) {
screen.DrawRune(e.X(), e.Y(), e.Rune(), e.Style()) if fovMap.Visible(e.X(), e.Y()) {
screen.SetRune(e.X()), e.Y(), e.Rune())
screen.SetForeground(e.X(), e.Y(), e.Style().Foreground)
}
} }
// ClearEntity clears a given entity from the screen. // ClearEntity clears a given entity from the screen.
func ClearEntity(screen *goro.Screen, e interfaces.Entity) { func ClearEntity(screen *goro.Screen, e interfaces.Entity, fovMap fov.Map) {
screen.SetRune(e.X(), e.Y(), ' ') if fovMap.Visible(e.X(), e.Y()) {
screen.SetRune(e.X(), e.Y(), ' ')
}
} }