Direction

This commit is contained in:
2023-11-21 20:12:56 +01:00
parent 58ce169cc2
commit b4d81fbcfb
4 changed files with 92 additions and 7 deletions

17
configuration/config.go Normal file
View File

@@ -0,0 +1,17 @@
package configuration
type Direction int
const (
North Direction = iota
East
South
West
NorthEast
SouthEast
SouthWest
NorthWest
)
// var AllDirections = []Direction{North, East, South, West, NorthEast, SouthEast, SouthWest, NorthWest}
var AllDirections = []Direction{South, North, NorthEast}

View File

@@ -3,13 +3,14 @@ package hero
import ( import (
"bytes" "bytes"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
"gitlab.com/kbr4/9heroja/configuration"
"gitlab.com/kbr4/9heroja/resources" "gitlab.com/kbr4/9heroja/resources"
"image" "image"
"log" "log"
"time" "time"
) )
const WalkSpeedMs = 400 const WalkSpeedMs = 300
var ( var (
heroImage *ebiten.Image heroImage *ebiten.Image
@@ -17,7 +18,8 @@ var (
) )
type Hero struct { type Hero struct {
step int step int
direction configuration.Direction
} }
type spritePosition struct { type spritePosition struct {
@@ -27,7 +29,10 @@ type spritePosition struct {
func (h *Hero) DrawHero(screen *ebiten.Image) { func (h *Hero) DrawHero(screen *ebiten.Image) {
positions := []spritePosition{ // set movement positions to be hashmap of sprite positions for each direction
movementPositions := map[configuration.Direction][]spritePosition{}
movementPositions[configuration.North] = []spritePosition{
{0, 166}, {0, 166},
{33, 166}, {33, 166},
{66, 166}, {66, 166},
@@ -37,7 +42,28 @@ func (h *Hero) DrawHero(screen *ebiten.Image) {
{66, 166}, {66, 166},
{33, 166}, {33, 166},
} }
p := positions[h.step%5] movementPositions[configuration.NorthEast] = []spritePosition{
{168, 0},
{201, 0},
{0, 33},
{33, 33},
{66, 33},
{33, 33},
{0, 33},
{201, 0},
}
movementPositions[configuration.South] = []spritePosition{
{66, 132},
{0, 99},
{33, 99},
{66, 99},
{99, 99},
{66, 99},
{33, 99},
{0, 99},
}
p := movementPositions[h.direction][h.step%8]
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
screenWidth := screen.Bounds().Max.X screenWidth := screen.Bounds().Max.X
@@ -88,6 +114,10 @@ func (h *Hero) Walk() {
} }
func (h *Hero) ChangeDirection(d configuration.Direction) {
h.direction = d
}
func (h *Hero) Stop() { func (h *Hero) Stop() {
h.step = 2 h.step = 2
walkTicker.Stop() walkTicker.Stop()

21
main.go
View File

@@ -2,10 +2,13 @@ package main
import ( import (
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
"gitlab.com/kbr4/9heroja/configuration"
"gitlab.com/kbr4/9heroja/hero" "gitlab.com/kbr4/9heroja/hero"
"gitlab.com/kbr4/9heroja/terrain" "gitlab.com/kbr4/9heroja/terrain"
_ "image/png" _ "image/png"
"log" "log"
"math/rand"
"time"
) )
const ( const (
@@ -45,7 +48,7 @@ type Game struct {
} }
func (g *Game) Update() error { func (g *Game) Update() error {
g.terrain.GoDown() g.terrain.Move()
return nil return nil
} }
@@ -66,7 +69,23 @@ func init() {
GameInstance = &Game{} GameInstance = &Game{}
GameInstance.hero = hero.NewHero() GameInstance.hero = hero.NewHero()
GameInstance.terrain = terrain.NewTerrain() GameInstance.terrain = terrain.NewTerrain()
GameInstance.hero.ChangeDirection(configuration.North)
GameInstance.terrain.ChangeDirection(configuration.North)
GameInstance.hero.Walk() 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() { func main() {

View File

@@ -3,9 +3,11 @@ package terrain
import ( import (
"bytes" "bytes"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
"gitlab.com/kbr4/9heroja/configuration"
"gitlab.com/kbr4/9heroja/resources" "gitlab.com/kbr4/9heroja/resources"
"image" "image"
"log" "log"
"slices"
) )
var ( var (
@@ -15,6 +17,7 @@ var (
type Terrain struct { type Terrain struct {
positionX float64 positionX float64
positionY float64 positionY float64
direction configuration.Direction
} }
func (t *Terrain) DrawTerrain(screen *ebiten.Image) { func (t *Terrain) DrawTerrain(screen *ebiten.Image) {
@@ -49,6 +52,22 @@ func NewTerrain() *Terrain {
} }
} }
func (t *Terrain) GoDown() { func (t *Terrain) Move() {
t.positionY += 1 if slices.Contains([]configuration.Direction{configuration.North, configuration.NorthEast, configuration.NorthWest}, t.direction) {
t.positionY += 1
}
if slices.Contains([]configuration.Direction{configuration.South, configuration.SouthEast, configuration.SouthWest}, t.direction) {
t.positionY -= 1
}
if slices.Contains([]configuration.Direction{configuration.East, configuration.NorthEast, configuration.SouthEast}, t.direction) {
t.positionX -= 1
}
if slices.Contains([]configuration.Direction{configuration.West, configuration.NorthWest, configuration.SouthWest}, t.direction) {
t.positionX += 1
}
}
func (t *Terrain) ChangeDirection(direction configuration.Direction) {
t.direction = direction
} }