Compare commits
1 Commits
tower_defe
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| a838fcec48 |
@@ -15,8 +15,6 @@ const (
|
|||||||
Hero ObjectType = 1 << iota
|
Hero ObjectType = 1 << iota
|
||||||
Zombie
|
Zombie
|
||||||
Bullet
|
Bullet
|
||||||
Ending
|
|
||||||
Starting
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Collidable interface {
|
type Collidable interface {
|
||||||
|
|||||||
22
hero/hero.go
@@ -10,6 +10,7 @@ import (
|
|||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
"log"
|
"log"
|
||||||
|
"slices"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -131,13 +132,9 @@ func (h *Hero) DrawHero(screen *ebiten.Image) {
|
|||||||
p := movementPositions[h.direction][h.step%8]
|
p := movementPositions[h.direction][h.step%8]
|
||||||
|
|
||||||
op := &ebiten.DrawImageOptions{}
|
op := &ebiten.DrawImageOptions{}
|
||||||
screenWidth := screen.Bounds().Max.X
|
|
||||||
screenHeight := screen.Bounds().Max.Y
|
|
||||||
|
|
||||||
// ground
|
// ground
|
||||||
op.GeoM.Reset()
|
op.GeoM.Reset()
|
||||||
h.X = float64(screenWidth/2 - 16)
|
|
||||||
h.Y = float64(screenHeight/2 - 16)
|
|
||||||
op.GeoM.Translate(h.X, h.Y)
|
op.GeoM.Translate(h.X, h.Y)
|
||||||
//op.GeoM.Translate(float64(i*tileSize-floorMod(g.cameraX, tileSize)),
|
//op.GeoM.Translate(float64(i*tileSize-floorMod(g.cameraX, tileSize)),
|
||||||
// float64((ny-1)*tileSize-floorMod(g.cameraY, tileSize)))
|
// float64((ny-1)*tileSize-floorMod(g.cameraY, tileSize)))
|
||||||
@@ -193,6 +190,8 @@ func NewHero() *Hero {
|
|||||||
Health: 100,
|
Health: 100,
|
||||||
gunLoaded: true,
|
gunLoaded: true,
|
||||||
}
|
}
|
||||||
|
hero.X = float64(10)
|
||||||
|
hero.Y = float64(10)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
@@ -284,3 +283,18 @@ func (h *Hero) Fire(offsetX float64, offsetY float64) (bullet *weapons.Handgun)
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func (h *Hero) Move() {
|
||||||
|
if slices.Contains([]configuration.Direction{configuration.North, configuration.NorthEast, configuration.NorthWest}, h.direction) {
|
||||||
|
h.Y -= 3
|
||||||
|
}
|
||||||
|
if slices.Contains([]configuration.Direction{configuration.South, configuration.SouthEast, configuration.SouthWest}, h.direction) {
|
||||||
|
h.Y += 3
|
||||||
|
}
|
||||||
|
if slices.Contains([]configuration.Direction{configuration.East, configuration.NorthEast, configuration.SouthEast}, h.direction) {
|
||||||
|
h.X += 3
|
||||||
|
}
|
||||||
|
if slices.Contains([]configuration.Direction{configuration.West, configuration.NorthWest, configuration.SouthWest}, h.direction) {
|
||||||
|
h.X -= 3
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
112
main.go
@@ -2,24 +2,23 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
_ "image/png"
|
|
||||||
"log"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
||||||
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
||||||
"gitlab.com/kbr4/9heroja/collision"
|
"gitlab.com/kbr4/9heroja/collision"
|
||||||
|
"gitlab.com/kbr4/9heroja/configuration"
|
||||||
|
"gitlab.com/kbr4/9heroja/hero"
|
||||||
"gitlab.com/kbr4/9heroja/input"
|
"gitlab.com/kbr4/9heroja/input"
|
||||||
"gitlab.com/kbr4/9heroja/terrain"
|
"gitlab.com/kbr4/9heroja/terrain"
|
||||||
"gitlab.com/kbr4/9heroja/tiles"
|
|
||||||
"gitlab.com/kbr4/9heroja/weapons"
|
"gitlab.com/kbr4/9heroja/weapons"
|
||||||
"gitlab.com/kbr4/9heroja/zombie"
|
"gitlab.com/kbr4/9heroja/zombie"
|
||||||
|
_ "image/png"
|
||||||
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
screenWidth = 720
|
screenWidth = 640
|
||||||
screenHeight = 1280
|
screenHeight = 480
|
||||||
tileSize = 33
|
tileSize = 33
|
||||||
titleFontSize = fontSize * 1.5
|
titleFontSize = fontSize * 1.5
|
||||||
fontSize = 24
|
fontSize = 24
|
||||||
@@ -43,26 +42,50 @@ func floorMod(x, y int) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Game struct {
|
type Game struct {
|
||||||
|
|
||||||
|
// The gopher's position
|
||||||
|
x16 int
|
||||||
|
y16 int
|
||||||
|
vy16 int
|
||||||
|
|
||||||
keys []ebiten.Key
|
keys []ebiten.Key
|
||||||
|
|
||||||
control *input.Keyboard
|
control *input.Keyboard
|
||||||
|
|
||||||
terrain *terrain.Terrain
|
hero *hero.Hero
|
||||||
zombies []*zombie.Zombie
|
terrain *terrain.Terrain
|
||||||
world *collision.World
|
zombies []*zombie.Zombie
|
||||||
bullets []*weapons.Handgun
|
world *collision.World
|
||||||
ending *tiles.Ending
|
bullets []*weapons.Handgun
|
||||||
starting *tiles.Starting
|
|
||||||
|
|
||||||
gameSpeed time.Duration
|
|
||||||
tickCount int
|
|
||||||
zombiesToSpawn int
|
|
||||||
zombieSpawnTicker *time.Ticker
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Game) Update() error {
|
func (g *Game) Update() error {
|
||||||
g.keys = inpututil.AppendPressedKeys(g.keys[:0])
|
g.keys = inpututil.AppendPressedKeys(g.keys[:0])
|
||||||
|
GameInstance.hero.ChangeDirection(GameInstance.control.DirectionFromKeys(g.keys))
|
||||||
GameInstance.terrain.ChangeDirection(GameInstance.control.DirectionFromKeys(g.keys))
|
GameInstance.terrain.ChangeDirection(GameInstance.control.DirectionFromKeys(g.keys))
|
||||||
|
bullet := GameInstance.hero.Fire(g.terrain.PositionX, g.terrain.PositionY)
|
||||||
|
if bullet != nil {
|
||||||
|
GameInstance.world.AddEntity(bullet)
|
||||||
|
g.bullets = append(g.bullets, bullet)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(g.keys) <= 0 {
|
||||||
|
g.hero.Stop()
|
||||||
|
} else {
|
||||||
|
g.hero.Move()
|
||||||
|
g.hero.Walk()
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, b := range g.bullets {
|
||||||
|
if b.IsFlying {
|
||||||
|
b.Move()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, z := range g.zombies {
|
||||||
|
z.TargetX = g.hero.X
|
||||||
|
z.TargetY = g.hero.Y
|
||||||
|
}
|
||||||
|
|
||||||
g.world.NotifyAboutCollisions()
|
g.world.NotifyAboutCollisions()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -70,8 +93,6 @@ func (g *Game) Update() error {
|
|||||||
func (g *Game) Draw(screen *ebiten.Image) {
|
func (g *Game) Draw(screen *ebiten.Image) {
|
||||||
g.terrain.DrawTerrain(screen)
|
g.terrain.DrawTerrain(screen)
|
||||||
for _, z := range g.zombies {
|
for _, z := range g.zombies {
|
||||||
z.OffsetX = g.terrain.PositionX
|
|
||||||
z.OffsetY = g.terrain.PositionY
|
|
||||||
z.DrawZombie(screen)
|
z.DrawZombie(screen)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,9 +103,7 @@ func (g *Game) Draw(screen *ebiten.Image) {
|
|||||||
b.DrawHandgunBullet(screen)
|
b.DrawHandgunBullet(screen)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
g.hero.DrawHero(screen)
|
||||||
g.ending.DrawEnding(screen)
|
|
||||||
g.starting.DrawStarting(screen)
|
|
||||||
msg := fmt.Sprintf(`TPS: %0.2f
|
msg := fmt.Sprintf(`TPS: %0.2f
|
||||||
FPS: %0.2f`, ebiten.ActualTPS(), ebiten.ActualFPS())
|
FPS: %0.2f`, ebiten.ActualTPS(), ebiten.ActualFPS())
|
||||||
ebitenutil.DebugPrint(screen, msg)
|
ebitenutil.DebugPrint(screen, msg)
|
||||||
@@ -103,38 +122,29 @@ func init() {
|
|||||||
GameInstance = &Game{}
|
GameInstance = &Game{}
|
||||||
GameInstance.world = collision.NewWorld()
|
GameInstance.world = collision.NewWorld()
|
||||||
GameInstance.control = &input.Keyboard{}
|
GameInstance.control = &input.Keyboard{}
|
||||||
GameInstance.zombies = []*zombie.Zombie{}
|
GameInstance.hero = hero.NewHero()
|
||||||
GameInstance.ending = tiles.NewEnding()
|
GameInstance.zombies = []*zombie.Zombie{zombie.NewZombie(), zombie.NewZombie(), zombie.NewZombie(), zombie.NewZombie(), zombie.NewZombie()}
|
||||||
GameInstance.starting = tiles.NewStarting()
|
|
||||||
|
|
||||||
GameInstance.world.AddEntity(GameInstance.starting)
|
GameInstance.world.AddEntity(GameInstance.hero)
|
||||||
GameInstance.world.AddEntity(GameInstance.ending)
|
// put zombies in random places on the screen but not too close to the hero or each other
|
||||||
|
for _, z := range GameInstance.zombies {
|
||||||
GameInstance.gameSpeed = 100 * time.Millisecond
|
z.TargetX = GameInstance.hero.X
|
||||||
GameInstance.zombieSpawnTicker = time.NewTicker(GameInstance.gameSpeed)
|
z.TargetY = GameInstance.hero.Y
|
||||||
GameInstance.zombiesToSpawn = 20
|
z.X = float64(configuration.Random(50, screenWidth-50))
|
||||||
|
z.Y = float64(configuration.Random(50, screenHeight-50))
|
||||||
|
for z.X > float64(screenWidth/2-64) && z.X < float64(screenWidth/2+64) && z.Y > float64(screenHeight/2-64) && z.Y < float64(screenHeight/2+64) {
|
||||||
|
z.X = float64(configuration.Random(0, screenWidth))
|
||||||
|
z.Y = float64(configuration.Random(0, screenHeight))
|
||||||
|
}
|
||||||
|
z.Walk()
|
||||||
|
GameInstance.world.AddEntity(z)
|
||||||
|
}
|
||||||
|
|
||||||
GameInstance.terrain = terrain.NewTerrain()
|
GameInstance.terrain = terrain.NewTerrain()
|
||||||
|
|
||||||
go func() {
|
GameInstance.hero.ChangeDirection(configuration.North)
|
||||||
log.Println("Starting zombie spawn ticker")
|
GameInstance.terrain.ChangeDirection(configuration.North)
|
||||||
for {
|
GameInstance.hero.Walk()
|
||||||
select {
|
|
||||||
case <-GameInstance.zombieSpawnTicker.C:
|
|
||||||
GameInstance.tickCount++
|
|
||||||
if GameInstance.tickCount%10 == 0 && GameInstance.zombiesToSpawn > 0 {
|
|
||||||
z := GameInstance.starting.SpawnZombie(GameInstance.ending.X, GameInstance.ending.Y)
|
|
||||||
GameInstance.zombies = append(GameInstance.zombies, z)
|
|
||||||
GameInstance.world.AddEntity(z)
|
|
||||||
GameInstance.zombiesToSpawn--
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if GameInstance.zombiesToSpawn <= 0 {
|
|
||||||
GameInstance.zombieSpawnTicker.Stop()
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
@@ -2,4 +2,3 @@ package resources
|
|||||||
|
|
||||||
const HeroTileSize = 33
|
const HeroTileSize = 33
|
||||||
const ZombieTileSize = 32
|
const ZombieTileSize = 32
|
||||||
const TileTileSize = 64
|
|
||||||
|
|||||||
@@ -16,10 +16,4 @@ var (
|
|||||||
|
|
||||||
//go:embed handgun.png
|
//go:embed handgun.png
|
||||||
Handgun_png []byte
|
Handgun_png []byte
|
||||||
|
|
||||||
//go:embed ending.png
|
|
||||||
Ending_png []byte
|
|
||||||
|
|
||||||
//go:embed starting.png
|
|
||||||
Starting_png []byte
|
|
||||||
)
|
)
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 841 B |
|
Before Width: | Height: | Size: 8.6 KiB After Width: | Height: | Size: 14 KiB |
|
Before Width: | Height: | Size: 9.1 KiB |
|
Before Width: | Height: | Size: 3.8 KiB After Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 819 B |
|
Before Width: | Height: | Size: 1.7 KiB After Width: | Height: | Size: 830 B |
@@ -7,7 +7,6 @@ import (
|
|||||||
"gitlab.com/kbr4/9heroja/resources"
|
"gitlab.com/kbr4/9heroja/resources"
|
||||||
"image"
|
"image"
|
||||||
"log"
|
"log"
|
||||||
"slices"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -54,22 +53,6 @@ func NewTerrain() *Terrain {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Terrain) Move() {
|
|
||||||
if slices.Contains([]configuration.Direction{configuration.North, configuration.NorthEast, configuration.NorthWest}, t.direction) {
|
|
||||||
t.PositionY += 3
|
|
||||||
}
|
|
||||||
if slices.Contains([]configuration.Direction{configuration.South, configuration.SouthEast, configuration.SouthWest}, t.direction) {
|
|
||||||
t.PositionY -= 3
|
|
||||||
}
|
|
||||||
if slices.Contains([]configuration.Direction{configuration.East, configuration.NorthEast, configuration.SouthEast}, t.direction) {
|
|
||||||
t.PositionX -= 3
|
|
||||||
}
|
|
||||||
if slices.Contains([]configuration.Direction{configuration.West, configuration.NorthWest, configuration.SouthWest}, t.direction) {
|
|
||||||
t.PositionX += 3
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *Terrain) ChangeDirection(direction configuration.Direction) {
|
func (t *Terrain) ChangeDirection(direction configuration.Direction) {
|
||||||
if direction != configuration.PreviouslyHeld {
|
if direction != configuration.PreviouslyHeld {
|
||||||
t.direction = direction
|
t.direction = direction
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
package tiles
|
|
||||||
|
|
||||||
type spritePosition struct {
|
|
||||||
x int
|
|
||||||
y int
|
|
||||||
}
|
|
||||||
@@ -1,81 +0,0 @@
|
|||||||
package tiles
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"image"
|
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
|
||||||
"gitlab.com/kbr4/9heroja/collision"
|
|
||||||
"gitlab.com/kbr4/9heroja/resources"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
destinationImage *ebiten.Image
|
|
||||||
)
|
|
||||||
|
|
||||||
type Ending struct {
|
|
||||||
X float64
|
|
||||||
Y float64
|
|
||||||
IsDead bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Ending) DrawEnding(screen *ebiten.Image) {
|
|
||||||
|
|
||||||
// set movement positions to be hashmap of sprite positions for each direction
|
|
||||||
|
|
||||||
op := &ebiten.DrawImageOptions{}
|
|
||||||
op.GeoM.Reset()
|
|
||||||
op.GeoM.Translate(e.X, e.Y) // Center the sprite on the tile
|
|
||||||
op.GeoM.Scale(1, 1)
|
|
||||||
// Apply red tint if the zombie is dead
|
|
||||||
if e.IsDead {
|
|
||||||
op.ColorScale.Scale(1, 0, 0, 1) // Scale the red channel up, green and blue down.
|
|
||||||
}
|
|
||||||
screen.DrawImage(destinationImage.SubImage(image.Rect(0, 0, resources.TileTileSize, resources.TileTileSize)).(*ebiten.Image), op)
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
img, _, err := image.Decode(bytes.NewReader(resources.Ending_png))
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
destinationImage = ebiten.NewImageFromImage(img)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewEnding() *Ending {
|
|
||||||
ending := &Ending{}
|
|
||||||
ending.X = 45
|
|
||||||
ending.Y = 75
|
|
||||||
return ending
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Ending) CollisionShape() collision.Polygon {
|
|
||||||
if !e.IsDead {
|
|
||||||
return collision.Polygon{
|
|
||||||
Points: []collision.Point{
|
|
||||||
{X: e.X, Y: e.Y},
|
|
||||||
{X: e.X + resources.TileTileSize, Y: e.Y},
|
|
||||||
{X: e.X + resources.TileTileSize, Y: e.Y + resources.TileTileSize},
|
|
||||||
{X: e.X, Y: e.Y + resources.TileTileSize},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return collision.Polygon{
|
|
||||||
Points: []collision.Point{},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Ending) CollisionObjectType() collision.ObjectType {
|
|
||||||
return collision.Ending
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Ending) HandleCollisionEvent(other collision.Collidable) {
|
|
||||||
coll := other.CollisionObjectType()
|
|
||||||
switch coll {
|
|
||||||
case collision.Zombie:
|
|
||||||
fmt.Println("Ending hit by zombie")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,88 +0,0 @@
|
|||||||
package tiles
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"image"
|
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
|
||||||
"gitlab.com/kbr4/9heroja/collision"
|
|
||||||
"gitlab.com/kbr4/9heroja/resources"
|
|
||||||
"gitlab.com/kbr4/9heroja/zombie"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
startingImage *ebiten.Image
|
|
||||||
)
|
|
||||||
|
|
||||||
type Starting struct {
|
|
||||||
X float64
|
|
||||||
Y float64
|
|
||||||
IsDead bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Starting) DrawStarting(screen *ebiten.Image) {
|
|
||||||
op := &ebiten.DrawImageOptions{}
|
|
||||||
op.GeoM.Reset()
|
|
||||||
op.GeoM.Translate(s.X, s.Y)
|
|
||||||
op.GeoM.Scale(1, 1)
|
|
||||||
if s.IsDead {
|
|
||||||
op.ColorScale.Scale(1, 0, 0, 1)
|
|
||||||
}
|
|
||||||
screen.DrawImage(startingImage.SubImage(image.Rect(0, 0, resources.TileTileSize, resources.TileTileSize)).(*ebiten.Image), op)
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
img, _, err := image.Decode(bytes.NewReader(resources.Starting_png))
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
startingImage = ebiten.NewImageFromImage(img)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewStarting() *Starting {
|
|
||||||
starting := &Starting{}
|
|
||||||
starting.X = 550
|
|
||||||
starting.Y = 1000
|
|
||||||
return starting
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Starting) CollisionShape() collision.Polygon {
|
|
||||||
if !s.IsDead {
|
|
||||||
return collision.Polygon{
|
|
||||||
Points: []collision.Point{
|
|
||||||
{X: s.X, Y: s.Y},
|
|
||||||
{X: s.X + resources.TileTileSize, Y: s.Y},
|
|
||||||
{X: s.X + resources.TileTileSize, Y: s.Y + resources.TileTileSize},
|
|
||||||
{X: s.X, Y: s.Y + resources.TileTileSize},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return collision.Polygon{
|
|
||||||
Points: []collision.Point{},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Starting) CollisionObjectType() collision.ObjectType {
|
|
||||||
return collision.Starting
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Starting) HandleCollisionEvent(other collision.Collidable) {
|
|
||||||
coll := other.CollisionObjectType()
|
|
||||||
switch coll {
|
|
||||||
case collision.Zombie:
|
|
||||||
fmt.Println("Starting hit by zombie")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Starting) SpawnZombie(whereToGoX, whereToGoY float64) *zombie.Zombie {
|
|
||||||
z := zombie.NewZombie()
|
|
||||||
z.X = s.X
|
|
||||||
z.Y = s.Y
|
|
||||||
z.WhereToGoX = whereToGoX
|
|
||||||
z.WhereToGoY = whereToGoY
|
|
||||||
z.Walk()
|
|
||||||
return z
|
|
||||||
}
|
|
||||||
@@ -3,35 +3,34 @@ package zombie
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
|
||||||
"log"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
"gitlab.com/kbr4/9heroja/collision"
|
"gitlab.com/kbr4/9heroja/collision"
|
||||||
"gitlab.com/kbr4/9heroja/configuration"
|
"gitlab.com/kbr4/9heroja/configuration"
|
||||||
"gitlab.com/kbr4/9heroja/resources"
|
"gitlab.com/kbr4/9heroja/resources"
|
||||||
|
"image"
|
||||||
|
"log"
|
||||||
|
"math"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const WalkSpeedMs = 10
|
const WalkSpeedMs = 50
|
||||||
|
|
||||||
var (
|
var (
|
||||||
zombieImage *ebiten.Image
|
zombieImage *ebiten.Image
|
||||||
|
walkTicker *time.Ticker
|
||||||
)
|
)
|
||||||
|
|
||||||
type Zombie struct {
|
type Zombie struct {
|
||||||
step int
|
step int
|
||||||
direction configuration.Direction
|
direction configuration.Direction
|
||||||
IsWalking bool
|
IsWalking bool
|
||||||
X float64
|
X float64
|
||||||
Y float64
|
Y float64
|
||||||
OffsetX float64
|
OffsetX float64
|
||||||
OffsetY float64
|
OffsetY float64
|
||||||
WhereToGoX float64
|
IsDead bool
|
||||||
WhereToGoY float64
|
TargetX float64
|
||||||
IsDead bool
|
TargetY float64
|
||||||
walkTicker *time.Ticker
|
|
||||||
animationTicker *time.Ticker
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type spritePosition struct {
|
type spritePosition struct {
|
||||||
@@ -67,9 +66,8 @@ func (z *Zombie) DrawZombie(screen *ebiten.Image) {
|
|||||||
// Apply red tint if the zombie is dead
|
// Apply red tint if the zombie is dead
|
||||||
if z.IsDead {
|
if z.IsDead {
|
||||||
op.ColorScale.Scale(1, 0, 0, 1) // Scale the red channel up, green and blue down.
|
op.ColorScale.Scale(1, 0, 0, 1) // Scale the red channel up, green and blue down.
|
||||||
} else {
|
|
||||||
screen.DrawImage(zombieImage.SubImage(image.Rect(p.x+1, p.y, p.x+resources.ZombieTileSize, p.y+resources.HeroTileSize)).(*ebiten.Image), op)
|
|
||||||
}
|
}
|
||||||
|
screen.DrawImage(zombieImage.SubImage(image.Rect(p.x+1, p.y, p.x+resources.ZombieTileSize, p.y+resources.HeroTileSize)).(*ebiten.Image), op)
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -78,6 +76,9 @@ func init() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
zombieImage = ebiten.NewImageFromImage(img)
|
zombieImage = ebiten.NewImageFromImage(img)
|
||||||
|
walkTicker = time.NewTicker(WalkSpeedMs * time.Millisecond)
|
||||||
|
// go routine that runs on every tick and increases step
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewZombie() *Zombie {
|
func NewZombie() *Zombie {
|
||||||
@@ -86,52 +87,31 @@ func NewZombie() *Zombie {
|
|||||||
IsWalking: false,
|
IsWalking: false,
|
||||||
IsDead: false,
|
IsDead: false,
|
||||||
}
|
}
|
||||||
zombie.walkTicker = time.NewTicker(WalkSpeedMs * time.Millisecond)
|
|
||||||
zombie.animationTicker = time.NewTicker(WalkSpeedMs * 10 * time.Millisecond) // Adjust the speed of animation frames
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-zombie.walkTicker.C:
|
case <-walkTicker.C:
|
||||||
if zombie.IsWalking {
|
|
||||||
// Move the zombie towards its target position
|
|
||||||
if zombie.X < zombie.WhereToGoX {
|
|
||||||
zombie.X += 1
|
|
||||||
} else if zombie.X > zombie.WhereToGoX {
|
|
||||||
zombie.X -= 1
|
|
||||||
}
|
|
||||||
if zombie.Y < zombie.WhereToGoY {
|
|
||||||
zombie.Y += 1
|
|
||||||
} else if zombie.Y > zombie.WhereToGoY {
|
|
||||||
zombie.Y -= 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-zombie.animationTicker.C:
|
|
||||||
if zombie.IsWalking {
|
if zombie.IsWalking {
|
||||||
zombie.step++
|
zombie.step++
|
||||||
if zombie.step > 3 {
|
if zombie.step > 3 {
|
||||||
zombie.step = 0
|
zombie.step = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
zombie.Move()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return zombie
|
return zombie
|
||||||
}
|
}
|
||||||
|
|
||||||
func (z *Zombie) Walk() {
|
func (z *Zombie) Walk() {
|
||||||
|
|
||||||
if !z.IsWalking {
|
if !z.IsWalking {
|
||||||
z.walkTicker.Reset(WalkSpeedMs * time.Millisecond)
|
walkTicker.Reset(WalkSpeedMs * time.Millisecond)
|
||||||
z.IsWalking = true
|
z.IsWalking = true
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -178,9 +158,13 @@ func (z *Zombie) HandleCollisionEvent(other collision.Collidable) {
|
|||||||
fmt.Println("Zombie hit by bullet")
|
fmt.Println("Zombie hit by bullet")
|
||||||
z.Stop()
|
z.Stop()
|
||||||
z.IsDead = true
|
z.IsDead = true
|
||||||
case collision.Ending:
|
|
||||||
fmt.Println("Zombie hit the ending")
|
|
||||||
z.Stop()
|
|
||||||
z.IsDead = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (z *Zombie) Move() {
|
||||||
|
fmt.Printf("Zombie target: %f, %f\n", z.TargetX, z.TargetY)
|
||||||
|
directionX := (z.TargetX - z.X) / math.Abs(z.TargetX+z.X)
|
||||||
|
directionY := (z.TargetY - z.Y) / math.Abs(z.TargetY+z.Y)
|
||||||
|
z.X += directionX
|
||||||
|
z.Y += directionY
|
||||||
|
}
|
||||||
|
|||||||