From 915be5f136ee61bfb8f7ff06d2f206f9a7290856 Mon Sep 17 00:00:00 2001 From: Shantanu Jamble Date: Mon, 11 Nov 2024 16:35:57 -0800 Subject: [PATCH] sjamble/feature-query-msg --- cardinal/component/movement.go | 6 ++-- cardinal/msg/movement_player.go | 1 + cardinal/system/MovementSystem.go | 30 ------------------ cardinal/system/movement_system.go | 49 ++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 33 deletions(-) create mode 100644 cardinal/msg/movement_player.go delete mode 100644 cardinal/system/MovementSystem.go create mode 100644 cardinal/system/movement_system.go diff --git a/cardinal/component/movement.go b/cardinal/component/movement.go index 28205a2..2a62415 100644 --- a/cardinal/component/movement.go +++ b/cardinal/component/movement.go @@ -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 { diff --git a/cardinal/msg/movement_player.go b/cardinal/msg/movement_player.go new file mode 100644 index 0000000..6cb5870 --- /dev/null +++ b/cardinal/msg/movement_player.go @@ -0,0 +1 @@ +package msg diff --git a/cardinal/system/MovementSystem.go b/cardinal/system/MovementSystem.go deleted file mode 100644 index 51d0a87..0000000 --- a/cardinal/system/MovementSystem.go +++ /dev/null @@ -1,30 +0,0 @@ -package system - -import ( - "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" -) - -// RegenSystem replenishes the player's HP at every tick. -// This provides an example of a system that doesn't rely on a transaction to update a component. -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.Location.X += movement.Velocity * float32(movement.Direction.X) - movement.Location.Y += movement.Velocity * float32(movement.Direction.Y) - - if err := cardinal.SetComponent[comp.Movement](world, id, movement); err != nil { - return true - } - return true - }) -} diff --git a/cardinal/system/movement_system.go b/cardinal/system/movement_system.go new file mode 100644 index 0000000..a1d6e6a --- /dev/null +++ b/cardinal/system/movement_system.go @@ -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 + }) +}