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

Movement validations flow setup #5

Merged
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
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 @@ -12,66 +12,60 @@

// 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 {
return cardinal.EachMessage[msg.MovementPlayerMsg, msg.MovementPlayerMsgReply](
world,
func(movement cardinal.TxData[msg.MovementPlayerMsg]) (msg.MovementPlayerMsgReply, error) {
fmt.Println("Hello from movement validation system", movement)

Check failure on line 40 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 40 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 40 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)
playerID, playerMovementData, err := queryTargetPlayerMovementData(world, movement.Msg.TargetNickname)
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 @@ -50,19 +50,18 @@
return playerID, playerHealth, err
}

func queryTargetPlayerMovementData(world cardinal.WorldContext, targetNickname string) (types.EntityID, *comp.Movement, error) {

Check failure on line 53 in cardinal/system/utils.go

View workflow job for this annotation

GitHub Actions / Go

line is 128 characters (lll)

Check failure on line 53 in cardinal/system/utils.go

View workflow job for this annotation

GitHub Actions / Go

line is 128 characters (lll)

Check failure on line 53 in cardinal/system/utils.go

View workflow job for this annotation

GitHub Actions / Go

line is 128 characters (lll)
var playerID types.EntityID
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 @@
// 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
Loading