Bullet support

This commit is contained in:
2023-12-28 14:42:20 +01:00
parent b6776fa9f3
commit d336fa4d15
9 changed files with 196 additions and 2 deletions

29
main.go
View File

@@ -1,13 +1,16 @@
package main
import (
"fmt"
"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/weapons"
"gitlab.com/kbr4/9heroja/zombie"
_ "image/png"
"log"
@@ -53,18 +56,32 @@ type Game struct {
terrain *terrain.Terrain
zombies []*zombie.Zombie
world *collision.World
bullets []*weapons.Handgun
}
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(GameInstance.control.ShouldFire(g.keys), 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
}
@@ -76,7 +93,19 @@ func (g *Game) Draw(screen *ebiten.Image) {
z.OffsetY = g.terrain.PositionY
z.DrawZombie(screen)
}
for _, b := range g.bullets {
b.OffsetX = g.terrain.PositionX
b.OffsetY = g.terrain.PositionY
if b.IsFlying {
b.DrawHandgunBullet(screen)
}
}
g.hero.DrawHero(screen)
msg := fmt.Sprintf(`TPS: %0.2f
FPS: %0.2f`, ebiten.ActualTPS(), ebiten.ActualFPS())
ebitenutil.DebugPrint(screen, msg)
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {