Entity Component System framework for Ebitengine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.2 KiB

//go:build example
// +build example
package system
import (
"code.rocketnine.space/tslocum/gohan"
"code.rocketnine.space/tslocum/gohan/_examples/twinstick/component"
"github.com/hajimehoshi/ebiten/v2"
)
type movementInputSystem struct {
player gohan.EntityID
}
func NewMovementInputSystem(player gohan.EntityID) *movementInputSystem {
return &movementInputSystem{
player: player,
}
}
func (s *movementInputSystem) Matches(e gohan.EntityID) bool {
return e == s.player
}
func (s *movementInputSystem) Update(e gohan.EntityID) error {
velocity := s.player.Component(component.VelocityComponentID).(*component.VelocityComponent)
if ebiten.IsKeyPressed(ebiten.KeyA) {
velocity.X -= 0.5
if velocity.X < -5 {
velocity.X = -5
}
}
if ebiten.IsKeyPressed(ebiten.KeyD) {
velocity.X += 0.5
if velocity.X > 5 {
velocity.X = 5
}
}
if ebiten.IsKeyPressed(ebiten.KeyW) {
velocity.Y -= 0.5
if velocity.Y < -5 {
velocity.Y = -5
}
}
if ebiten.IsKeyPressed(ebiten.KeyS) {
velocity.Y += 0.5
if velocity.Y > 5 {
velocity.Y = 5
}
}
return nil
}
func (s *movementInputSystem) Draw(_ gohan.EntityID, _ *ebiten.Image) error {
return gohan.ErrSystemWithoutDraw
}