54 lines
1012 B
Go
54 lines
1012 B
Go
package input
|
|
|
|
import (
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
import "gitlab.com/kbr4/9heroja/configuration"
|
|
|
|
type Keyboard struct {
|
|
}
|
|
|
|
func (k *Keyboard) DirectionFromKeys(keys []ebiten.Key) configuration.Direction {
|
|
direction := ""
|
|
for _, key := range keys {
|
|
switch key {
|
|
case ebiten.KeyUp:
|
|
direction += "N"
|
|
case ebiten.KeyRight:
|
|
direction += "E"
|
|
case ebiten.KeyLeft:
|
|
direction += "W"
|
|
case ebiten.KeyDown:
|
|
direction += "S"
|
|
}
|
|
}
|
|
|
|
// sort direction string
|
|
s := strings.Split(direction, "")
|
|
sort.Strings(s)
|
|
sortedDirection := strings.Join(s, "")
|
|
|
|
switch sortedDirection {
|
|
case "N":
|
|
return configuration.North
|
|
case "E":
|
|
return configuration.East
|
|
case "S":
|
|
return configuration.South
|
|
case "W":
|
|
return configuration.West
|
|
case "EN":
|
|
return configuration.NorthEast
|
|
case "ES":
|
|
return configuration.SouthEast
|
|
case "SW":
|
|
return configuration.SouthWest
|
|
case "NW":
|
|
return configuration.NorthWest
|
|
default:
|
|
return configuration.PreviouslyHeld
|
|
}
|
|
}
|