Bullet support
This commit is contained in:
@@ -14,6 +14,7 @@ type ObjectType int
|
|||||||
const (
|
const (
|
||||||
Hero ObjectType = 1 << iota
|
Hero ObjectType = 1 << iota
|
||||||
Zombie
|
Zombie
|
||||||
|
Bullet
|
||||||
)
|
)
|
||||||
|
|
||||||
type Collidable interface {
|
type Collidable interface {
|
||||||
|
|||||||
@@ -18,7 +18,10 @@ func (w *World) RemoveEntity(e Collidable) {
|
|||||||
|
|
||||||
func (w *World) NotifyAboutCollisions() {
|
func (w *World) NotifyAboutCollisions() {
|
||||||
for i, entity := range w.Entities {
|
for i, entity := range w.Entities {
|
||||||
for _, other := range w.Entities[i+1:] {
|
for j, other := range w.Entities {
|
||||||
|
if i == j {
|
||||||
|
continue
|
||||||
|
}
|
||||||
if entity.CollisionShape().Overlaps(other.CollisionShape()) {
|
if entity.CollisionShape().Overlaps(other.CollisionShape()) {
|
||||||
entity.HandleCollisionEvent(other)
|
entity.HandleCollisionEvent(other)
|
||||||
other.HandleCollisionEvent(entity)
|
other.HandleCollisionEvent(entity)
|
||||||
|
|||||||
15
hero/hero.go
15
hero/hero.go
@@ -6,6 +6,7 @@ import (
|
|||||||
"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"
|
||||||
|
"gitlab.com/kbr4/9heroja/weapons"
|
||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
"log"
|
"log"
|
||||||
@@ -248,3 +249,17 @@ func (h *Hero) HandleCollisionEvent(other collision.Collidable) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Hero) Fire(fire bool, offsetX float64, offsetY float64) (bullet *weapons.Handgun) {
|
||||||
|
if fire {
|
||||||
|
bullet = weapons.NewHandgun()
|
||||||
|
bullet.X = h.X + (resources.HeroTileSize / 2) - offsetX
|
||||||
|
bullet.Y = h.Y + (resources.HeroTileSize / 2) - offsetY
|
||||||
|
bullet.OffsetX = offsetX
|
||||||
|
bullet.OffsetY = offsetY
|
||||||
|
bullet.Fire(h.direction)
|
||||||
|
return bullet
|
||||||
|
} else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package input
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -51,3 +52,7 @@ func (k *Keyboard) DirectionFromKeys(keys []ebiten.Key) configuration.Direction
|
|||||||
return configuration.PreviouslyHeld
|
return configuration.PreviouslyHeld
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (k *Keyboard) ShouldFire(keys []ebiten.Key) bool {
|
||||||
|
return inpututil.IsKeyJustReleased(ebiten.KeySpace)
|
||||||
|
}
|
||||||
|
|||||||
29
main.go
29
main.go
@@ -1,13 +1,16 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
|
"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/configuration"
|
||||||
"gitlab.com/kbr4/9heroja/hero"
|
"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/weapons"
|
||||||
"gitlab.com/kbr4/9heroja/zombie"
|
"gitlab.com/kbr4/9heroja/zombie"
|
||||||
_ "image/png"
|
_ "image/png"
|
||||||
"log"
|
"log"
|
||||||
@@ -53,18 +56,32 @@ type Game struct {
|
|||||||
terrain *terrain.Terrain
|
terrain *terrain.Terrain
|
||||||
zombies []*zombie.Zombie
|
zombies []*zombie.Zombie
|
||||||
world *collision.World
|
world *collision.World
|
||||||
|
bullets []*weapons.Handgun
|
||||||
}
|
}
|
||||||
|
|
||||||
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.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(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 {
|
if len(g.keys) <= 0 {
|
||||||
g.hero.Stop()
|
g.hero.Stop()
|
||||||
} else {
|
} else {
|
||||||
g.terrain.Move()
|
g.terrain.Move()
|
||||||
g.hero.Walk()
|
g.hero.Walk()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, b := range g.bullets {
|
||||||
|
if b.IsFlying {
|
||||||
|
b.Move()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
g.world.NotifyAboutCollisions()
|
g.world.NotifyAboutCollisions()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -76,7 +93,19 @@ func (g *Game) Draw(screen *ebiten.Image) {
|
|||||||
z.OffsetY = g.terrain.PositionY
|
z.OffsetY = g.terrain.PositionY
|
||||||
z.DrawZombie(screen)
|
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)
|
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) {
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
|
||||||
|
|||||||
@@ -13,4 +13,7 @@ var (
|
|||||||
|
|
||||||
//go:embed zombie3.png
|
//go:embed zombie3.png
|
||||||
Zombie_png []byte
|
Zombie_png []byte
|
||||||
|
|
||||||
|
//go:embed handgun.png
|
||||||
|
Handgun_png []byte
|
||||||
)
|
)
|
||||||
|
|||||||
BIN
resources/handgun.png
Normal file
BIN
resources/handgun.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 967 B |
134
weapons/handgun.go
Normal file
134
weapons/handgun.go
Normal file
@@ -0,0 +1,134 @@
|
|||||||
|
package weapons
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
|
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
||||||
|
"gitlab.com/kbr4/9heroja/collision"
|
||||||
|
"gitlab.com/kbr4/9heroja/configuration"
|
||||||
|
"gitlab.com/kbr4/9heroja/resources"
|
||||||
|
"image"
|
||||||
|
"log"
|
||||||
|
"slices"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const BulletSpeedMs = 300
|
||||||
|
|
||||||
|
var (
|
||||||
|
handgunBulletImage *ebiten.Image
|
||||||
|
)
|
||||||
|
|
||||||
|
type Handgun struct {
|
||||||
|
direction configuration.Direction
|
||||||
|
IsFlying bool
|
||||||
|
X float64
|
||||||
|
Y float64
|
||||||
|
OffsetX float64
|
||||||
|
OffsetY float64
|
||||||
|
IsDisintegrated bool
|
||||||
|
flyingTicker *time.Ticker
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handgun) DrawHandgunBullet(screen *ebiten.Image) {
|
||||||
|
|
||||||
|
screenWidth := screen.Bounds().Max.X
|
||||||
|
screenHeight := screen.Bounds().Max.Y
|
||||||
|
|
||||||
|
if h.X > float64(screenWidth) || h.X < 0 || h.Y > float64(screenHeight) || h.Y < 0 {
|
||||||
|
h.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
if !h.IsDisintegrated && h.IsFlying {
|
||||||
|
ebitenutil.DebugPrint(screen, fmt.Sprintf("\n\n\n\n\n\n\ndx: %f y:%f isint: %t, flying: %t", h.X, h.Y, h.IsDisintegrated, h.IsFlying))
|
||||||
|
op := &ebiten.DrawImageOptions{}
|
||||||
|
op.GeoM.Reset()
|
||||||
|
op.GeoM.Translate(h.X+h.OffsetX, h.Y+h.OffsetY)
|
||||||
|
op.GeoM.Scale(1, 1)
|
||||||
|
|
||||||
|
screen.DrawImage(
|
||||||
|
handgunBulletImage,
|
||||||
|
op,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
img, _, err := image.Decode(bytes.NewReader(resources.Handgun_png))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
handgunBulletImage = ebiten.NewImageFromImage(img)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHandgun() *Handgun {
|
||||||
|
handgun := &Handgun{
|
||||||
|
IsFlying: false,
|
||||||
|
IsDisintegrated: false,
|
||||||
|
}
|
||||||
|
return handgun
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handgun) Fire(direction configuration.Direction) {
|
||||||
|
h.direction = direction
|
||||||
|
h.flyingTicker = time.NewTicker(BulletSpeedMs * time.Millisecond)
|
||||||
|
if !h.IsFlying {
|
||||||
|
// h.flyingTicker.Reset(BulletSpeedMs * time.Millisecond)
|
||||||
|
h.IsFlying = true
|
||||||
|
h.IsDisintegrated = false
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handgun) Stop() {
|
||||||
|
h.IsFlying = false
|
||||||
|
h.IsDisintegrated = true
|
||||||
|
fmt.Println("Bullet stopped")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handgun) CollisionShape() collision.Polygon {
|
||||||
|
if !h.IsDisintegrated {
|
||||||
|
return collision.Polygon{
|
||||||
|
Points: []collision.Point{
|
||||||
|
{X: h.X + h.OffsetX, Y: h.Y + h.OffsetY},
|
||||||
|
{X: h.X + h.OffsetX + 4, Y: h.Y + h.OffsetY},
|
||||||
|
{X: h.X + h.OffsetX + 4, Y: h.Y + h.OffsetY + 4},
|
||||||
|
{X: h.X + h.OffsetX, Y: h.Y + h.OffsetY + 4},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return collision.Polygon{
|
||||||
|
Points: []collision.Point{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handgun) CollisionObjectType() collision.ObjectType {
|
||||||
|
return collision.Bullet
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handgun) HandleCollisionEvent(other collision.Collidable) {
|
||||||
|
coll := other.CollisionObjectType()
|
||||||
|
switch coll {
|
||||||
|
case collision.Zombie:
|
||||||
|
fmt.Println("Bullet hit zombie")
|
||||||
|
h.Stop()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func (h *Handgun) Move() {
|
||||||
|
if slices.Contains([]configuration.Direction{configuration.North, configuration.NorthEast, configuration.NorthWest}, h.direction) {
|
||||||
|
h.Y -= 1
|
||||||
|
}
|
||||||
|
if slices.Contains([]configuration.Direction{configuration.South, configuration.SouthEast, configuration.SouthWest}, h.direction) {
|
||||||
|
h.Y += 1
|
||||||
|
}
|
||||||
|
if slices.Contains([]configuration.Direction{configuration.East, configuration.NorthEast, configuration.SouthEast}, h.direction) {
|
||||||
|
h.X += 1
|
||||||
|
}
|
||||||
|
if slices.Contains([]configuration.Direction{configuration.West, configuration.NorthWest, configuration.SouthWest}, h.direction) {
|
||||||
|
h.X -= 1
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ package zombie
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"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"
|
||||||
@@ -147,6 +148,9 @@ func (z *Zombie) HandleCollisionEvent(other collision.Collidable) {
|
|||||||
case collision.Hero:
|
case collision.Hero:
|
||||||
z.Stop()
|
z.Stop()
|
||||||
z.IsDead = true
|
z.IsDead = true
|
||||||
|
case collision.Bullet:
|
||||||
|
fmt.Println("Zombie hit by bullet")
|
||||||
|
z.Stop()
|
||||||
|
z.IsDead = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user