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 #4

Draft
wants to merge 2 commits into
base: 11-11-added_systems_and_msgs_for_movement_component
Choose a base branch
from
Draft
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
9 changes: 9 additions & 0 deletions cardinal/system/default_spawner.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@
func SpawnDefaultPlayersSystem(world cardinal.WorldContext) error {
for i := 0; i < 10; i++ {
name := fmt.Sprintf("default-%d", i)
fmt.Println("Creating entity for ", name)

Check failure on line 16 in cardinal/system/default_spawner.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 16 in cardinal/system/default_spawner.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 16 in cardinal/system/default_spawner.go

View workflow job for this annotation

GitHub Actions / Go

use of `fmt.Println` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)
_, err := cardinal.Create(world,
comp.Player{Nickname: name},
comp.Health{HP: InitialHP},
comp.Movement{
CurrentLocation: comp.Location{X: InitialXLocation, Y: InitialYLocation},
Velocity: InitialVelocity,
CurrentDirection: comp.Direction{
comp.Directions[InitialDirection].X,
comp.Directions[InitialDirection].Y,
},
},
)
if err != nil {
return err
Expand Down
47 changes: 33 additions & 14 deletions cardinal/system/movement_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@

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

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
}

// TODO: How to get delta time?
movement.CurrentLocation.X += movement.Velocity * float64(movement.CurrentDirection.X)
movement.CurrentLocation.Y += movement.Velocity * float64(movement.CurrentDirection.Y)
// 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
Expand All @@ -35,21 +37,38 @@
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.001
const errorTolerance float64 = 5
if math.Abs(playerMovementData.CurrentLocation.X-movement.Msg.LocationX) > errorTolerance ||
math.Abs(playerMovementData.CurrentLocation.Y-movement.Msg.LocationY) > errorTolerance {
return msg.MovementPlayerMsgReply{IsValid: false, LocationX: playerMovementData.CurrentLocation.X, LocationY: playerMovementData.CurrentLocation.Y}, nil
} else {
playerMovementData.CurrentLocation.X = movement.Msg.LocationX
playerMovementData.CurrentLocation.Y = movement.Msg.LocationY
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)
}
return msg.MovementPlayerMsgReply{IsValid: true, LocationX: playerMovementData.CurrentLocation.X, LocationY: playerMovementData.CurrentLocation.Y}, nil
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},
nil
})
}
2 changes: 2 additions & 0 deletions cardinal/system/regen.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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"
Expand All @@ -18,6 +19,7 @@
if err != nil {
return true
}
fmt.Println(id)

Check failure on line 22 in cardinal/system/regen.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/regen.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/regen.go

View workflow job for this annotation

GitHub Actions / Go

use of `fmt.Println` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)
health.HP++
if err := cardinal.SetComponent[comp.Health](world, id, health); err != nil {
return true
Expand Down
3 changes: 1 addition & 2 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](), filter.Component[comp.Movement]())).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
}

// Terminates the search if the player is found
if player.Nickname == targetNickname {
playerID = id
Expand Down
Loading