Hidden, Identifier, z.go->misc.go

This commit is contained in:
Josh Deprez 2021-08-01 17:08:26 +10:00 committed by Josh Deprez
parent cd7b0fae99
commit 0af9bf298c
6 changed files with 49 additions and 13 deletions

View file

@ -18,9 +18,14 @@ func init() {
} }
// PerfDisplay debugprints CurrentTPS and CurrentFPS in the top left. // PerfDisplay debugprints CurrentTPS and CurrentFPS in the top left.
type PerfDisplay struct{} type PerfDisplay struct {
Hidden bool
}
func (PerfDisplay) Draw(screen *ebiten.Image, _ ebiten.GeoM) { func (p PerfDisplay) Draw(screen *ebiten.Image, _ ebiten.GeoM) {
if p.Hidden {
return
}
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f FPS: %0.2f", ebiten.CurrentTPS(), ebiten.CurrentFPS())) ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f FPS: %0.2f", ebiten.CurrentTPS(), ebiten.CurrentFPS()))
} }

View file

@ -2,6 +2,7 @@ package engine
import ( import (
"encoding/gob" "encoding/gob"
"fmt"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
) )
@ -10,10 +11,10 @@ func init() {
gob.Register(Game{}) gob.Register(Game{})
} }
// IDer components have a name. This makes it easier for components to // Identifier components have a sense of self. This makes it easier for
// find and interact with one another. // components to find and interact with one another.
type IDer interface { type Identifier interface {
ID() string Ident() string
} }
// Scanner components can be scanned. It is called when the game // Scanner components can be scanned. It is called when the game
@ -47,6 +48,20 @@ func (g *Game) Update() error {
return g.Scene.Update() return g.Scene.Update()
} }
// RegisterComponent tells the game there is a new component.
func (g *Game) RegisterComponent(c interface{}) {
if id, ok := c.(Identifier); ok {
g.componentsByID[id.Ident()] = c
}
}
// UnregisterComponent tells the game the component is no more.
func (g *Game) UnregisterComponent(c interface{}) {
if id, ok := c.(Identifier); ok {
delete(g.componentsByID, id.Ident())
}
}
// Component returns the component with a given ID. // Component returns the component with a given ID.
func (g *Game) Component(id string) interface{} { return g.componentsByID[id] } func (g *Game) Component(id string) interface{} { return g.componentsByID[id] }
@ -71,12 +86,10 @@ func (g *Game) walk(c interface{}, v func(interface{}) bool) {
// Build builds the component database. // Build builds the component database.
func (g *Game) Build() { func (g *Game) Build() {
byID := make(map[string]interface{}) g.componentsByID = make(map[string]interface{})
g.walk(g.Scene, func(c interface{}) bool { g.Walk(func(c interface{}) bool {
if id, ok := c.(IDer); ok { g.RegisterComponent(c)
byID[id.ID()] = c
}
return true return true
}) })
g.componentsByID = byID fmt.Printf("%#v\n", g.componentsByID)
} }

View file

@ -1,5 +1,11 @@
package engine package engine
// ID implements Identifier directly (as a string value).
type ID string
// Ident returns id as a string.
func (id ID) Ident() string { return string(id) }
// ZPos implements ZPositioner directly (as a float64 value). // ZPos implements ZPositioner directly (as a float64 value).
type ZPos float64 type ZPos float64

View file

@ -30,11 +30,17 @@ type ZPositioner interface {
// Scene manages drawing and updating a bunch of components. // Scene manages drawing and updating a bunch of components.
type Scene struct { type Scene struct {
Components []interface{} Components []interface{}
Hidden bool
ID
Transform ebiten.GeoM Transform ebiten.GeoM
ZPos
} }
// Draw draws all components in order. // Draw draws all components in order.
func (s *Scene) Draw(screen *ebiten.Image, geom ebiten.GeoM) { func (s *Scene) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
if s.Hidden {
return
}
geom.Concat(s.Transform) geom.Concat(s.Transform)
for _, i := range s.Components { for _, i := range s.Components {
if d, ok := i.(Drawer); ok { if d, ok := i.(Drawer); ok {

View file

@ -15,6 +15,8 @@ func init() {
// Tilemap renders a grid of tiles. // Tilemap renders a grid of tiles.
type Tilemap struct { type Tilemap struct {
Hidden bool
ID
Map [][]Tile Map [][]Tile
Src ImageRef // must be a horizontal tile set Src ImageRef // must be a horizontal tile set
TileSize int TileSize int
@ -24,6 +26,9 @@ type Tilemap struct {
// Draw draws the tilemap. // Draw draws the tilemap.
func (t *Tilemap) Draw(screen *ebiten.Image, geom ebiten.GeoM) { func (t *Tilemap) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
if t.Hidden {
return
}
geom.Concat(t.Transform) geom.Concat(t.Transform)
for j, row := range t.Map { for j, row := range t.Map {
for i, tile := range row { for i, tile := range row {

View file

@ -74,6 +74,7 @@ func main() {
ScreenHeight: screenHeight, ScreenHeight: screenHeight,
ScreenWidth: screenWidth, ScreenWidth: screenWidth,
Scene: &engine.Scene{ Scene: &engine.Scene{
ID: "root_scene",
Components: []interface{}{ Components: []interface{}{
&engine.GobDumper{ &engine.GobDumper{
KeyCombo: []ebiten.Key{ KeyCombo: []ebiten.Key{