finish part2
This commit is contained in:
parent
8fceb614a7
commit
6dabde08bc
19
main.go
19
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:
|
||||
if !gameMap.IsBlocked(player.X+action.X, player.Y+action.Y) {
|
||||
player.Move(action.X, action.Y)
|
||||
}
|
||||
case ActionQuit:
|
||||
goro.Quit()
|
||||
}
|
||||
|
|
38
mapping/gamemap.go
Normal file
38
mapping/gamemap.go
Normal 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
7
mapping/tile.go
Normal 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
|
||||
}
|
||||
|
17
render.go
17
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()
|
||||
|
|
Loading…
Reference in New Issue
Block a user