Added weapon drops and collection. New laser item asset.

This commit is contained in:
2024-11-22 17:57:16 -05:00
parent b3a8ef8c0f
commit 253c708d45
6 changed files with 151 additions and 4 deletions

View File

@@ -35,6 +35,7 @@ type Canvas struct {
counter int
score int
splashes []*elements.Splash
wpdrops []*elements.WeaponDrop
hero *elements.Hero
charge *elements.Explosion
goblin *elements.FlyGoblin
@@ -82,6 +83,7 @@ func (c *Canvas) Update() error {
c.Initialize()
} else {
c.UpdateHero()
c.UpdateWeaponDrops()
c.UpdateWeapons()
c.UpdateProjectiles()
c.UpdateCharge()
@@ -90,6 +92,7 @@ func (c *Canvas) Update() error {
c.CleanupTargets()
c.UpdateSplashes()
c.CleanSplashes()
c.CleanupDrops()
c.counter++
}
@@ -135,6 +138,14 @@ func (c *Canvas) Draw(drawimg *ebiten.Image) {
}
}
//draw weapon drops
for _, drop := range c.wpdrops {
drop.Draw()
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(drop.GetPosition().X-float64(drop.Sprite.Bounds().Dx())/2, drop.GetPosition().Y-float64(drop.Sprite.Bounds().Dy())/2)
c.Sprite.DrawImage(drop.Sprite, op)
}
//draw enemies
for _, e := range c.enemies {
e.Draw()
@@ -212,6 +223,7 @@ func (c *Canvas) Initialize() {
c.InitializeHero()
c.CleanSplashes()
c.ResetWeaponDrops()
c.enemies = c.enemies[:0]
c.gameover = false
c.initialized = true
@@ -536,6 +548,16 @@ func (c *Canvas) CleanupTargets() {
// remove dead targets by iterating over all targets
i := 0
for _, e := range c.enemies {
//compute odds for dropping an item on dead enemies
if e.GetEnemyState() == elements.MoverActionDead {
if rand.Float64() > 0.98 {
drop := elements.NewWeaponDrop(gamedata.WeaponTypeLaser)
drop.SetPosition(e.GetPosition())
c.wpdrops = append(c.wpdrops, drop)
}
}
//moving valid targets to the front of the slice
if e.GetEnemyState() < elements.MoverActionDead &&
!(e.GetPosition().X < -640*2 || e.GetPosition().X > 640*2 ||
@@ -847,7 +869,7 @@ func (c *Canvas) LaserAttempt4() {
}
//fmt.Printf("%f \n", a)
if d <= 50 && e.GetEnemyState() <= gamedata.EnemyStateHit {
if d <= float64(e.GetSprite().Bounds().Dx()) && e.GetEnemyState() <= gamedata.EnemyStateHit {
if IsPixelColliding(c.laserMask, e.GetSprite(),
image.Pt(0, 0),
@@ -890,6 +912,22 @@ func (c *Canvas) CleanSplashes() {
c.splashes = c.splashes[:i]
}
func (c *Canvas) CleanupDrops() {
i := 0
for _, drop := range c.wpdrops {
if !drop.IsCollected() {
c.wpdrops[i] = drop
i++
}
}
for j := i; j < len(c.wpdrops); j++ {
c.wpdrops[j] = nil
}
c.wpdrops = c.wpdrops[:i]
}
func (c *Canvas) UpdateWeapons() {
if !c.gameover {
//check for weapon inputs
@@ -909,3 +947,41 @@ func (c *Canvas) UpdateWeapons() {
}
}
func (c *Canvas) UpdateWeaponDrops() {
//do we have any drops? let's calculate the chances
//
for _, drop := range c.wpdrops {
drop.Update()
//has the hero collided with any? add to holster
//boundary box collision check
if c.hero.Pos.X >= drop.GetPosition().X-float64(drop.Sprite.Bounds().Dx())/2 &&
c.hero.Pos.X <= drop.GetPosition().X+float64(drop.Sprite.Bounds().Dx())/2 &&
c.hero.Pos.Y >= drop.GetPosition().Y-float64(drop.Sprite.Bounds().Dy())/2 &&
c.hero.Pos.Y <= drop.GetPosition().Y+float64(drop.Sprite.Bounds().Dy())/2 {
//fmt.Println("hero trying to pick up weapon maybe")
if IsPixelColliding(
c.hero.Sprite,
drop.Sprite,
image.Pt(int(c.hero.Pos.X), int(c.hero.Pos.Y)),
image.Pt(int(drop.GetPosition().X), int(drop.GetPosition().Y)),
) {
fmt.Println("weapon acquired")
drop.SetCollected(true)
c.holster.AddWeapon(weapons.NewLaser())
}
}
}
}
func (c *Canvas) ResetWeaponDrops() {
for i := range c.wpdrops {
c.wpdrops[i] = nil
}
c.wpdrops = c.wpdrops[:0]
}