Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace function ComponentType by more comprehensive ComponentInfo #318

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading