-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c3c531
commit b063e12
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} |