66 lines
1.4 KiB
Go
66 lines
1.4 KiB
Go
package system
|
|
|
|
import (
|
|
"code.rocketnine.space/tslocum/commandeuropa/component"
|
|
"code.rocketnine.space/tslocum/commandeuropa/world"
|
|
"code.rocketnine.space/tslocum/gohan"
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
)
|
|
|
|
type HandleInput struct {
|
|
Once *component.Once
|
|
}
|
|
|
|
func (r *HandleInput) Update(e gohan.Entity) error {
|
|
const panDistance = 3
|
|
|
|
// Pan with keyboard.
|
|
if ebiten.IsKeyPressed(ebiten.KeyLeft) {
|
|
world.CamX -= panDistance
|
|
}
|
|
if ebiten.IsKeyPressed(ebiten.KeyRight) {
|
|
world.CamX += panDistance
|
|
}
|
|
if ebiten.IsKeyPressed(ebiten.KeyUp) {
|
|
world.CamY -= panDistance
|
|
}
|
|
if ebiten.IsKeyPressed(ebiten.KeyDown) {
|
|
world.CamY += panDistance
|
|
}
|
|
|
|
// Zoom with mouse.
|
|
const minZoom = 1
|
|
const maxZoom = 7
|
|
_, scroll := ebiten.Wheel()
|
|
if scroll < 0 {
|
|
world.CamScale -= 1
|
|
if world.CamScale < minZoom {
|
|
world.CamScale = minZoom
|
|
}
|
|
} else if scroll > 0 {
|
|
world.CamScale += 1
|
|
if world.CamScale > maxZoom {
|
|
world.CamScale = maxZoom
|
|
}
|
|
}
|
|
|
|
// Pan with mouse.
|
|
const panArea = 7
|
|
x, y := ebiten.CursorPosition()
|
|
if x <= panArea {
|
|
world.CamX -= panDistance
|
|
} else if x >= world.ScreenWidth-panArea {
|
|
world.CamX += panDistance
|
|
}
|
|
if y <= panArea {
|
|
world.CamY -= panDistance
|
|
} else if y >= world.ScreenHeight-panArea {
|
|
world.CamY += panDistance
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *HandleInput) Draw(e gohan.Entity, screen *ebiten.Image) error {
|
|
return gohan.ErrUnregister
|
|
}
|