package weapons import ( "bytes" "fmt" "github.com/hajimehoshi/ebiten/v2" "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 OffsetXAtStart float64 OffsetYAtStart 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 { op := &ebiten.DrawImageOptions{} op.GeoM.Reset() op.GeoM.Translate(h.X+h.OffsetX-h.OffsetXAtStart, h.Y+h.OffsetY-h.OffsetYAtStart) op.GeoM.Scale(1, 1) op.ColorScale.Scale(1, 1, 1, 1) // draw circle 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 - 3 - h.OffsetXAtStart, Y: h.Y + h.OffsetY - 3 - h.OffsetYAtStart}, {X: h.X + h.OffsetX + 7 - h.OffsetXAtStart, Y: h.Y + h.OffsetY - 3 - h.OffsetYAtStart}, {X: h.X + h.OffsetX + 7 - h.OffsetXAtStart, Y: h.Y + h.OffsetY + 7 - h.OffsetYAtStart}, {X: h.X + h.OffsetX - 3 - h.OffsetXAtStart, Y: h.Y + h.OffsetY + 7 - h.OffsetYAtStart}, }, } } 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 -= 6 } if slices.Contains([]configuration.Direction{configuration.South, configuration.SouthEast, configuration.SouthWest}, h.direction) { h.Y += 6 } if slices.Contains([]configuration.Direction{configuration.East, configuration.NorthEast, configuration.SouthEast}, h.direction) { h.X += 6 } if slices.Contains([]configuration.Direction{configuration.West, configuration.NorthWest, configuration.SouthWest}, h.direction) { h.X -= 6 } }