ident tweaks

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

View file

@ -2,7 +2,6 @@ package engine
import ( import (
"encoding/gob" "encoding/gob"
"fmt"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
) )
@ -50,16 +49,28 @@ func (g *Game) Update() error {
// RegisterComponent tells the game there is a new component. // RegisterComponent tells the game there is a new component.
func (g *Game) RegisterComponent(c interface{}) { func (g *Game) RegisterComponent(c interface{}) {
if id, ok := c.(Identifier); ok { i, ok := c.(Identifier)
g.componentsByID[id.Ident()] = c if !ok {
return
} }
id := i.Ident()
if id == "" {
return
}
g.componentsByID[id] = c
} }
// UnregisterComponent tells the game the component is no more. // UnregisterComponent tells the game the component is no more.
func (g *Game) UnregisterComponent(c interface{}) { func (g *Game) UnregisterComponent(c interface{}) {
if id, ok := c.(Identifier); ok { i, ok := c.(Identifier)
delete(g.componentsByID, id.Ident()) if !ok {
return
} }
id := i.Ident()
if id == "" {
return
}
delete(g.componentsByID, id)
} }
// Component returns the component with a given ID. // Component returns the component with a given ID.
@ -91,5 +102,4 @@ func (g *Game) Build() {
g.RegisterComponent(c) g.RegisterComponent(c)
return true return true
}) })
fmt.Printf("%#v\n", g.componentsByID)
} }

View file

@ -83,6 +83,7 @@ func main() {
}, },
}, },
&engine.Tilemap{ &engine.Tilemap{
ID: "terrain",
Map: tiles, Map: tiles,
Src: engine.ImageRef{Path: "assets/boxes.png"}, Src: engine.ImageRef{Path: "assets/boxes.png"},
TileSize: 16, TileSize: 16,