-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed incorrect work of the views and more tests added
- Loading branch information
Showing
6 changed files
with
205 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/sedyh/mizu/pkg/engine" | ||
"github.com/sedyh/mizu/test/helper" | ||
) | ||
|
||
type Position struct { | ||
X, Y int | ||
} | ||
|
||
type Velocity struct { | ||
X, Y int | ||
} | ||
|
||
type Gravity struct { | ||
Value int | ||
} | ||
|
||
type First struct { | ||
Value bool | ||
} | ||
|
||
type Ball struct { | ||
First | ||
Position | ||
Velocity | ||
Gravity | ||
} | ||
|
||
type Movement struct { | ||
*Position | ||
*Velocity | ||
} | ||
|
||
func (m *Movement) Update(_ engine.World) { | ||
m.Position.X += m.Velocity.X | ||
m.Position.Y += m.Velocity.Y | ||
} | ||
|
||
type Falling struct { | ||
*Velocity | ||
*Gravity | ||
} | ||
|
||
func (f *Falling) Update(_ engine.World) { | ||
f.Velocity.Y += f.Gravity.Value | ||
} | ||
|
||
var _ = Describe("Game world", func() { | ||
It("Should be able to create a system that traverses each entity", func() { | ||
var world engine.World | ||
|
||
// Run the world with two systems in two entities | ||
helper.RunSingleSceneGame(func(w engine.World) { | ||
w.AddComponents(First{}, Position{}, Velocity{}, Gravity{}) | ||
w.AddEntities( | ||
&Ball{First{true}, Position{1, 5}, Velocity{-3, 4}, Gravity{2}}, | ||
&Ball{First{}, Position{-5, 3}, Velocity{1, 8}, Gravity{3}}, | ||
) | ||
w.AddSystems(&Movement{}, &Falling{}) | ||
world = w | ||
}) | ||
|
||
// Check that each system done it work right | ||
world.View(First{}, Position{}, Velocity{}).Each(func(e engine.Entity) { | ||
var first *First | ||
var position *Position | ||
var velocity *Velocity | ||
e.Get(&first, &position, &velocity) | ||
if first.Value { | ||
Expect(*position).To(Equal(Position{-2, 9})) | ||
Expect(*velocity).To(Equal(Velocity{-3, 6})) | ||
} | ||
}) | ||
world.View(First{}, Position{}, Velocity{}).Each(func(e engine.Entity) { | ||
var first *First | ||
var position *Position | ||
var velocity *Velocity | ||
e.Get(&first, &position, &velocity) | ||
if !first.Value { | ||
Expect(*position).To(Equal(Position{-4, 11})) | ||
Expect(*velocity).To(Equal(Velocity{1, 11})) | ||
} | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/sedyh/mizu/pkg/engine" | ||
"github.com/sedyh/mizu/test/helper" | ||
) | ||
|
||
var _ = Describe("Game world", func() { | ||
It("Should be able to create the correct View", func() { | ||
type ComponentA struct { | ||
Value int | ||
} | ||
type ComponentB struct { | ||
Value int | ||
} | ||
type ComponentC struct { | ||
Value int | ||
} | ||
type ComponentD struct { | ||
Value int | ||
} | ||
type EntityA struct { | ||
ComponentA | ||
ComponentB | ||
ComponentC | ||
} | ||
type EntityB struct { | ||
ComponentD | ||
} | ||
i := 0 | ||
helper.RunSingleSceneGame(func(w engine.World) { | ||
w.AddComponents(ComponentA{}, ComponentB{}, ComponentC{}, ComponentD{}) | ||
w.AddEntities(&EntityA{ComponentA{}, ComponentB{}, ComponentC{}}, &EntityB{ComponentD{}}) | ||
for j := 0; j < 100; j++ { | ||
// Each view should fire only one time per iteration | ||
for _, e := range w.View(ComponentA{}).Filter() { | ||
var a *ComponentA | ||
e.Get(&a) | ||
i++ | ||
} | ||
for _, e := range w.View(ComponentA{}, ComponentB{}).Filter() { | ||
var a *ComponentA | ||
var b *ComponentB | ||
e.Get(&a, &b) | ||
i++ | ||
} | ||
// The order of the components is not important | ||
for _, e := range w.View(ComponentB{}, ComponentC{}).Filter() { | ||
var b *ComponentB | ||
var c *ComponentC | ||
e.Get(&b, &c) | ||
i++ | ||
} | ||
for _, e := range w.View(ComponentC{}, ComponentB{}).Filter() { | ||
var c *ComponentC | ||
var b *ComponentB | ||
e.Get(&c, &b) | ||
i++ | ||
} | ||
for _, e := range w.View(ComponentA{}, ComponentB{}, ComponentB{}).Filter() { | ||
var a *ComponentA | ||
var b *ComponentB | ||
var c *ComponentC | ||
e.Get(&c, &b, &a) | ||
i++ | ||
} | ||
} | ||
}) | ||
Expect(i).To(Equal(500)) | ||
}) | ||
}) |