diff --git a/entry.go b/entry.go index 8291e8f..e14e56f 100644 --- a/entry.go +++ b/entry.go @@ -53,8 +53,12 @@ func Set[T any](e *Entry, ctype component.IComponentType, component *T) { // SetValue sets the value of the component. func SetValue[T any](e *Entry, ctype component.IComponentType, value T) { - c := Get[T](e, ctype) - *c = value + *Get[T](e, ctype) = value +} + +// GetValue gets the value of the component. +func GetValue[T any](e *Entry, ctype component.IComponentType) T { + return *Get[T](e, ctype) } // Remove removes the component from the entry. diff --git a/entry_test.go b/entry_test.go new file mode 100644 index 0000000..8ce8a11 --- /dev/null +++ b/entry_test.go @@ -0,0 +1,61 @@ +package donburi_test + +import ( + "testing" + + "github.com/yohamta/donburi" +) + +func TestSetValue(t *testing.T) { + var ( + transform = donburi.NewComponentType[transformData]() + ) + world := donburi.NewWorld() + a := world.Create(transform) + entryA := world.Entry(a) + + trData := transformData{ + Position: vec2f{10, 20}, + } + donburi.SetValue(entryA, transform, trData) + got := donburi.GetValue[transformData](entryA, transform) + + if got != trData { + t.Errorf("got: %v, want: %v", got, trData) + } +} + +func TestGetComponents(t *testing.T) { + var ( + transform = donburi.NewComponentType[transformData]() + velocity = donburi.NewComponentType[velocityData]() + tag = donburi.NewTag().SetName("tag") + ) + + world := donburi.NewWorld() + a := world.Create(transform, velocity, tag) + entryA := world.Entry(a) + + trData := transformData{ + Position: vec2f{10, 20}, + } + veData := velocityData{ + Velocity: vec2f{30, 40}, + } + + donburi.SetValue(entryA, transform, trData) + donburi.SetValue(entryA, velocity, veData) + + gots := donburi.GetComponents(entryA) + wants := []interface{}{trData, veData, struct{}{}} + + if len(gots) != len(wants) { + t.Fatalf("got: %v, want: %v", gots, wants) + } + + for i, got := range gots { + if got != wants[i] { + t.Errorf("got: %v, want: %v", got, wants[i]) + } + } +} diff --git a/internal/storage/mock.go b/internal/storage/mock.go index 6d54547..a03b975 100644 --- a/internal/storage/mock.go +++ b/internal/storage/mock.go @@ -50,3 +50,7 @@ func (m *MockComponentType[T]) setDefaultVal(ptr unsafe.Pointer) { func (m *MockComponentType[T]) Name() string { return fmt.Sprintf("%s[%s]", reflect.TypeOf(m).Name(), m.typ.Name()) } + +func (m *MockComponentType[T]) Typ() reflect.Type { + return m.typ +}