Tower defense, start, ending
@@ -15,6 +15,8 @@ const (
|
||||
Hero ObjectType = 1 << iota
|
||||
Zombie
|
||||
Bullet
|
||||
Ending
|
||||
Starting
|
||||
)
|
||||
|
||||
type Collidable interface {
|
||||
|
||||
58
main.go
@@ -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() {
|
||||
|
||||
@@ -2,3 +2,4 @@ package resources
|
||||
|
||||
const HeroTileSize = 33
|
||||
const ZombieTileSize = 32
|
||||
const TileTileSize = 64
|
||||
|
||||
@@ -16,4 +16,10 @@ var (
|
||||
|
||||
//go:embed handgun.png
|
||||
Handgun_png []byte
|
||||
|
||||
//go:embed ending.png
|
||||
Ending_png []byte
|
||||
|
||||
//go:embed starting.png
|
||||
Starting_png []byte
|
||||
)
|
||||
|
||||
BIN
resources/ending.png
Normal file
|
After Width: | Height: | Size: 841 B |
|
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 8.6 KiB |
BIN
resources/hero.gif
Normal file
|
After Width: | Height: | Size: 9.1 KiB |
|
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 3.8 KiB |
BIN
resources/starting.png
Normal file
|
After Width: | Height: | Size: 819 B |
|
Before Width: | Height: | Size: 830 B After Width: | Height: | Size: 1.7 KiB |
6
tiles/common.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package tiles
|
||||
|
||||
type spritePosition struct {
|
||||
x int
|
||||
y int
|
||||
}
|
||||
81
tiles/ending.go
Normal 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")
|
||||
}
|
||||
}
|
||||
77
tiles/starting.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package tiles
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image"
|
||||
"log"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"gitlab.com/kbr4/9heroja/collision"
|
||||
"gitlab.com/kbr4/9heroja/resources"
|
||||
)
|
||||
|
||||
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")
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||