Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sjamble/feature-query-msg #2

Open
wants to merge 1 commit into
base: sjamble/feature-movement-system
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
})
}
Loading