doctorlectro/system/input_move.go

106 lines
2.4 KiB
Go

package system
import (
"os"
"code.rocketnine.space/tslocum/doctorlectro/component"
"code.rocketnine.space/tslocum/doctorlectro/world"
"code.rocketnine.space/tslocum/gohan"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
const (
moveSpeed = 0.02
maxMoveSpeed = 1.0
)
type playerMoveSystem struct {
Position *component.Position
Velocity *component.Velocity
Sprite *component.Sprite
Player *component.Player
}
func NewPlayerMoveSystem() *playerMoveSystem {
return &playerMoveSystem{}
}
func (s *playerMoveSystem) Update(e gohan.Entity) error {
if ebiten.IsKeyPressed(ebiten.KeyEscape) && !world.DisableEsc {
os.Exit(0)
return nil
}
if ebiten.IsKeyPressed(ebiten.KeyControl) && inpututil.IsKeyJustPressed(ebiten.KeyV) {
v := 1
if ebiten.IsKeyPressed(ebiten.KeyShift) {
v = 2
}
if world.Debug == v {
world.Debug = 0
} else {
world.Debug = v
}
return nil
}
if !world.GameStarted {
if ebiten.IsKeyPressed(ebiten.KeyEnter) || ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
world.StartGame()
}
return nil
}
/*if inpututil.IsKeyJustPressed(ebiten.KeyM) {
if asset.SoundLevelMusic.IsPlaying() {
asset.SoundLevelMusic.Pause()
} else {
asset.SoundLevelMusic.Play()
}
}*/
if world.GameOver {
if inpututil.IsKeyJustPressed(ebiten.KeyEnter) {
world.ResetGame = true
}
return nil
}
pressLeft := ebiten.IsKeyPressed(ebiten.KeyLeft) || ebiten.IsKeyPressed(ebiten.KeyA)
pressRight := ebiten.IsKeyPressed(ebiten.KeyRight) || ebiten.IsKeyPressed(ebiten.KeyD)
pressUp := ebiten.IsKeyPressed(ebiten.KeyUp) || ebiten.IsKeyPressed(ebiten.KeyW)
pressDown := ebiten.IsKeyPressed(ebiten.KeyDown) || ebiten.IsKeyPressed(ebiten.KeyS)
if (pressLeft && !pressRight) ||
(pressRight && !pressLeft) {
if pressLeft {
if s.Velocity.X > -maxMoveSpeed {
s.Velocity.X += -moveSpeed
}
world.LastWalkDirL = true
} else if pressRight {
if s.Velocity.X < maxMoveSpeed {
s.Velocity.X += moveSpeed
}
world.LastWalkDirL = false
}
}
if (pressUp && !pressDown) ||
(pressDown && !pressUp) {
if pressUp && world.Clinging {
s.Velocity.Y += -moveSpeed
world.LastWalkDirU = true
} else if pressDown && world.Clinging {
s.Velocity.Y += moveSpeed
world.LastWalkDirU = false
}
}
return nil
}
func (s *playerMoveSystem) Draw(_ gohan.Entity, _ *ebiten.Image) error {
return gohan.ErrUnregister
}