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

Add movement component and system #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
28 changes: 28 additions & 0 deletions cardinal/component/movement.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package component

type Direction struct {
X int
Y int
}

var Directions = map[int]Direction{
0: {1, 0}, // right
1: {-1, 0}, // left
2: {0, -1}, // up
3: {0, 1}, // down
}

type Location struct {
X float32
Y float32
}

type Movement struct {
Direction Direction
Velocity float32
Location Location
}

func (Movement) Name() string {
return "Movement"
}
9 changes: 9 additions & 0 deletions cardinal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ import (
"shantanu-starter-game/system"
)

/*
TODO:
1. setup a new component and system for movement.
2. should take in direction and have an onTick flow to lerp from current position to new position.
3. whenever you get a new position, you should validate if its close enought to lerped position.
4. if its close enough, you should update to that position continue lerping.
5. if its not close enough, you should throw a an error. Client should handle this error and try again.
*/

func main() {
w, err := cardinal.NewWorld(cardinal.WithDisableSignatureVerification())
if err != nil {
Expand Down
30 changes: 30 additions & 0 deletions cardinal/system/MovementSystem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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
})
}
Loading