mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 11:18:54 +01:00
examples/2048: Use inpututil (#415)
This commit is contained in:
parent
d4d05d12ea
commit
7dc36747a6
@ -18,6 +18,7 @@ package twenty48
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
|
"github.com/hajimehoshi/ebiten/inpututil"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Dir represents a direction.
|
// Dir represents a direction.
|
||||||
@ -79,8 +80,6 @@ func (d Dir) Vector() (x, y int) {
|
|||||||
|
|
||||||
// Input represents the current key states.
|
// Input represents the current key states.
|
||||||
type Input struct {
|
type Input struct {
|
||||||
keyState map[ebiten.Key]int
|
|
||||||
|
|
||||||
mouseState mouseState
|
mouseState mouseState
|
||||||
mouseInitPosX int
|
mouseInitPosX int
|
||||||
mouseInitPosY int
|
mouseInitPosY int
|
||||||
@ -97,20 +96,9 @@ type Input struct {
|
|||||||
|
|
||||||
// NewInput generates a new Input object.
|
// NewInput generates a new Input object.
|
||||||
func NewInput() *Input {
|
func NewInput() *Input {
|
||||||
return &Input{
|
return &Input{}
|
||||||
keyState: map[ebiten.Key]int{},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
|
||||||
dirKeys = map[ebiten.Key]Dir{
|
|
||||||
ebiten.KeyUp: DirUp,
|
|
||||||
ebiten.KeyRight: DirRight,
|
|
||||||
ebiten.KeyDown: DirDown,
|
|
||||||
ebiten.KeyLeft: DirLeft,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
func abs(x int) int {
|
func abs(x int) int {
|
||||||
if x < 0 {
|
if x < 0 {
|
||||||
return -x
|
return -x
|
||||||
@ -137,13 +125,6 @@ func vecToDir(dx, dy int) (Dir, bool) {
|
|||||||
|
|
||||||
// Update updates the current input states.
|
// Update updates the current input states.
|
||||||
func (i *Input) Update() {
|
func (i *Input) Update() {
|
||||||
for k := range dirKeys {
|
|
||||||
if ebiten.IsKeyPressed(k) {
|
|
||||||
i.keyState[k]++
|
|
||||||
} else {
|
|
||||||
i.keyState[k] = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
switch i.mouseState {
|
switch i.mouseState {
|
||||||
case mouseStateNone:
|
case mouseStateNone:
|
||||||
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
|
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
|
||||||
@ -218,10 +199,17 @@ func (i *Input) Update() {
|
|||||||
// Dir returns a currently pressed direction.
|
// Dir returns a currently pressed direction.
|
||||||
// Dir returns false if no direction key is pressed.
|
// Dir returns false if no direction key is pressed.
|
||||||
func (i *Input) Dir() (Dir, bool) {
|
func (i *Input) Dir() (Dir, bool) {
|
||||||
for k, d := range dirKeys {
|
if inpututil.IsKeyJustPressed(ebiten.KeyUp) {
|
||||||
if i.keyState[k] == 1 {
|
return DirUp, true
|
||||||
return d, true
|
|
||||||
}
|
}
|
||||||
|
if inpututil.IsKeyJustPressed(ebiten.KeyLeft) {
|
||||||
|
return DirLeft, true
|
||||||
|
}
|
||||||
|
if inpututil.IsKeyJustPressed(ebiten.KeyRight) {
|
||||||
|
return DirRight, true
|
||||||
|
}
|
||||||
|
if inpututil.IsKeyJustPressed(ebiten.KeyDown) {
|
||||||
|
return DirDown, true
|
||||||
}
|
}
|
||||||
if i.mouseState == mouseStateSettled {
|
if i.mouseState == mouseStateSettled {
|
||||||
return i.mouseDir, true
|
return i.mouseDir, true
|
||||||
|
Loading…
Reference in New Issue
Block a user