Automatic shooting

This commit is contained in:
2024-01-04 05:57:21 +01:00
parent d336fa4d15
commit 156e3ed43c
5 changed files with 51 additions and 35 deletions

View File

@@ -14,10 +14,12 @@ import (
)
const WalkSpeedMs = 300
const ReloadSpeedMs = 1000
var (
heroImage *ebiten.Image
walkTicker *time.Ticker
heroImage *ebiten.Image
walkTicker *time.Ticker
reloadTicker *time.Ticker
)
type Hero struct {
@@ -27,6 +29,7 @@ type Hero struct {
Health int // Health as a percentage (0-100)
X float64
Y float64
gunLoaded bool
}
type spriteFramePosition struct {
@@ -178,6 +181,7 @@ func init() {
}
heroImage = ebiten.NewImageFromImage(img)
walkTicker = time.NewTicker(WalkSpeedMs * time.Millisecond)
reloadTicker = time.NewTicker(ReloadSpeedMs * time.Millisecond)
// go routine that runs on every tick and increases step
}
@@ -187,6 +191,7 @@ func NewHero() *Hero {
step: 0,
IsWalking: false,
Health: 100,
gunLoaded: true,
}
go func() {
@@ -200,6 +205,15 @@ func NewHero() *Hero {
}
}
}()
go func() {
for {
select {
case <-reloadTicker.C:
hero.gunLoaded = true
}
}
}()
return hero
}
@@ -224,6 +238,11 @@ func (h *Hero) Stop() {
h.IsWalking = false
}
func (h *Hero) StopShooting() {
h.gunLoaded = false
reloadTicker.Stop()
}
func (h *Hero) CollisionShape() collision.Polygon {
return collision.Polygon{
Points: []collision.Point{
@@ -250,13 +269,15 @@ func (h *Hero) HandleCollisionEvent(other collision.Collidable) {
}
}
func (h *Hero) Fire(fire bool, offsetX float64, offsetY float64) (bullet *weapons.Handgun) {
if fire {
func (h *Hero) Fire(offsetX float64, offsetY float64) (bullet *weapons.Handgun) {
if h.gunLoaded {
h.gunLoaded = false
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.X = h.X + (resources.HeroTileSize / 2)
bullet.Y = h.Y + (resources.HeroTileSize / 2)
bullet.OffsetXAtStart = offsetX
bullet.OffsetYAtStart = offsetY
bullet.Fire(h.direction)
return bullet
} else {