Entity Component System framework for Ebiten
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.1 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.Entity
}
func NewMovementInputSystem(player gohan.Entity) *movementInputSystem {
return &movementInputSystem{
player: player,
}
}
func (s *movementInputSystem) Matches(e gohan.Entity) bool {
return e == s.player
}
func (s *movementInputSystem) Update(e gohan.Entity) error {
velocity := component.Velocity(s.player)
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.Entity, _ *ebiten.Image) error {
return gohan.ErrSystemWithoutDraw
}