Browse Source

Print Component name when debugging

main
Trevor Slocum 1 month ago
parent
commit
e08cfe7970
  1. 10
      component.go
  2. 14
      context.go
  3. 61
      world.go

10
component.go

@ -1,5 +1,9 @@
package gohan
import (
"strconv"
)
// ComponentID is a component identifier. Each Component is assigned a unique ID
// via World.NewComponentID, and implements a ComponentID method returning its ID.
type ComponentID int
@ -23,6 +27,8 @@ func (w *World) NewComponentID() ComponentID {
w.components[i] = append(w.components[i], nil)
}
w.systemComponentNames = append(w.systemComponentNames, strconv.Itoa(int(w.maxComponentID)))
return w.maxComponentID
}
@ -32,6 +38,10 @@ func (w *World) AddComponent(entity Entity, component Component) {
defer w.componentMutex.Unlock()
componentID := component.ComponentID()
if debug != 0 && !w.haveSystemComponentName[componentID] {
w.systemComponentNames[componentID] = getName(component)
w.haveSystemComponentName[componentID] = true
}
w.components[entity][componentID] = component

14
context.go

@ -1,7 +1,5 @@
package gohan
import "log"
// Context represents the current iteration of a System's matching entities. It
// provides methods for retrieving components for the currently matched Entity,
// and removing the currently matched Entity.
@ -16,18 +14,6 @@ type Context struct {
// Component gets a Component of the currently handled Entity.
func (ctx *Context) Component(componentID ComponentID) interface{} {
if debug != 0 {
var found bool
for _, id := range ctx.allowed {
if id == componentID {
found = true
break
}
}
if !found {
log.Panicf("illegal component access: component %d is not needed or used by %s", componentID, ctx.world.systemName(ctx.systemIndex))
}
}
return ctx.components[componentID]
}

61
world.go

@ -68,6 +68,9 @@ type World struct {
systemDrawnEntitiesV int
systemDrawnEntitiesT time.Time
systemComponentNames []string
haveSystemComponentName map[ComponentID]bool
cacheTime time.Duration
ctx *Context
@ -84,6 +87,7 @@ func NewWorld() *World {
cacheTime: time.Second,
handledModifiedEntities: make(map[Entity]bool),
haveSystemComponentName: make(map[ComponentID]bool),
}
w.ctx = &Context{
@ -92,6 +96,7 @@ func NewWorld() *World {
// Pad slices to match IDs starting with 1.
w.components = append(w.components, nil)
w.systemComponentNames = append(w.systemComponentNames, "")
return w
}
@ -143,14 +148,6 @@ func AddSystemAfter(system System, after ...System) {
}
*/
func (w *World) systemName(i int) string {
t := reflect.TypeOf(w.systems[i])
for t.Kind() == reflect.Ptr {
return strings.Title(t.Elem().Name())
}
return strings.Title(t.Name())
}
func (w *World) updateSystem(i int) (int, error) {
w.ctx.systemIndex = i
w.ctx.allowed = w.systemComponentIDs[i]
@ -390,18 +387,6 @@ func (w *World) CurrentDraws() int {
return w.systemDrawnEntitiesV
}
func uniqueComponentIDs(v []ComponentID) []ComponentID {
var list []ComponentID
keys := make(map[ComponentID]bool)
for _, entry := range v {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
// Preallocate creates and then immediately removes the specified number of entities.
// Because Gohan reuses removed entities, this has the effect of pre-allocating
// the memory used later to create entities normally. Pre-allocating enough
@ -420,3 +405,39 @@ func (w *World) Preallocate(entities int) {
w.RemoveEntity(e[i])
}
}
func uniqueComponentIDs(v []ComponentID) []ComponentID {
var list []ComponentID
keys := make(map[ComponentID]bool)
for _, entry := range v {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
func (w *World) componentName(id ComponentID) string {
if int(id) < len(w.systemComponentNames) {
return w.systemComponentNames[id]
}
return strconv.Itoa(int(id))
}
func (w *World) systemName(i int) string {
if i < len(w.systems) {
return getName(w.systems[i])
}
return strconv.Itoa(i)
}
func getName(v interface{}) string {
t := reflect.TypeOf(v)
if t.Kind() == reflect.Ptr {
return strings.Title(t.Elem().Name())
} else if t.Kind() == reflect.Struct {
return strings.Title(t.Name())
}
return ""
}

Loading…
Cancel
Save