Skip to content

Commit

Permalink
See Argus-Labs/world-engine@9e257f5 from refs/heads/main
Browse files Browse the repository at this point in the history
  • Loading branch information
Argus-Labs committed Feb 23, 2024
1 parent 3561273 commit cc5a530
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 43 deletions.
2 changes: 1 addition & 1 deletion cardinal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func main() {
// Register queries
// NOTE: You must register your queries here for it to be accessible.
Must(
cardinal.RegisterQuery[query.WorldVarsRequest, query.WorldVarsResponse](w, "world-vars", query.WorldVars),
cardinal.RegisterQuery[query.PlayerHealthRequest, query.PlayerHealthResponse](w, "player-health", query.PlayerHealth),
)

// Each system executes deterministically in the order they are added.
Expand Down
53 changes: 53 additions & 0 deletions cardinal/query/query_player_health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package query

import (
"fmt"

comp "github.com/argus-labs/starter-game-template/cardinal/component"

"pkg.world.dev/world-engine/cardinal"
)

type PlayerHealthRequest struct {
Nickname string
}

type PlayerHealthResponse struct {
HP int
}

func PlayerHealth(world cardinal.WorldContext, req *PlayerHealthRequest) (*PlayerHealthResponse, error) {
search, err := world.NewSearch(cardinal.Exact(comp.Player{}, comp.Health{}))
if err != nil {
return nil, err
}

var playerHealth *comp.Health
err = search.Each(world, func(id cardinal.EntityID) bool {
player, err := cardinal.GetComponent[comp.Player](world, id)
if err != nil {
return false
}

// Terminates the search if the player is found
if player.Nickname == req.Nickname {
playerHealth, err = cardinal.GetComponent[comp.Health](world, id)
if err != nil {
return false
}
return false
}

// Continue searching if the player is not the target player
return true
})
if err != nil {
return nil, err
}

if playerHealth == nil {
return nil, fmt.Errorf("player %s does not exist", req.Nickname)
}

return &PlayerHealthResponse{HP: playerHealth.HP}, nil
}
30 changes: 0 additions & 30 deletions cardinal/query/query_world_vars.go

This file was deleted.

12 changes: 0 additions & 12 deletions cardinal/world/vars.go

This file was deleted.

0 comments on commit cc5a530

Please sign in to comment.