diff --git a/cardinal/msg/movement_player.go b/cardinal/msg/movement_player.go index a73aaee..c946228 100644 --- a/cardinal/msg/movement_player.go +++ b/cardinal/msg/movement_player.go @@ -2,7 +2,7 @@ package msg type MovementPlayerMsg struct { TargetNickname string `json:"target"` - Velocity int `json:"velocity"` + Velocity float64 `json:"velocity"` Direction int `json:"direction"` LocationX float64 `json:"locationX"` LocationY float64 `json:"locationY"` diff --git a/cardinal/system/movement_system.go b/cardinal/system/movement_system.go index ed66350..7227dae 100644 --- a/cardinal/system/movement_system.go +++ b/cardinal/system/movement_system.go @@ -13,38 +13,24 @@ import ( // MovementSystem updates the player's location based on its velocity and direction. func MovementSystem(world cardinal.WorldContext) error { fmt.Println("Hello from movement system") + return cardinal.NewSearch().Entity( - filter.Exact(filter.Component[comp.Player](), filter.Component[comp.Movement]())). + filter.Contains(filter.Component[comp.Movement]())). Each(world, func(id types.EntityID) bool { movement, err := cardinal.GetComponent[comp.Movement](world, id) if err != nil { - fmt.Println("Error in movement system", err) + fmt.Println("Error in movement system") return true } - fmt.Println(movement) + // Simulate the motion of the player + movement.CurrentLocation.X += movement.Velocity * float64(movement.CurrentDirection.X) * 0.2 + movement.CurrentLocation.Y += movement.Velocity * float64(movement.CurrentDirection.Y) * 0.2 + if err := cardinal.SetComponent[comp.Movement](world, id, movement); err != nil { return true } return true }) - //return cardinal.NewSearch().Entity( - // filter.Exact(filter.Component[comp.Movement]())). - // Each(world, func(id types.EntityID) bool { - // fmt.Println("Hello from movement system") - // movement, err := cardinal.GetComponent[comp.Movement](world, id) - // if err != nil { - // fmt.Println("Error in movement system") - // return true - // } - // velocity := 5.0 - // movement.CurrentLocation.X += velocity * float64(movement.CurrentDirection.X) * 0.2 - // movement.CurrentLocation.Y += velocity * float64(movement.CurrentDirection.Y) * 0.2 - // - // if err := cardinal.SetComponent[comp.Movement](world, id, movement); err != nil { - // return true - // } - // return true - // }) } func MovementValidationSystem(world cardinal.WorldContext) error { @@ -56,22 +42,30 @@ func MovementValidationSystem(world cardinal.WorldContext) error { if err != nil { return msg.MovementPlayerMsgReply{}, fmt.Errorf("failed to update movement: %w", err) } - const errorTolerance float64 = 0.2 + const errorTolerance float64 = 5 if math.Abs(playerMovementData.CurrentLocation.X-movement.Msg.LocationX) > errorTolerance || math.Abs(playerMovementData.CurrentLocation.Y-movement.Msg.LocationY) > errorTolerance { + fmt.Println(fmt.Errorf("player moved too far from server location")) + // force player to reset to servers location return msg.MovementPlayerMsgReply{IsValid: false, LocationX: playerMovementData.CurrentLocation.X, LocationY: playerMovementData.CurrentLocation.Y}, nil } + + // update the movement if its error margin is within the tolerance + playerMovementData.Velocity = movement.Msg.Velocity + playerMovementData.CurrentDirection = comp.Directions[movement.Msg.Direction] playerMovementData.CurrentLocation.X = movement.Msg.LocationX playerMovementData.CurrentLocation.Y = movement.Msg.LocationY + fmt.Println(playerMovementData) if err := cardinal.SetComponent[comp.Movement](world, playerID, playerMovementData); err != nil { return msg.MovementPlayerMsgReply{IsValid: false, LocationX: playerMovementData.CurrentLocation.X, LocationY: playerMovementData.CurrentLocation.Y}, fmt.Errorf("failed to update movement: %w", err) } + fmt.Println("Updated player movement ", err) return msg.MovementPlayerMsgReply{IsValid: true, LocationX: playerMovementData.CurrentLocation.X, LocationY: playerMovementData.CurrentLocation.Y}, diff --git a/cardinal/system/utils.go b/cardinal/system/utils.go index 30d4dda..ce2dd45 100644 --- a/cardinal/system/utils.go +++ b/cardinal/system/utils.go @@ -55,14 +55,13 @@ func queryTargetPlayerMovementData(world cardinal.WorldContext, targetNickname s var playerMovementComponent *comp.Movement var err error searchErr := cardinal.NewSearch().Entity( - filter.Exact(filter.Component[comp.Player]())).Each(world, + filter.Contains(filter.Component[comp.Movement]())).Each(world, func(id types.EntityID) bool { var player *comp.Player player, err = cardinal.GetComponent[comp.Player](world, id) if err != nil { return false } - fmt.Println(player) // Terminates the search if the player is found if player.Nickname == targetNickname { playerID = id @@ -76,7 +75,6 @@ func queryTargetPlayerMovementData(world cardinal.WorldContext, targetNickname s // Continue searching if the player is not the target player return true }) - fmt.Println("finding ", targetNickname) if searchErr != nil { return 0, nil, err }