-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b063e12
commit 915be5f
Showing
4 changed files
with
53 additions
and
33 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 @@ | ||
package msg |
This file was deleted.
Oops, something went wrong.
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,49 @@ | ||
package system | ||
|
||
import ( | ||
"fmt" | ||
"pkg.world.dev/world-engine/cardinal" | ||
"pkg.world.dev/world-engine/cardinal/search/filter" | ||
"pkg.world.dev/world-engine/cardinal/types" | ||
comp "shantanu-starter-game/component" | ||
"shantanu-starter-game/msg" | ||
) | ||
|
||
// MovementSystem updates the player's location based on its velocity and direction. | ||
func MovementSystem(world cardinal.WorldContext) error { | ||
return cardinal.NewSearch().Entity( | ||
filter.Exact(filter.Component[comp.Player](), filter.Component[comp.Movement]())). | ||
Each(world, func(id types.EntityID) bool { | ||
movement, err := cardinal.GetComponent[comp.Movement](world, id) | ||
if err != nil { | ||
return true | ||
} | ||
|
||
// TODO: How to get delta time? | ||
movement.CurrentLocation.X += movement.Velocity * float32(movement.CurrentDirection.X) | ||
movement.CurrentLocation.Y += movement.Velocity * float32(movement.CurrentDirection.Y) | ||
|
||
if err := cardinal.SetComponent[comp.Movement](world, id, movement); err != nil { | ||
return true | ||
} | ||
return true | ||
}) | ||
} | ||
|
||
func MovementValidationSystem(world cardinal.WorldContext) error { | ||
return cardinal.EachMessage[msg.AttackPlayerMsg, msg.AttackPlayerMsgReply]( | ||
world, | ||
func(attack cardinal.TxData[msg.AttackPlayerMsg]) (msg.AttackPlayerMsgReply, error) { | ||
playerID, playerHealth, err := queryTargetPlayer(world, attack.Msg.TargetNickname) | ||
if err != nil { | ||
return msg.AttackPlayerMsgReply{}, fmt.Errorf("failed to inflict damage: %w", err) | ||
} | ||
|
||
playerHealth.HP -= AttackDamage | ||
if err := cardinal.SetComponent[comp.Health](world, playerID, playerHealth); err != nil { | ||
return msg.AttackPlayerMsgReply{}, fmt.Errorf("failed to inflict damage: %w", err) | ||
} | ||
|
||
return msg.AttackPlayerMsgReply{Damage: AttackDamage}, nil | ||
}) | ||
} |