-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
See Argus-Labs/world-engine@9e257f5 from refs/heads/main
- Loading branch information
Argus-Labs
committed
Feb 23, 2024
1 parent
3561273
commit cc5a530
Showing
4 changed files
with
54 additions
and
43 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
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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.