Got help and fixed up to start part 5!
This commit is contained in:
parent
f13438951a
commit
696ccd216e
14
fov.go
14
fov.go
|
@ -1,18 +1,18 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"steel/mapping"
|
"steel/interfaces"
|
||||||
|
|
||||||
"github.com/kettek/goro/fov"
|
"github.com/kettek/goro/fov"
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitializeFoV(g *mapping.GameMap) fov.Map {
|
func InitializeFoV(g interfaces.GameMap) fov.Map {
|
||||||
fovMap := fov.NewMap(g.Width, g.Height, fov.AlgorithmBBQ)
|
fovMap := fov.NewMap(g.Width(), g.Height(), fov.AlgorithmBBQ)
|
||||||
|
|
||||||
for x := range g.Tiles {
|
for x := 0; x < g.Width(); x++ {
|
||||||
for y, tile := range g.Tiles[x] {
|
for y := 0; y < g.Height(); y++ {
|
||||||
fovMap.SetBlocksMovement(x, y, tile.BlockMovement)
|
fovMap.SetBlocksMovement(x, y, g.IsBlocked(x, y))
|
||||||
fovMap.SetBlocksLight(x, y, tile.BlockSight)
|
fovMap.SetBlocksLight(x, y, g.IsOpaque(x, y))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
12
interfaces/gamemap.go
Normal file
12
interfaces/gamemap.go
Normal file
|
@ -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)
|
||||||
|
}
|
11
main.go
11
main.go
|
@ -46,16 +46,11 @@ func main() {
|
||||||
npc,
|
npc,
|
||||||
}
|
}
|
||||||
|
|
||||||
gameMap := mapping.GameMap{
|
gameMap := mapping.NewGameMap(mapWidth, mapHeight)
|
||||||
Width: mapWidth,
|
|
||||||
Height: mapHeight,
|
|
||||||
}
|
|
||||||
|
|
||||||
gameMap.Initialize()
|
|
||||||
|
|
||||||
gameMap.MakeMap(maxRooms, roomMinSize, roomMaxSize, player)
|
gameMap.MakeMap(maxRooms, roomMinSize, roomMaxSize, player)
|
||||||
|
|
||||||
fovMap := InitializeFoV(&gameMap)
|
fovMap := InitializeFoV(gameMap)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
|
||||||
|
@ -68,7 +63,7 @@ func main() {
|
||||||
|
|
||||||
fovRecompute = false
|
fovRecompute = false
|
||||||
|
|
||||||
ClearAll(screen, entities)
|
ClearAll(screen, entities, fovMap)
|
||||||
|
|
||||||
// Handle events.
|
// Handle events.
|
||||||
switch event := screen.WaitEvent().(type) {
|
switch event := screen.WaitEvent().(type) {
|
||||||
|
|
|
@ -12,9 +12,9 @@ type GameMap struct {
|
||||||
tiles [][]Tile
|
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 {
|
func NewGameMap(width, height int) interfaces.GameMap {
|
||||||
g := &GameMapmake{
|
g := &GameMap{
|
||||||
width: width,
|
width: width,
|
||||||
height: height,
|
height: height,
|
||||||
}
|
}
|
||||||
|
@ -84,17 +84,17 @@ 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) {
|
||||||
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) {
|
func (g *GameMap) CreateHTunnel(x1, x2, y int) {
|
||||||
for x := goro.MinInt(x1, x2); x <= goro.MaxInt(x1, x2); x++ {
|
for x := goro.MinInt(x1, x2); x <= goro.MaxInt(x1, x2); x++ {
|
||||||
if g.InBounds(x, y) {
|
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) {
|
func (g *GameMap) CreateVTunnel(y1, y2, x int) {
|
||||||
for y := goro.MinInt(y1, y2); y <= goro.MaxInt(y1, y2); y++ {
|
for y := goro.MinInt(y1, y2); y <= goro.MaxInt(y1, y2); y++ {
|
||||||
if g.InBounds(x, 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) {
|
if !g.InBounds(x, y) {
|
||||||
return true
|
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.
|
// 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
|
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.
|
// 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 {
|
||||||
|
|
|
@ -6,20 +6,22 @@ type Rect struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRect returns a new Rect type with its properties calculated.
|
// NewRect returns a new Rect type with its properties calculated.
|
||||||
func NewRect(x int, y int, w int, h int) *Rect {
|
func NewRect(x int, y int, w int, h int) Rect {
|
||||||
return &Rect{
|
return Rect{
|
||||||
X1: x,
|
X1: x,
|
||||||
Y1: y,
|
Y1: y,
|
||||||
X2: x + w,
|
X2: x + w,
|
||||||
Y2: y + h,
|
Y2: y + h,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Center returns the center of the Rect.
|
// Center returns the center of the Rect.
|
||||||
func (r *Rect) Center() (x, y int) {
|
func (r *Rect) Center() (x, y int) {
|
||||||
return (r.X1 + r.X2) / 2, (r.Y1 + r.Y2) / 2
|
return (r.X1 + r.X2) / 2, (r.Y1 + r.Y2) / 2
|
||||||
}
|
}
|
||||||
|
|
||||||
// Intersect returns a bool representing if the Rect intersects with another.
|
// 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 {
|
if r.X1 <= other.X2 && r.X2 >= other.X1 && r.Y1 <= other.Y2 && r.Y2 >= other.Y1 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,11 +5,10 @@ import (
|
||||||
"github.com/kettek/goro/fov"
|
"github.com/kettek/goro/fov"
|
||||||
|
|
||||||
"steel/interfaces"
|
"steel/interfaces"
|
||||||
"steel/mapping"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// 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 interfaces.GameMap, fovMap fov.Map, fovRecompute bool, colors map[string]goro.Color) {
|
||||||
if fovRecompute {
|
if fovRecompute {
|
||||||
// Draw all the tiles in the game map.
|
// Draw all the tiles in the game map.
|
||||||
for x := 0; x < gameMap.Width(); x++ {
|
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.
|
// DrawEntity draws a given entity to the screen.
|
||||||
func DrawEntity(screen *goro.Screen, e interfaces.Entity, fovMap fov.Map) {
|
func DrawEntity(screen *goro.Screen, e interfaces.Entity, fovMap fov.Map) {
|
||||||
if fovMap.Visible(e.X(), e.Y()) {
|
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)
|
screen.SetForeground(e.X(), e.Y(), e.Style().Foreground)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user