135 lines
3.1 KiB
Go
135 lines
3.1 KiB
Go
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
|
|
}
|
|
|
|
}
|