diff --git a/collision/types.go b/collision/types.go index fd070a9..57f2e38 100644 --- a/collision/types.go +++ b/collision/types.go @@ -15,6 +15,8 @@ const ( Hero ObjectType = 1 << iota Zombie Bullet + Ending + Starting ) type Collidable interface { diff --git a/main.go b/main.go index 6203d06..f12fbfd 100644 --- a/main.go +++ b/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() { diff --git a/resources/constants.go b/resources/constants.go index a3ea203..fd64190 100644 --- a/resources/constants.go +++ b/resources/constants.go @@ -2,3 +2,4 @@ package resources const HeroTileSize = 33 const ZombieTileSize = 32 +const TileTileSize = 64 diff --git a/resources/embed.go b/resources/embed.go index 90936c9..daf3491 100644 --- a/resources/embed.go +++ b/resources/embed.go @@ -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 ) diff --git a/resources/ending.png b/resources/ending.png new file mode 100644 index 0000000..1a9cd1e Binary files /dev/null and b/resources/ending.png differ diff --git a/resources/grass.png b/resources/grass.png index cd09802..3711ca4 100644 Binary files a/resources/grass.png and b/resources/grass.png differ diff --git a/resources/hero.gif b/resources/hero.gif new file mode 100644 index 0000000..521f4d4 Binary files /dev/null and b/resources/hero.gif differ diff --git a/resources/hero.png b/resources/hero.png index 2237754..0060c97 100644 Binary files a/resources/hero.png and b/resources/hero.png differ diff --git a/resources/starting.png b/resources/starting.png new file mode 100644 index 0000000..1e30cc0 Binary files /dev/null and b/resources/starting.png differ diff --git a/resources/zombie3.png b/resources/zombie3.png index 0111fb7..d49465c 100644 Binary files a/resources/zombie3.png and b/resources/zombie3.png differ diff --git a/tiles/common.go b/tiles/common.go new file mode 100644 index 0000000..7bceaf3 --- /dev/null +++ b/tiles/common.go @@ -0,0 +1,6 @@ +package tiles + +type spritePosition struct { + x int + y int +} diff --git a/tiles/ending.go b/tiles/ending.go new file mode 100644 index 0000000..f68363a --- /dev/null +++ b/tiles/ending.go @@ -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") + } +} diff --git a/tiles/starting.go b/tiles/starting.go new file mode 100644 index 0000000..9fda11b --- /dev/null +++ b/tiles/starting.go @@ -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") + } +} diff --git a/zombie/zombie.go b/zombie/zombie.go index 7ba9987..7fc993d 100644 --- a/zombie/zombie.go +++ b/zombie/zombie.go @@ -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 } }