diff --git a/main.go b/main.go index dcbda0c..fccadd4 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "github.com/kettek/goro" "steel/entity" + "steel/mapping" ) func main() { @@ -19,6 +20,20 @@ func main() { screen.SetSize(60, 14) // 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}) npc := entity.NewEntity(screen.Columns/2-5, screen. Rows/2, '@', goro.Style{Foreground: goro.ColorYellow}) @@ -32,14 +47,16 @@ func main() { // The game loop. for { // Draw screen. - DrawAll(screen, entities) + DrawAll(screen, entities, gameMap, colors) ClearAll(screen, entities) // Handle events. switch event := screen.WaitEvent().(type) { case goro.EventKey: switch action := handleKeyEvent(event).(type) { 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: goro.Quit() } diff --git a/mapping/gamemap.go b/mapping/gamemap.go new file mode 100644 index 0000000..297ea8a --- /dev/null +++ b/mapping/gamemap.go @@ -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 + } + diff --git a/mapping/tile.go b/mapping/tile.go new file mode 100644 index 0000000..d6ce77c --- /dev/null +++ b/mapping/tile.go @@ -0,0 +1,7 @@ +package mapping +// Tile represents the state of a given location in a GameMap. +type Tile struct { + BlockMovement bool + BlockSight bool +} + diff --git a/render.go b/render.go index b0b49f7..cbd63ad 100644 --- a/render.go +++ b/render.go @@ -4,10 +4,23 @@ import ( "github.com/kettek/goro" "steel/entity" + "steel/mapping" ) -// DrawAll draws all entities to the screen and flushes it. -func DrawAll(screen *goro.Screen, entities[]*entity.Entity) { +// 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"]) + } + } + } + // Draw all the entities. for _, entity := range entities { + DrawEntity(screen, entity) } screen.Flush() diff --git a/steel.exe b/steel.exe index 7840b30..2321b06 100644 Binary files a/steel.exe and b/steel.exe differ