Skip to content

Commit

Permalink
Add GetValue() function (#129)
Browse files Browse the repository at this point in the history
* fix test

* add GetValue function
  • Loading branch information
yohamta authored Mar 23, 2024
1 parent 34c2e5d commit eaf13a5
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
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
}

0 comments on commit eaf13a5

Please sign in to comment.