Refactor to use better interfaces and event callbacks.

This commit is contained in:
2024-11-15 16:11:45 -05:00
parent 4ced75d66c
commit cbc4ba5eb3
16 changed files with 1009 additions and 14 deletions

View File

@@ -16,6 +16,8 @@ type Boss struct {
Spawned bool
Pos gamedata.Coordinates
Right bool
Health int
Touched bool
damage bool
cycle int
Action MoverAction
@@ -32,6 +34,8 @@ func NewBoss() *Boss {
hitcount: 0,
Maks: ebiten.NewImage(96, 96),
MaskDest: ebiten.NewImage(96, 96),
Health: 100,
Touched: false,
}
b.Maks.Fill(color.White)
return b
@@ -45,6 +49,9 @@ func (b *Boss) Update() {
b.damageduration = 0
}
}
if b.Action == MoverActionDead {
b.Spawned = false
}
b.cycle++
}
@@ -71,15 +78,17 @@ func (b *Boss) Draw() {
switch b.Action {
case MoverActionDefault:
b.Sprite.DrawImage(assets.ImageBank[assets.Worm].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), op)
case MoverActionDamaged:
if (b.cycle/5)%2 == 0 && b.damage {
b.MaskDest.DrawImage(assets.ImageBank[assets.Worm].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), op)
b.MaskDest.DrawImage(assets.ImageBank[assets.WormDamaged].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), op)
op := &ebiten.DrawImageOptions{}
op.GeoM.Reset()
op.Blend = ebiten.BlendSourceAtop
b.MaskDest.DrawImage(b.Maks, op)
b.Sprite.DrawImage(b.MaskDest, nil)
} else {
b.Sprite.DrawImage(assets.ImageBank[assets.Worm].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), op)
b.Sprite.DrawImage(assets.ImageBank[assets.WormDamaged].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), op)
}
case MoverActionExploding:
op.GeoM.Scale(2, 2)
@@ -95,7 +104,9 @@ func (b *Boss) Draw() {
func (b *Boss) SetHit() {
b.hitcount++
b.damage = true
if b.hitcount > 10 {
b.Health--
if b.Health <= 0 {
b.Action = MoverActionExploding
b.cycle = 0
}
@@ -107,4 +118,13 @@ func (b *Boss) Reset() {
b.damageduration = 0
b.Action = MoverActionDefault
b.Spawned = false
b.Health = 100
}
func (b *Boss) ToggleColor() {
if b.Action == MoverActionDefault {
b.Action = MoverActionDamaged
} else if b.Action == MoverActionDamaged {
b.Action = MoverActionDefault
}
}