finish part2

This commit is contained in:
gmzar 2019-07-02 23:55:29 -07:00
parent 8fceb614a7
commit 6dabde08bc
5 changed files with 79 additions and 4 deletions

21
main.go
View File

@ -6,6 +6,7 @@ import (
"github.com/kettek/goro" "github.com/kettek/goro"
"steel/entity" "steel/entity"
"steel/mapping"
) )
func main() { func main() {
@ -19,6 +20,20 @@ func main() {
screen.SetSize(60, 14) screen.SetSize(60, 14)
// Our initial variables. // Our initial variables.
mapWidth, mapHeight := 80, 24
colors := map[string]goro.Color{
"darkWall": goro.Color{R: 0, G: 0, B: 100, A:255},
"darkGround": goro.Color{R: 50, G: 50, B: 150, A: 255},
}
gameMap := mapping.GameMap{
Width: mapWidth,
Height: mapHeight,
}
gameMap.Initialize()
player := entity.NewEntity(screen.Columns/2, screen.Rows/2, '@', goro.Style{Foreground: goro.ColorWhite}) player := entity.NewEntity(screen.Columns/2, screen.Rows/2, '@', 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})
@ -32,14 +47,16 @@ func main() {
// The game loop. // The game loop.
for { for {
// Draw screen. // Draw screen.
DrawAll(screen, entities) DrawAll(screen, entities, gameMap, colors)
ClearAll(screen, entities) ClearAll(screen, entities)
// Handle events. // Handle events.
switch event := screen.WaitEvent().(type) { switch event := screen.WaitEvent().(type) {
case goro.EventKey: case goro.EventKey:
switch action := handleKeyEvent(event).(type) { switch action := handleKeyEvent(event).(type) {
case ActionMove: case ActionMove:
player.Move(action.X, action.Y) if !gameMap.IsBlocked(player.X+action.X, player.Y+action.Y) {
player.Move(action.X, action.Y)
}
case ActionQuit: case ActionQuit:
goro.Quit() goro.Quit()
} }

38
mapping/gamemap.go Normal file
View File

@ -0,0 +1,38 @@
package mapping
// GameMap is our map data type.
type GameMap struct {
Width, Height int
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.
func (g *GameMap) Initialize() {
g.Tiles = make([][]Tile, g.Width)
for x := range g.Tiles {
g.Tiles[x] = make([]Tile, g. Height)
}
g.Tiles[30][22] = Tile {
BlockMovement: true,
BlockSight: true,
}
g.Tiles[31][22] = Tile{
BlockMovement: true,
BlockSight: true,
}
g.Tiles[32][22] = Tile{
BlockMovement: true,
BlockSight: true,
}
}
// IsBlocked returns if the given coordinates are blocking movement.
func (g *GameMap) IsBlocked(x, y int) bool {
// Always block if ourside our GameMap's bounds.
if x < 0 || x >= g.Width || y < 0 || y >= g.Height {
return true
}
return g.Tiles[x][y].BlockMovement
}

7
mapping/tile.go Normal file
View File

@ -0,0 +1,7 @@
package mapping
// Tile represents the state of a given location in a GameMap.
type Tile struct {
BlockMovement bool
BlockSight bool
}

View File

@ -4,10 +4,23 @@ import (
"github.com/kettek/goro" "github.com/kettek/goro"
"steel/entity" "steel/entity"
"steel/mapping"
) )
// DrawAll draws all entities 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[]*entity.Entity) { 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"])
}
}
}
// Draw all the entities.
for _, entity := range entities { for _, entity := range entities {
DrawEntity(screen, entity) DrawEntity(screen, entity)
} }
screen.Flush() screen.Flush()

BIN
steel.exe

Binary file not shown.