From 8bbf6c4ff518e6f334e742203449136caac620c5 Mon Sep 17 00:00:00 2001 From: gmzar Date: Wed, 17 Jul 2019 01:30:15 -0700 Subject: [PATCH] got to 3/6 of part 4, but failed to build --- fov.go | 23 +++++++++++++++++++++++ go.mod | 1 + go.sum | 3 +++ main.go | 45 ++++++++++++++++++++++++++++++++------------- render.go | 55 ++++++++++++++++++++++++++++++++++++------------------- 5 files changed, 95 insertions(+), 32 deletions(-) create mode 100644 fov.go diff --git a/fov.go b/fov.go new file mode 100644 index 0000000..11aa3d0 --- /dev/null +++ b/fov.go @@ -0,0 +1,23 @@ +package main + +import ( + "steel/mapping" + "github.com/kettek/goro/fov" +) + +func InitializeFoV(g *mapping.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) + } + } + + return fovMap +} + +func RecomputeFov(fovMap fov.Map, centerX, centerY int, radius int, fov.Light) { + fovMap.Recompute(centerX, centerY, radius, light) +} diff --git a/go.mod b/go.mod index 6bf0e24..cf5db60 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,6 @@ go 1.12 require ( github.com/kettek/goro v0.0.0-20190716230418-ed743d8603f6 + github.com/kettek/goro-game v0.0.0-20190715002527-c93e962913d4 golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb // indirect ) diff --git a/go.sum b/go.sum index 104e8ec..253ea25 100644 --- a/go.sum +++ b/go.sum @@ -34,10 +34,13 @@ 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-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-20190714065548-99acc2c2dfc6/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/kettek/goro-game v0.0.0-20190715002527-c93e962913d4 h1:lKD8ikzpJJNDFrEr4IOutR1xPrrAm4XSvw4+jqXzquQ= +github.com/kettek/goro-game v0.0.0-20190715002527-c93e962913d4/go.mod h1:I19bCZN5DV5IpzeLR2WHbxnXEwsLnfms3ZszaTjoJUo= 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/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= diff --git a/main.go b/main.go index cc227d2..0599861 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,10 @@ package main import ( "github.com/kettek/goro" + "github.com/kettek/goro-game/entity" + "github.com/kettek/goro-game/mapping" + "github.com/kettek/goro/fov" "log" - "steel/entity" "steel/mapping" ) @@ -13,6 +15,7 @@ func main() { if err := goro.InitEbiten(); err != nil { log.Fatal(err) } + goro.Run(func(screen *goro.Screen) { // Screen configuration. screen.SetTitle("Steel Lord") @@ -25,9 +28,22 @@ func main() { mapWidth, mapHeight := 80, 40 maxRooms, roomMinSize, roomMaxSize := 30, 6, 10 + fovRadius := 10 + fovRecompute := true + colors := map[string]goro.Color{ - "darkWall": goro.ColorGray, - "darkGround": goro.ColorGreen, + "darkWall": goro.Color{R: 25, G: 25, B: 25, A: 255}, + "darkGround": goro.Color{R: 100, G: 100, B: 100, A: 255}, + "lightWall": goro.Color{R: 50, G: 50, B: 50, A: 255}, + "lightGround": goro.Color{R: 150, G: 150, B: 150, A: 255}, + } + + 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}) + + entities := []*entity.Entity{ + player, + npc, } gameMap := mapping.GameMap{ @@ -36,21 +52,24 @@ func main() { } gameMap.Initialize() + + gameMap.MakeMap(maxRooms, roomMinSize, roomMaxSize, player) - 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}) - - entities := []*entity.Entity{ - player, - npc, - } - - gameMap.MakeMap(maxRooms, roomMinSize, roomMaxSize, player) + fovMap := InitializeFoV(&gameMap) for { + + if fovRecompute { + RecomputeFoV(fovMap, player.X, player.Y, fovRadius, fov.Light{}) + } + // Draw screen. - DrawAll(screen, entities, gameMap, colors) + DrawAll(screen, entities, gameMap, fovMap, fovRecompute, colors) + + fovRecompute = false + ClearAll(screen, entities) + // Handle events. switch event := screen.WaitEvent().(type) { case goro.EventKey: diff --git a/render.go b/render.go index cbd63ad..7cb08e8 100644 --- a/render.go +++ b/render.go @@ -2,40 +2,57 @@ package main import ( "github.com/kettek/goro" + "github.com/kettek/goro/fov" "steel/entity" "steel/mapping" ) + // DrawAll draws all entities and the gameMap to the screen and flushes it. -func DrawAll(screen *goro.Screen, entities[]*entity.Entity, gameMap mapping.GameMap, colors map[string]goro.Color) { - // Draw all the tiles within the game map. - for x, column := range gameMap.Tiles { - for y, tile := range column { - if tile.BlockSight { - screen.SetBackground(x, y, colors["darkWall"]) - } else { - screen.SetBackground(x, y, colors["darkGround"]) +func DrawAll(screen *goro.Screen, entities []*entity.Entity, gameMap mapping.GameMap, fovMap fov.Map, fovRecompute bool, colors map[string]goro.Color) { + if fovRecompute { + // Draw all the riles in the game map. + for x, column := range gameMap.Tiles { + for y, tile := range column { + visible := fovMap.Visible(x, y) + + if visible { + if tile.BlockSight { + screen.SetBackground(x, y, colors["lightWall"]) + } else { + screen.SetBackground(x, y, colors["lightGround"]) + } + } else { + if tile.BlockSight { + screen.SetBackground(x, y, colors["darkWall"]) + } else { + screen.SetBackground(x, y, colors["darkGround"]) + } } } } - // Draw all the entities. - for _, entity := range entities { - - DrawEntity(screen, entity) - } - screen.Flush() } + +// Draw all the entities in the game map. +for_, entity := range entities { + DrawEntity(screen, entity) +} +screen.Flush() +} + // ClearAll clears all entities from the screen. func ClearAll(screen *goro.Screen, entities[]*entity.Entity) { for _, entity := range entities { ClearEntity(screen, entity) + } } -} + // DrawEntity draws a given entity to the screen. -func DrawEntity(screen *goro.Screen, e*entity.Entity) { +func DrawEntity(screen *goro.Screen, e *entity.Entity) { screen.DrawRune(e.X, e.Y, e.Rune, e.Style) } + // ClearEntity clears a given entity from the screen. -func ClearEntity(screen *goro.Screen, e*entity.Entity) { - screen.DrawRune(e.X, e.Y, ' ', goro.Style{}) -} \ No newline at end of file +func ClearEntity(screen *goro.Screen, e *entity.Entity) { + screen.SetRune(e.X, e.Y, ' ') +}