Tower defense, start, ending

This commit is contained in:
2025-08-24 09:32:43 +02:00
parent 156e3ed43c
commit e8d99dd002
14 changed files with 238 additions and 50 deletions

View File

@@ -12,22 +12,25 @@ import (
"time"
)
const WalkSpeedMs = 50
const WalkSpeedMs = 10
var (
zombieImage *ebiten.Image
walkTicker *time.Ticker
zombieImage *ebiten.Image
walkTicker *time.Ticker
animationTicker *time.Ticker
)
type Zombie struct {
step int
direction configuration.Direction
IsWalking bool
X float64
Y float64
OffsetX float64
OffsetY float64
IsDead bool
step int
direction configuration.Direction
IsWalking bool
X float64
Y float64
OffsetX float64
OffsetY float64
WhereToGoX float64
WhereToGoY float64
IsDead bool
}
type spritePosition struct {
@@ -63,8 +66,9 @@ func (z *Zombie) DrawZombie(screen *ebiten.Image) {
// Apply red tint if the zombie is dead
if z.IsDead {
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() {
@@ -74,8 +78,7 @@ func init() {
}
zombieImage = ebiten.NewImageFromImage(img)
walkTicker = time.NewTicker(WalkSpeedMs * time.Millisecond)
// go routine that runs on every tick and increases step
animationTicker = time.NewTicker(WalkSpeedMs * 5 * time.Millisecond) // Adjust the speed of animation frames
}
func NewZombie() *Zombie {
@@ -89,6 +92,27 @@ func NewZombie() *Zombie {
for {
select {
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 <-animationTicker.C:
if zombie.IsWalking {
zombie.step++
if zombie.step > 3 {
@@ -98,6 +122,7 @@ func NewZombie() *Zombie {
}
}
}()
return zombie
}
@@ -152,5 +177,9 @@ func (z *Zombie) HandleCollisionEvent(other collision.Collidable) {
fmt.Println("Zombie hit by bullet")
z.Stop()
z.IsDead = true
case collision.Ending:
fmt.Println("Zombie hit the ending")
z.Stop()
z.IsDead = true
}
}