Keyboard input for moving
This commit is contained in:
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() {
|
||||
|
||||
Reference in New Issue
Block a user