Skip to content

Commit

Permalink
Replace function ComponentType by more comprehensive ComponentInfo (#318
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mlange-42 authored Jan 11, 2024
1 parent fdf6139 commit 195b5b8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ This change was necessary to get the same performance as before, despite the mor

### Features

* Adds functions `ComponentType(*World, ID) reflect.Type` and `ResourceType(*World, ID) reflect.Type` (#315)
* Adds methods `World.Ids(Entity) []ID` and `Query.Ids() []ID` (#315)
* Adds functions `ComponentInfo(*World, ID)` and `ResourceType(*World, ResID)` (#315, #318)
* Adds methods `World.Ids(Entity)` and `Query.Ids()` (#315)

### Other

Expand Down
8 changes: 8 additions & 0 deletions ecs/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ type componentType struct {
ID
Type reflect.Type
}

// CompInfo provides information about a registered component.
// Returned by [ComponentInfo].
type CompInfo struct {
ID ID
Type reflect.Type
IsRelation bool
}
15 changes: 12 additions & 3 deletions ecs/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,18 @@ func TypeID(w *World, tp reflect.Type) ID {
return w.componentID(tp)
}

// ComponentType returns the reflect.Type for a component [ID], and whether the ID is assigned.
func ComponentType(w *World, id ID) (reflect.Type, bool) {
return w.registry.ComponentType(id)
// ComponentInfo returns the [CompInfo] for a component [ID], and whether the ID is assigned.
func ComponentInfo(w *World, id ID) (CompInfo, bool) {
tp, ok := w.registry.ComponentType(id)
if !ok {
return CompInfo{}, false
}

return CompInfo{
ID: id,
Type: tp,
IsRelation: w.registry.IsRelation.Get(id),
}, true
}

// ResourceID returns the [ResID] for a resource type via generics.
Expand Down
12 changes: 6 additions & 6 deletions ecs/world_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,22 +182,22 @@ func TestWorldComponents(t *testing.T) {
assert.Panics(t, func() { w.Get(newEntityGen(1, 0), posID) })
}

func TestWorldTypes(t *testing.T) {
func TestWorldComponentInfo(t *testing.T) {
w := NewWorld()
_ = ComponentID[Velocity](&w)
posID := ComponentID[Position](&w)

tp, ok := ComponentType(&w, posID)
info, ok := ComponentInfo(&w, posID)
assert.True(t, ok)
assert.Equal(t, tp, reflect.TypeOf(Position{}))
assert.Equal(t, info.Type, reflect.TypeOf(Position{}))

tp, ok = ComponentType(&w, 3)
info, ok = ComponentInfo(&w, 3)
assert.False(t, ok)
assert.Equal(t, tp, nil)
assert.Equal(t, info, CompInfo{})

resID := ResourceID[Velocity](&w)

tp, ok = ResourceType(&w, resID)
tp, ok := ResourceType(&w, resID)
assert.True(t, ok)
assert.Equal(t, tp, reflect.TypeOf(Velocity{}))

Expand Down

0 comments on commit 195b5b8

Please sign in to comment.