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

@@ -62,6 +62,7 @@ func NewGame() *Game {
musicInitialized: false,
boss: elements.NewBoss(),
}
return g
}
@@ -195,6 +196,14 @@ func (g *Game) Draw(screen *ebiten.Image) {
op.GeoM.Translate(-MOVER_WIDTH, -MOVER_HEIGHT)
op.GeoM.Translate(g.boss.Pos.X, g.boss.Pos.Y)
screen.DrawImage(g.boss.Sprite, op)
//text.Draw(screen, fmt.Sprintf("%d", g.boss.Health), fonts.SurviveFont.Arcade, 100, 50, color.White)
//boss health bar
x0 := g.boss.Pos.X - 96
y0 := g.boss.Pos.Y - 60
vector.DrawFilledRect(screen, float32(x0), float32(y0), 204, 12, color.Black, true)
vector.DrawFilledRect(screen, float32(x0+2), float32(y0+2), float32(g.boss.Health)*2, 8, color.RGBA{R: 0xff, G: 0x00, B: 0x00, A: 0xff}, true)
}
g.projectileMask.Clear()
@@ -280,14 +289,15 @@ func (g *Game) StepGame() {
//append new projectiles
g.AppendProjectiles()
//add new target with increasing frequency
g.SpawnEnemies()
//handle pulsewave updates
g.HandlePulseWaveUpdate()
if !g.boss.Spawned && g.counter > 600 {
g.SpawnBoss()
if !g.boss.Spawned {
//add new target with increasing frequency
g.SpawnEnemies()
if g.counter > 2000 {
g.SpawnBoss()
}
}
}
@@ -346,6 +356,20 @@ func (g *Game) HandlePulseWaveUpdate() {
//target.SetHit()
}
}
//check for boss
if g.boss.Spawned {
dx := g.boss.Pos.X - g.hero.Pos.X
dy := g.boss.Pos.Y - g.hero.Pos.Y
r := math.Sqrt(dx*dx + dy*dy)
if r >= g.explosion.Radius-40 && r <= g.explosion.Radius+40 &&
g.boss.Action <= elements.MoverActionDamaged && !g.boss.Touched {
g.boss.ToggleColor()
g.boss.Touched = true
//target.SetHit()
}
}
}
}
@@ -393,10 +417,10 @@ func (g *Game) UpdateProjectiles() {
}
}
//boss check first, boundary check
//boss check: first, boundary check
if p.Pos.X >= g.boss.Pos.X-MOVER_WIDTH && p.Pos.X <= g.boss.Pos.X+MOVER_WIDTH &&
p.Pos.Y >= g.boss.Pos.Y-MOVER_HEIGHT && p.Pos.Y <= g.boss.Pos.Y+MOVER_HEIGHT &&
g.boss.Action < elements.MoverActionDying {
g.boss.Action == elements.MoverActionDamaged {
//fmt.Println("potential collision")
//the following computes total collisions in the image using a projectile mask that is a duplicate of what is on screen
@@ -476,6 +500,7 @@ func (g *Game) ResetTargetTouches() {
for _, t := range g.targets {
t.Touched = false
}
g.boss.Touched = false
}
func (g *Game) AppendProjectiles() {
@@ -636,6 +661,7 @@ func (g *Game) UpdateBoss() {
g.boss.Update()
if g.boss.Action == elements.MoverActionExploding && !g.boss.SplodeInitiated {
g.score += 10
player := audioContext.NewPlayerFromBytes(assets.Splode)
player.Play()
g.boss.SplodeInitiated = true
@@ -697,3 +723,7 @@ func (g *Game) HasCollided(mask *ebiten.Image, size int) bool {
}
return result
}
func (g *Game) SetInputs(gamedata.GameInputs) {
}