Compare commits

2 Commits

Author SHA1 Message Date
0e89b8be70 Spawning radi 2025-09-21 08:24:28 +02:00
e8d99dd002 Tower defense, start, ending 2025-08-24 09:32:43 +02:00
14 changed files with 282 additions and 74 deletions

View File

@@ -15,6 +15,8 @@ const (
Hero ObjectType = 1 << iota Hero ObjectType = 1 << iota
Zombie Zombie
Bullet Bullet
Ending
Starting
) )
type Collidable interface { type Collidable interface {

104
main.go
View File

@@ -2,23 +2,24 @@ 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 = 640 screenWidth = 720
screenHeight = 480 screenHeight = 1280
tileSize = 33 tileSize = 33
titleFontSize = fontSize * 1.5 titleFontSize = fontSize * 1.5
fontSize = 24 fontSize = 24
@@ -42,46 +43,26 @@ 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
hero *hero.Hero terrain *terrain.Terrain
terrain *terrain.Terrain zombies []*zombie.Zombie
zombies []*zombie.Zombie world *collision.World
world *collision.World bullets []*weapons.Handgun
bullets []*weapons.Handgun ending *tiles.Ending
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.terrain.Move()
g.hero.Walk()
}
for _, b := range g.bullets {
if b.IsFlying {
b.Move()
}
}
g.world.NotifyAboutCollisions() g.world.NotifyAboutCollisions()
return nil return nil
} }
@@ -101,7 +82,9 @@ 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)
@@ -120,27 +103,38 @@ func init() {
GameInstance = &Game{} GameInstance = &Game{}
GameInstance.world = collision.NewWorld() GameInstance.world = collision.NewWorld()
GameInstance.control = &input.Keyboard{} GameInstance.control = &input.Keyboard{}
GameInstance.hero = hero.NewHero() GameInstance.zombies = []*zombie.Zombie{}
GameInstance.zombies = []*zombie.Zombie{zombie.NewZombie(), zombie.NewZombie(), zombie.NewZombie(), zombie.NewZombie(), zombie.NewZombie()} GameInstance.ending = tiles.NewEnding()
GameInstance.starting = tiles.NewStarting()
GameInstance.world.AddEntity(GameInstance.hero) GameInstance.world.AddEntity(GameInstance.starting)
// put zombies in random places on the screen but not too close to the hero or each other GameInstance.world.AddEntity(GameInstance.ending)
for _, z := range GameInstance.zombies {
z.X = float64(configuration.Random(50, screenWidth-50)) GameInstance.gameSpeed = 100 * time.Millisecond
z.Y = float64(configuration.Random(50, screenHeight-50)) GameInstance.zombieSpawnTicker = time.NewTicker(GameInstance.gameSpeed)
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) { GameInstance.zombiesToSpawn = 20
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()
GameInstance.hero.ChangeDirection(configuration.North) go func() {
GameInstance.terrain.ChangeDirection(configuration.North) log.Println("Starting zombie spawn ticker")
GameInstance.hero.Walk() for {
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() {

View File

@@ -2,3 +2,4 @@ package resources
const HeroTileSize = 33 const HeroTileSize = 33
const ZombieTileSize = 32 const ZombieTileSize = 32
const TileTileSize = 64

View File

@@ -16,4 +16,10 @@ 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
) )

BIN
resources/ending.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 841 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 8.6 KiB

BIN
resources/hero.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 3.8 KiB

BIN
resources/starting.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 819 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 830 B

After

Width:  |  Height:  |  Size: 1.7 KiB

6
tiles/common.go Normal file
View File

@@ -0,0 +1,6 @@
package tiles
type spritePosition struct {
x int
y int
}

81
tiles/ending.go Normal file
View File

@@ -0,0 +1,81 @@
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")
}
}

88
tiles/starting.go Normal file
View File

@@ -0,0 +1,88 @@
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
}

View File

@@ -3,31 +3,35 @@ 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"
"time"
) )
const WalkSpeedMs = 50 const WalkSpeedMs = 10
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
IsDead bool WhereToGoX float64
WhereToGoY float64
IsDead bool
walkTicker *time.Ticker
animationTicker *time.Ticker
} }
type spritePosition struct { type spritePosition struct {
@@ -63,8 +67,9 @@ 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() {
@@ -73,9 +78,6 @@ 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 {
@@ -84,11 +86,34 @@ 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 <-walkTicker.C: case <-zombie.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 {
@@ -98,13 +123,14 @@ func NewZombie() *Zombie {
} }
} }
}() }()
return zombie return zombie
} }
func (z *Zombie) Walk() { func (z *Zombie) Walk() {
if !z.IsWalking { if !z.IsWalking {
walkTicker.Reset(WalkSpeedMs * time.Millisecond) z.walkTicker.Reset(WalkSpeedMs * time.Millisecond)
z.IsWalking = true z.IsWalking = true
} }
@@ -152,5 +178,9 @@ 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
} }
} }