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

58
main.go
View File

@@ -2,23 +2,24 @@ package main
import (
"fmt"
_ "image/png"
"log"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"gitlab.com/kbr4/9heroja/collision"
"gitlab.com/kbr4/9heroja/configuration"
"gitlab.com/kbr4/9heroja/hero"
"gitlab.com/kbr4/9heroja/input"
"gitlab.com/kbr4/9heroja/terrain"
"gitlab.com/kbr4/9heroja/tiles"
"gitlab.com/kbr4/9heroja/weapons"
"gitlab.com/kbr4/9heroja/zombie"
_ "image/png"
"log"
)
const (
screenWidth = 640
screenHeight = 480
screenWidth = 720
screenHeight = 1280
tileSize = 33
titleFontSize = fontSize * 1.5
fontSize = 24
@@ -52,35 +53,17 @@ type Game struct {
control *input.Keyboard
hero *hero.Hero
terrain *terrain.Terrain
zombies []*zombie.Zombie
world *collision.World
bullets []*weapons.Handgun
terrain *terrain.Terrain
zombies []*zombie.Zombie
world *collision.World
bullets []*weapons.Handgun
ending *tiles.Ending
starting *tiles.Starting
}
func (g *Game) Update() error {
g.keys = inpututil.AppendPressedKeys(g.keys[:0])
GameInstance.hero.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()
return nil
@@ -101,7 +84,9 @@ func (g *Game) Draw(screen *ebiten.Image) {
b.DrawHandgunBullet(screen)
}
}
g.hero.DrawHero(screen)
g.ending.DrawEnding(screen)
g.starting.DrawStarting(screen)
msg := fmt.Sprintf(`TPS: %0.2f
FPS: %0.2f`, ebiten.ActualTPS(), ebiten.ActualFPS())
ebitenutil.DebugPrint(screen, msg)
@@ -120,10 +105,13 @@ func init() {
GameInstance = &Game{}
GameInstance.world = collision.NewWorld()
GameInstance.control = &input.Keyboard{}
GameInstance.hero = hero.NewHero()
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.starting)
GameInstance.world.AddEntity(GameInstance.ending)
GameInstance.world.AddEntity(GameInstance.hero)
// put zombies in random places on the screen but not too close to the hero or each other
for _, z := range GameInstance.zombies {
z.X = float64(configuration.Random(50, screenWidth-50))
@@ -132,15 +120,13 @@ func init() {
z.X = float64(configuration.Random(0, screenWidth))
z.Y = float64(configuration.Random(0, screenHeight))
}
z.WhereToGoX = GameInstance.ending.X
z.WhereToGoY = GameInstance.ending.Y
z.Walk()
GameInstance.world.AddEntity(z)
}
GameInstance.terrain = terrain.NewTerrain()
GameInstance.hero.ChangeDirection(configuration.North)
GameInstance.terrain.ChangeDirection(configuration.North)
GameInstance.hero.Walk()
}
func main() {