You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.2 KiB
Go
61 lines
1.2 KiB
Go
//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) Components() []gohan.ComponentID {
|
|
return []gohan.ComponentID{
|
|
component.VelocityComponentID,
|
|
component.WeaponComponentID,
|
|
}
|
|
}
|
|
|
|
func (s *movementInputSystem) Update(ctx *gohan.Context) error {
|
|
velocity := component.Velocity(ctx)
|
|
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(ctx *gohan.Context, _ *ebiten.Image) error {
|
|
return gohan.ErrSystemWithoutDraw
|
|
}
|