Converted to use entity interfaces
This commit is contained in:
parent
eea1fa9388
commit
085b1078d4
|
@ -1,25 +1,70 @@
|
||||||
package entity
|
package entity
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"steel/interfaces"
|
||||||
|
|
||||||
"github.com/kettek/goro"
|
"github.com/kettek/goro"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Entity is a type that represents an active entity in the world.
|
// Entity is a type that represents an active entity in the world.
|
||||||
type Entity struct {
|
type Entity struct {
|
||||||
X, Y int
|
x, y int
|
||||||
Rune rune
|
rune rune
|
||||||
Style goro.Style
|
style goro.Style
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewEntity returns a pointer to a newly created Entity.
|
||||||
|
func NewEntity(x int, y int, r rune, s goro.Style) interfaces.Entity {
|
||||||
|
return &Entity{
|
||||||
|
x: x,
|
||||||
|
y: y,
|
||||||
|
rune: r,
|
||||||
|
style: s,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Move moves the entity by a given amount.
|
// Move moves the entity by a given amount.
|
||||||
func (e *Entity) Move(x, y int) {
|
func (e *Entity) Move(x, y int) {
|
||||||
e.X += x
|
e.x += x
|
||||||
e.Y += y
|
e.y += y
|
||||||
}
|
}
|
||||||
// NewEntity returns a pointer to a newly created Entity.
|
|
||||||
func NewEntity(x int, y int, r rune, s goro.Style) *Entity {
|
// X returns the entity's X value.
|
||||||
return &Entity{
|
func (e *Entity) X() int {
|
||||||
X: x,
|
return e.x
|
||||||
Y: y,
|
|
||||||
Rune: r,
|
|
||||||
Style: s,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetX sets the entity's X value
|
||||||
|
func (e *Entity) SetX(x int) {
|
||||||
|
e.x = x
|
||||||
|
}
|
||||||
|
|
||||||
|
// Y returns the entity's Y value.
|
||||||
|
func (e *Entity) Y() int {
|
||||||
|
return e.y
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetY sets the entity's Y value
|
||||||
|
func (e *Entity) SetY(y int) {
|
||||||
|
e.y = y
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rune returns the entity's rune.
|
||||||
|
func (e *Entity) Rune() rune {
|
||||||
|
return e.rune
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetRune sets the entity's rune.
|
||||||
|
func (e *Entity) SetRune(r rune) {
|
||||||
|
e.rune = r
|
||||||
|
}
|
||||||
|
|
||||||
|
// Style returns the entity's style.
|
||||||
|
func (e *Entity) Style() goro.Style {
|
||||||
|
return e.style
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetStyle sets the entity's style.
|
||||||
|
func (e *Entity) SetStyle(s goro.Style) {
|
||||||
|
e.style = s
|
||||||
}
|
}
|
17
interfaces/entity.go
Normal file
17
interfaces/entity.go
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
package interfaces
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/kettek/goro"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Entity interface {
|
||||||
|
X() int
|
||||||
|
SetX(int)
|
||||||
|
Y() int
|
||||||
|
SetY(int)
|
||||||
|
Rune() rune
|
||||||
|
SetRune(rune)
|
||||||
|
Style() goro.Style
|
||||||
|
SetStyle(goro.Style)
|
||||||
|
Move(int, int)
|
||||||
|
}
|
7
main.go
7
main.go
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"steel/entity"
|
"steel/entity"
|
||||||
|
"steel/interfaces"
|
||||||
"steel/mapping"
|
"steel/mapping"
|
||||||
|
|
||||||
"github.com/kettek/goro"
|
"github.com/kettek/goro"
|
||||||
|
@ -40,7 +41,7 @@ func main() {
|
||||||
player := entity.NewEntity(screen.Columns/2, screen.Rows/2+5, '@', goro.Style{Foreground: goro.ColorWhite})
|
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})
|
npc := entity.NewEntity(screen.Columns/2-5, screen.Rows/2, '@', goro.Style{Foreground: goro.ColorYellow})
|
||||||
|
|
||||||
entities := []*entity.Entity{
|
entities := []interfaces.Entity{
|
||||||
player,
|
player,
|
||||||
npc,
|
npc,
|
||||||
}
|
}
|
||||||
|
@ -59,7 +60,7 @@ func main() {
|
||||||
for {
|
for {
|
||||||
|
|
||||||
if fovRecompute {
|
if fovRecompute {
|
||||||
RecomputeFov(fovMap, player.X, player.Y, fovRadius, fov.Light{})
|
RecomputeFov(fovMap, player.X(), player.Y(), fovRadius, fov.Light{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw screen.
|
// Draw screen.
|
||||||
|
@ -74,7 +75,7 @@ func main() {
|
||||||
case goro.EventKey:
|
case goro.EventKey:
|
||||||
switch action := handleKeyEvent(event).(type) {
|
switch action := handleKeyEvent(event).(type) {
|
||||||
case ActionMove:
|
case ActionMove:
|
||||||
if !gameMap.IsBlocked(player.X+action.X, player.Y+action.Y) {
|
if !gameMap.IsBlocked(player.X()+action.X, player.Y()+action.Y) {
|
||||||
player.Move(action.X, action.Y)
|
player.Move(action.X, action.Y)
|
||||||
fovRecompute = true
|
fovRecompute = true
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ package mapping
|
||||||
import (
|
import (
|
||||||
"github.com/kettek/goro"
|
"github.com/kettek/goro"
|
||||||
|
|
||||||
"steel/entity"
|
"steel/interfaces"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GameMap is our map data type.
|
// GameMap is our map data type.
|
||||||
|
@ -28,7 +28,7 @@ func (g *GameMap) Initialize() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// MakeMap creates a new randomized map. This is built according to the passed arguments.
|
// MakeMap creates a new randomized map. This is built according to the passed arguments.
|
||||||
func (g *GameMap) MakeMap(maxRooms, roomMinSize, roomMaxSize int, player *entity.Entity) {
|
func (g *GameMap) MakeMap(maxRooms, roomMinSize, roomMaxSize int, player interfaces.Entity) {
|
||||||
var rooms []*Rect
|
var rooms []*Rect
|
||||||
|
|
||||||
for r := 0; r < maxRooms; r++ {
|
for r := 0; r < maxRooms; r++ {
|
||||||
|
@ -56,8 +56,8 @@ func (g *GameMap) MakeMap(maxRooms, roomMinSize, roomMaxSize int, player *entity
|
||||||
|
|
||||||
// Always place the player in the center of the first room.
|
// Always place the player in the center of the first room.
|
||||||
if len(rooms) == 0 {
|
if len(rooms) == 0 {
|
||||||
player.X = roomCenterX
|
player.SetX(roomCenterX)
|
||||||
player.Y = roomCenterY
|
player.SetY(roomCenterY)
|
||||||
} else {
|
} else {
|
||||||
prevCenterX, prevCenterY := rooms[len(rooms)-1].Center()
|
prevCenterX, prevCenterY := rooms[len(rooms)-1].Center()
|
||||||
|
|
||||||
|
|
14
render.go
14
render.go
|
@ -4,12 +4,12 @@ import (
|
||||||
"github.com/kettek/goro"
|
"github.com/kettek/goro"
|
||||||
"github.com/kettek/goro/fov"
|
"github.com/kettek/goro/fov"
|
||||||
|
|
||||||
"steel/entity"
|
"steel/interfaces"
|
||||||
"steel/mapping"
|
"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 []*entity.Entity, gameMap mapping.GameMap, fovMap fov.Map, fovRecompute bool, colors map[string]goro.Color) {
|
func DrawAll(screen *goro.Screen, entities []interfaces.Entity, gameMap mapping.GameMap, fovMap fov.Map, fovRecompute bool, colors map[string]goro.Color) {
|
||||||
if fovRecompute {
|
if fovRecompute {
|
||||||
// Draw all the riles in the game map.
|
// Draw all the riles in the game map.
|
||||||
for x, column := range gameMap.Tiles {
|
for x, column := range gameMap.Tiles {
|
||||||
|
@ -41,18 +41,18 @@ func DrawAll(screen *goro.Screen, entities []*entity.Entity, gameMap mapping.Gam
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClearAll clears all entities from the screen.
|
// ClearAll clears all entities from the screen.
|
||||||
func ClearAll(screen *goro.Screen, entities []*entity.Entity) {
|
func ClearAll(screen *goro.Screen, entities []interfaces.Entity) {
|
||||||
for _, entity := range entities {
|
for _, entity := range entities {
|
||||||
ClearEntity(screen, entity)
|
ClearEntity(screen, entity)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DrawEntity draws a given entity to the screen.
|
// DrawEntity draws a given entity to the screen.
|
||||||
func DrawEntity(screen *goro.Screen, e *entity.Entity) {
|
func DrawEntity(screen *goro.Screen, e interfaces.Entity) {
|
||||||
screen.DrawRune(e.X, e.Y, e.Rune, e.Style)
|
screen.DrawRune(e.X(), e.Y(), e.Rune(), e.Style())
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClearEntity clears a given entity from the screen.
|
// ClearEntity clears a given entity from the screen.
|
||||||
func ClearEntity(screen *goro.Screen, e *entity.Entity) {
|
func ClearEntity(screen *goro.Screen, e interfaces.Entity) {
|
||||||
screen.SetRune(e.X, e.Y, ' ')
|
screen.SetRune(e.X(), e.Y(), ' ')
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user