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

Add GetValue() function #129

Merged
merged 2 commits into from
Mar 23, 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
8 changes: 6 additions & 2 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
61 changes: 61 additions & 0 deletions entry_test.go
Original file line number Diff line number Diff line change
@@ -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])
}
}
}
4 changes: 4 additions & 0 deletions internal/storage/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Loading