Keyboard input for moving
This commit is contained in:
@@ -11,6 +11,5 @@ const (
|
||||
SouthEast
|
||||
SouthWest
|
||||
NorthWest
|
||||
PreviouslyHeld
|
||||
)
|
||||
|
||||
var AllDirections = []Direction{North, East, South, West, NorthEast, SouthEast, SouthWest, NorthWest}
|
||||
|
||||
14
hero/hero.go
14
hero/hero.go
@@ -20,6 +20,7 @@ var (
|
||||
type Hero struct {
|
||||
step int
|
||||
direction configuration.Direction
|
||||
IsWalking bool
|
||||
}
|
||||
|
||||
type spritePosition struct {
|
||||
@@ -146,7 +147,8 @@ func init() {
|
||||
|
||||
func NewHero() *Hero {
|
||||
hero := &Hero{
|
||||
step: 0,
|
||||
step: 0,
|
||||
IsWalking: false,
|
||||
}
|
||||
|
||||
go func() {
|
||||
@@ -165,15 +167,21 @@ func NewHero() *Hero {
|
||||
|
||||
func (h *Hero) Walk() {
|
||||
|
||||
walkTicker.Reset(WalkSpeedMs * time.Millisecond)
|
||||
if !h.IsWalking {
|
||||
walkTicker.Reset(WalkSpeedMs * time.Millisecond)
|
||||
h.IsWalking = true
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (h *Hero) ChangeDirection(d configuration.Direction) {
|
||||
h.direction = d
|
||||
if d != configuration.PreviouslyHeld {
|
||||
h.direction = d
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Hero) Stop() {
|
||||
h.step = 2
|
||||
walkTicker.Stop()
|
||||
h.IsWalking = false
|
||||
}
|
||||
|
||||
53
input/keyboard.go
Normal file
53
input/keyboard.go
Normal file
@@ -0,0 +1,53 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
33
main.go
33
main.go
@@ -2,13 +2,13 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
||||
"gitlab.com/kbr4/9heroja/configuration"
|
||||
"gitlab.com/kbr4/9heroja/hero"
|
||||
"gitlab.com/kbr4/9heroja/input"
|
||||
"gitlab.com/kbr4/9heroja/terrain"
|
||||
_ "image/png"
|
||||
"log"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -43,12 +43,24 @@ type Game struct {
|
||||
y16 int
|
||||
vy16 int
|
||||
|
||||
keys []ebiten.Key
|
||||
|
||||
control *input.Keyboard
|
||||
|
||||
hero *hero.Hero
|
||||
terrain *terrain.Terrain
|
||||
}
|
||||
|
||||
func (g *Game) Update() error {
|
||||
g.terrain.Move()
|
||||
g.keys = inpututil.AppendPressedKeys(g.keys[:0])
|
||||
GameInstance.hero.ChangeDirection(GameInstance.control.DirectionFromKeys(g.keys))
|
||||
GameInstance.terrain.ChangeDirection(GameInstance.control.DirectionFromKeys(g.keys))
|
||||
if len(g.keys) <= 0 {
|
||||
g.hero.Stop()
|
||||
} else {
|
||||
g.terrain.Move()
|
||||
g.hero.Walk()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -67,25 +79,12 @@ var GameInstance *Game
|
||||
func init() {
|
||||
|
||||
GameInstance = &Game{}
|
||||
GameInstance.control = &input.Keyboard{}
|
||||
GameInstance.hero = hero.NewHero()
|
||||
GameInstance.terrain = terrain.NewTerrain()
|
||||
GameInstance.hero.ChangeDirection(configuration.North)
|
||||
GameInstance.terrain.ChangeDirection(configuration.North)
|
||||
GameInstance.hero.Walk()
|
||||
|
||||
ticker := time.NewTicker(1500 * time.Millisecond)
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
// change terrain direction to random direction
|
||||
randomDirection := configuration.AllDirections[rand.Intn(len(configuration.AllDirections))]
|
||||
GameInstance.terrain.ChangeDirection(randomDirection)
|
||||
GameInstance.hero.ChangeDirection(randomDirection)
|
||||
}
|
||||
}
|
||||
|
||||
}()
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
@@ -69,5 +69,7 @@ func (t *Terrain) Move() {
|
||||
}
|
||||
|
||||
func (t *Terrain) ChangeDirection(direction configuration.Direction) {
|
||||
t.direction = direction
|
||||
if direction != configuration.PreviouslyHeld {
|
||||
t.direction = direction
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user