Skip to content

Commit

Permalink
sjamble/feature-query-msg
Browse files Browse the repository at this point in the history
  • Loading branch information
shantanu-argus committed Nov 12, 2024
1 parent b063e12 commit 915be5f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 33 deletions.
6 changes: 3 additions & 3 deletions cardinal/component/movement.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ type Location struct {
}

type Movement struct {
Direction Direction
Velocity float32
Location Location
CurrentDirection Direction
Velocity float32
CurrentLocation Location
}

func (Movement) Name() string {
Expand Down
1 change: 1 addition & 0 deletions cardinal/msg/movement_player.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package msg
30 changes: 0 additions & 30 deletions cardinal/system/MovementSystem.go

This file was deleted.

49 changes: 49 additions & 0 deletions cardinal/system/movement_system.go
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
})
}

0 comments on commit 915be5f

Please sign in to comment.