From 696ccd216e16d3d2e3dbfa174859dbd8793829c5 Mon Sep 17 00:00:00 2001 From: gmzar Date: Fri, 19 Jul 2019 21:08:46 -0700 Subject: [PATCH] Got help and fixed up to start part 5! --- fov.go | 14 +++++++------- interfaces/gamemap.go | 12 ++++++++++++ main.go | 11 +++-------- mapping/gamemap.go | 24 +++++++++++++++++------- mapping/rect.go | 10 ++++++---- render.go | 5 ++--- 6 files changed, 47 insertions(+), 29 deletions(-) create mode 100644 interfaces/gamemap.go diff --git a/fov.go b/fov.go index 0cde484..b3c23c3 100644 --- a/fov.go +++ b/fov.go @@ -1,18 +1,18 @@ package main import ( - "steel/mapping" + "steel/interfaces" "github.com/kettek/goro/fov" ) -func InitializeFoV(g *mapping.GameMap) fov.Map { - fovMap := fov.NewMap(g.Width, g.Height, fov.AlgorithmBBQ) +func InitializeFoV(g interfaces.GameMap) fov.Map { + fovMap := fov.NewMap(g.Width(), g.Height(), fov.AlgorithmBBQ) - for x := range g.Tiles { - for y, tile := range g.Tiles[x] { - fovMap.SetBlocksMovement(x, y, tile.BlockMovement) - fovMap.SetBlocksLight(x, y, tile.BlockSight) + for x := 0; x < g.Width(); x++ { + for y := 0; y < g.Height(); y++ { + fovMap.SetBlocksMovement(x, y, g.IsBlocked(x, y)) + fovMap.SetBlocksLight(x, y, g.IsOpaque(x, y)) } } diff --git a/interfaces/gamemap.go b/interfaces/gamemap.go new file mode 100644 index 0000000..89f77de --- /dev/null +++ b/interfaces/gamemap.go @@ -0,0 +1,12 @@ +package interfaces + +// Our GameMap interface. This provides access to tile state and more. +type GameMap interface { + Width() int + Height() int + IsBlocked(x, y int) bool + IsOpaque(x, y int) bool + Explored(x, y int) bool + SetExplored(x, y int, explored bool) + MakeMap(maxRooms, roomMinSize, roomMaxSize int, player Entity) +} diff --git a/main.go b/main.go index 36c4e22..2e9233e 100644 --- a/main.go +++ b/main.go @@ -46,16 +46,11 @@ func main() { npc, } - gameMap := mapping.GameMap{ - Width: mapWidth, - Height: mapHeight, - } - - gameMap.Initialize() + gameMap := mapping.NewGameMap(mapWidth, mapHeight) gameMap.MakeMap(maxRooms, roomMinSize, roomMaxSize, player) - fovMap := InitializeFoV(&gameMap) + fovMap := InitializeFoV(gameMap) for { @@ -68,7 +63,7 @@ func main() { fovRecompute = false - ClearAll(screen, entities) + ClearAll(screen, entities, fovMap) // Handle events. switch event := screen.WaitEvent().(type) { diff --git a/mapping/gamemap.go b/mapping/gamemap.go index 4c50dc9..2d948de 100644 --- a/mapping/gamemap.go +++ b/mapping/gamemap.go @@ -12,9 +12,9 @@ type GameMap struct { tiles [][]Tile } -// 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. +// 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 NewGameMap(width, height int) interfaces.GameMap { - g := &GameMapmake{ + g := &GameMap{ width: width, height: height, } @@ -84,17 +84,17 @@ func (g *GameMap) CreateRoom(r Rect) { for x := r.X1 + 1; x < r.X2; x++ { for y := r.Y1 + 1; y < r.Y2; y++ { if g.InBounds(x, y) { - g.Tiles[x][y] = Tile{} + g.tiles[x][y] = Tile{} } } } } -//Create HTunnel creates a horizontal tunnel from x1 to/from x1 starting at y. +//CreateHTunnel creates a horizontal tunnel from x1 to/from x1 starting at y. func (g *GameMap) CreateHTunnel(x1, x2, y int) { for x := goro.MinInt(x1, x2); x <= goro.MaxInt(x1, x2); x++ { if g.InBounds(x, y) { - g.Tiles[x][y] = Tile{} + g.tiles[x][y] = Tile{} } } } @@ -103,7 +103,7 @@ func (g *GameMap) CreateHTunnel(x1, x2, y int) { func (g *GameMap) CreateVTunnel(y1, y2, x int) { for y := goro.MinInt(y1, y2); y <= goro.MaxInt(y1, y2); y++ { if g.InBounds(x, y) { - g.Tiles[x][y] = Tile{} + g.tiles[x][y] = Tile{} } } } @@ -133,7 +133,7 @@ func (g *GameMap) IsBlocked(x, y int) bool { if !g.InBounds(x, y) { return true } - return g.Tiles[x][y].Flags&BlockMovement != 0 + return g.tiles[x][y].Flags&BlockMovement != 0 } // IsOpaque returns if the given coordinates are blocking sight. @@ -144,6 +144,16 @@ func (g *GameMap) IsOpaque(x, y int) bool { return g.tiles[x][y].Flags&BlockSight != 0 } +// Width returns the width of the map. +func (g *GameMap) Width() int { + return g.width +} + +// Height returns the height of the map. +func (g *GameMap) Height() int { + return g.height +} + // InBounds returns if the given coordinates are within the map's bounds. func (g *GameMap) InBounds(x, y int) bool { if x < 0 || x >= g.width || y < 0 || y >= g.height { diff --git a/mapping/rect.go b/mapping/rect.go index 6a2495c..9664e6f 100644 --- a/mapping/rect.go +++ b/mapping/rect.go @@ -6,22 +6,24 @@ type Rect struct { } // NewRect returns a new Rect type with its properties calculated. -func NewRect(x int, y int, w int, h int) *Rect { - return &Rect{ +func NewRect(x int, y int, w int, h int) Rect { + return Rect{ X1: x, Y1: y, X2: x + w, Y2: y + h, } } + // Center returns the center of the Rect. func (r *Rect) Center() (x, y int) { return (r.X1 + r.X2) / 2, (r.Y1 + r.Y2) / 2 } + // Intersect returns a bool representing if the Rect intersects with another. -func (r *Rect) Intersect(other *Rect) bool { +func (r *Rect) Intersect(other Rect) bool { if r.X1 <= other.X2 && r.X2 >= other.X1 && r.Y1 <= other.Y2 && r.Y2 >= other.Y1 { return true } return false -} \ No newline at end of file +} diff --git a/render.go b/render.go index bba4a6b..ef8c63d 100644 --- a/render.go +++ b/render.go @@ -5,11 +5,10 @@ import ( "github.com/kettek/goro/fov" "steel/interfaces" - "steel/mapping" ) // 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 interfaces.GameMap, fovMap fov.Map, fovRecompute bool, colors map[string]goro.Color) { if fovRecompute { // Draw all the tiles in the game map. for x := 0; x < gameMap.Width(); x++ { @@ -51,7 +50,7 @@ func ClearAll(screen *goro.Screen, entities []interfaces.Entity, fovMap fov.Map) // DrawEntity draws a given entity to the screen. func DrawEntity(screen *goro.Screen, e interfaces.Entity, fovMap fov.Map) { if fovMap.Visible(e.X(), e.Y()) { - screen.SetRune(e.X()), e.Y(), e.Rune()) + screen.SetRune(e.X(), e.Y(), e.Rune()) screen.SetForeground(e.X(), e.Y(), e.Style().Foreground) } }