Skip to content

Commit

Permalink
Movement validations flow setup
Browse files Browse the repository at this point in the history
  • Loading branch information
shantanu-argus committed Nov 15, 2024
1 parent 7aab0b3 commit a0fa937
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 26 deletions.
2 changes: 1 addition & 1 deletion cardinal/msg/movement_player.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
38 changes: 16 additions & 22 deletions cardinal/system/movement_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Check failure on line 15 in cardinal/system/movement_system.go

View workflow job for this annotation

GitHub Actions / Go

use of `fmt.Println` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)

Check failure on line 15 in cardinal/system/movement_system.go

View workflow job for this annotation

GitHub Actions / Go

use of `fmt.Println` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)

Check failure on line 15 in cardinal/system/movement_system.go

View workflow job for this annotation

GitHub Actions / Go

use of `fmt.Println` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)

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")

Check failure on line 22 in cardinal/system/movement_system.go

View workflow job for this annotation

GitHub Actions / Go

use of `fmt.Println` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)

Check failure on line 22 in cardinal/system/movement_system.go

View workflow job for this annotation

GitHub Actions / Go

use of `fmt.Println` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)

Check failure on line 22 in cardinal/system/movement_system.go

View workflow job for this annotation

GitHub Actions / Go

use of `fmt.Println` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)
return true
}
fmt.Println(movement)
// Simulate the motion of the player
movement.CurrentLocation.X += movement.Velocity * float64(movement.CurrentDirection.X) * 0.2

Check failure on line 26 in cardinal/system/movement_system.go

View workflow job for this annotation

GitHub Actions / Go

mnd: Magic number: 0.2, in <operation> detected (gomnd)

Check failure on line 26 in cardinal/system/movement_system.go

View workflow job for this annotation

GitHub Actions / Go

mnd: Magic number: 0.2, in <operation> detected (gomnd)

Check failure on line 26 in cardinal/system/movement_system.go

View workflow job for this annotation

GitHub Actions / Go

mnd: Magic number: 0.2, in <operation> detected (gomnd)
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 {
Expand All @@ -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"))

Check failure on line 48 in cardinal/system/movement_system.go

View workflow job for this annotation

GitHub Actions / Go

use of `fmt.Println` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)

Check failure on line 48 in cardinal/system/movement_system.go

View workflow job for this annotation

GitHub Actions / Go

use of `fmt.Println` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)

Check failure on line 48 in cardinal/system/movement_system.go

View workflow job for this annotation

GitHub Actions / Go

use of `fmt.Println` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)
// 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)

Check failure on line 61 in cardinal/system/movement_system.go

View workflow job for this annotation

GitHub Actions / Go

use of `fmt.Println` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)

Check failure on line 61 in cardinal/system/movement_system.go

View workflow job for this annotation

GitHub Actions / Go

use of `fmt.Println` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)

Check failure on line 61 in cardinal/system/movement_system.go

View workflow job for this annotation

GitHub Actions / Go

use of `fmt.Println` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)
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)

Check failure on line 68 in cardinal/system/movement_system.go

View workflow job for this annotation

GitHub Actions / Go

use of `fmt.Println` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)

Check failure on line 68 in cardinal/system/movement_system.go

View workflow job for this annotation

GitHub Actions / Go

use of `fmt.Println` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)

Check failure on line 68 in cardinal/system/movement_system.go

View workflow job for this annotation

GitHub Actions / Go

use of `fmt.Println` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)
return msg.MovementPlayerMsgReply{IsValid: true,
LocationX: playerMovementData.CurrentLocation.X,
LocationY: playerMovementData.CurrentLocation.Y},
Expand Down
4 changes: 1 addition & 3 deletions cardinal/system/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit a0fa937

Please sign in to comment.