Skip to content

Commit

Permalink
upgrade to new version of cardinal.
Browse files Browse the repository at this point in the history
  • Loading branch information
pyrofolium committed May 17, 2024
1 parent 20306e2 commit f08e0fe
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 26 deletions.
2 changes: 1 addition & 1 deletion cardinal/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.22.1

require (
github.com/rs/zerolog v1.32.0
pkg.world.dev/world-engine/cardinal v1.2.6-beta
pkg.world.dev/world-engine/cardinal v1.3.2
)

require (
Expand Down
2 changes: 2 additions & 0 deletions cardinal/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,8 @@ pkg.world.dev/world-engine/assert v1.0.0 h1:vD6+QLT1pvQa86FPFi+wGcVA7PEwTGndXr+P
pkg.world.dev/world-engine/assert v1.0.0/go.mod h1:bwA9YZ40+Tte6GUKibfqByxBLLt+54zjjFako8cpSuU=
pkg.world.dev/world-engine/cardinal v1.2.6-beta h1:nbgfqpvq0hcni5dzpCTDwCEH3sO6p/J+yqfxhfKwtzU=
pkg.world.dev/world-engine/cardinal v1.2.6-beta/go.mod h1:G5XveSFC1FNzybm+P6ONqhAHJLwQxLbp5vRcMGWuER4=
pkg.world.dev/world-engine/cardinal v1.3.2 h1:pzS33m8NwxMblyDoXnE6ChFkh02w0sof2NRzoX/dHsc=
pkg.world.dev/world-engine/cardinal v1.3.2/go.mod h1:G5XveSFC1FNzybm+P6ONqhAHJLwQxLbp5vRcMGWuER4=
pkg.world.dev/world-engine/rift v1.1.0-beta.0.20240402214846-de1fc179818a h1:P9pf6itjPBlooxj8p0jTkIY7ozcjSgCYsuh/BkbL56o=
pkg.world.dev/world-engine/rift v1.1.0-beta.0.20240402214846-de1fc179818a/go.mod h1:XAD40g4r3qOp3CTa66JBY+ACEUtjr01loyHiUZIheN8=
pkg.world.dev/world-engine/sign v1.0.1-beta h1:ZwVeJYdf88t6qIHPurbdKJKVxUN0dfp0gSF7OCA9xt8=
Expand Down
31 changes: 16 additions & 15 deletions cardinal/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,22 @@ func TestInitSystem_SpawnDefaultPlayersSystem_DefaultPlayersAreSpawned(t *testin
wCtx := cardinal.NewReadOnlyWorldContext(tf.World)

foundPlayers := map[string]bool{}
searchErr := cardinal.NewSearch(wCtx, filter.Contains(component.Health{})).Each(func(id types.EntityID) bool {
player, err := cardinal.GetComponent[component.Player](wCtx, id)
if err != nil {
t.Fatalf("failed to get player: %v", err)
}
health, err := cardinal.GetComponent[component.Health](wCtx, id)
if err != nil {
t.Fatalf("failed to get health: %v", err)
}
if health.HP < 100 {
t.Fatalf("new player should have at least 100 health; got %v", health.HP)
}
foundPlayers[player.Nickname] = true
return true
})
searchErr := cardinal.NewSearch().Entity(filter.Contains(filter.Component[component.Health]())).
Each(wCtx, func(id types.EntityID) bool {
player, err := cardinal.GetComponent[component.Player](wCtx, id)
if err != nil {
t.Fatalf("failed to get player: %v", err)
}
health, err := cardinal.GetComponent[component.Health](wCtx, id)
if err != nil {
t.Fatalf("failed to get health: %v", err)
}
if health.HP < 100 {
t.Fatalf("new player should have at least 100 health; got %v", health.HP)
}
foundPlayers[player.Nickname] = true
return true
})
if searchErr != nil {
t.Fatalf("failed to perform search: %v", searchErr)
}
Expand Down
7 changes: 3 additions & 4 deletions cardinal/query/player_health.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ type PlayerHealthResponse struct {
func PlayerHealth(world cardinal.WorldContext, req *PlayerHealthRequest) (*PlayerHealthResponse, error) {
var playerHealth *comp.Health
var err error
searchErr := cardinal.NewSearch(
world,
filter.Exact(comp.Player{}, comp.Health{})).
Each(func(id types.EntityID) bool {
searchErr := cardinal.NewSearch().Entity(
filter.Exact(filter.Component[comp.Player](), filter.Component[comp.Health]())).
Each(world, func(id types.EntityID) bool {
var player *comp.Player
player, err = cardinal.GetComponent[comp.Player](world, id)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cardinal/system/regen.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// RegenSystem replenishes the player's HP at every tick.
// This provides an example of a system that doesn't rely on a transaction to update a component.
func RegenSystem(world cardinal.WorldContext) error {
return cardinal.NewSearch(world, filter.Exact(comp.Player{}, comp.Health{})).Each(func(id types.EntityID) bool {
return cardinal.NewSearch().Entity(filter.Exact(filter.Component[comp.Player](), filter.Component[comp.Health]())).Each(world, func(id types.EntityID) bool {

Check failure on line 14 in cardinal/system/regen.go

View workflow job for this annotation

GitHub Actions / Go

line is 158 characters (lll)
health, err := cardinal.GetComponent[comp.Health](world, id)
if err != nil {
return true
Expand Down
3 changes: 2 additions & 1 deletion cardinal/system/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ func queryTargetPlayer(world cardinal.WorldContext, targetNickname string) (type
var playerID types.EntityID
var playerHealth *comp.Health
var err error
searchErr := cardinal.NewSearch(world, filter.Exact(comp.Player{}, comp.Health{})).Each(
searchErr := cardinal.NewSearch().Entity(
filter.Exact(filter.Component[comp.Player](), filter.Component[comp.Health]())).Each(world,
func(id types.EntityID) bool {
var player *comp.Player
player, err = cardinal.GetComponent[comp.Player](world, id)
Expand Down
18 changes: 14 additions & 4 deletions cardinal/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"testing"

"gotest.tools/v3/assert"

"pkg.world.dev/world-engine/cardinal"
"pkg.world.dev/world-engine/cardinal/receipt"
"pkg.world.dev/world-engine/cardinal/search/filter"
Expand Down Expand Up @@ -58,13 +60,21 @@ func TestSystem_PlayerSpawnerSystem_CanCreatePlayer(t *testing.T) {
wCtx := cardinal.NewReadOnlyWorldContext(tf.World)
// This search demonstrates the use of a "Where" clause, which limits the search results to only the entity IDs
// that end up returning true from the anonymous function. In this case, we're looking for a specific nickname.
id := cardinal.NewSearch(wCtx, filter.All()).Where(func(id types.EntityID) bool {
acc := make([]types.EntityID, 0)
err := cardinal.NewSearch().Entity(filter.All()).Each(wCtx, func(id types.EntityID) bool {
player, err := cardinal.GetComponent[component.Player](wCtx, id)
if err != nil {
t.Fatalf("failed to get player component: %v", err)
}
return player.Nickname == nickname
}).MustFirst()
if player.Nickname == nickname {
acc = append(acc, id)
return false
}
return true
})
assert.NilError(t, err)
assert.Equal(t, len(acc), 1)
id := acc[0]

health, err := cardinal.GetComponent[component.Health](wCtx, id)
if err != nil {
Expand Down Expand Up @@ -106,7 +116,7 @@ func TestSystem_AttackSystem_AttackingTargetReducesTheirHealth(t *testing.T) {
var found bool
// This search demonstrates the "Each" pattern. Every entity ID is considered, and as long as the anonymous
// function return true, the search will continue.
searchErr := cardinal.NewSearch(wCtx, filter.All()).Each(func(id types.EntityID) bool {
searchErr := cardinal.NewSearch().Entity(filter.All()).Each(wCtx, func(id types.EntityID) bool {
player, err := cardinal.GetComponent[component.Player](wCtx, id)
if err != nil {
t.Fatalf("failed to get player component for %v", id)
Expand Down

0 comments on commit f08e0fe

Please sign in to comment.