From 6d25ee261b04d625a5951534841125465c0ee542 Mon Sep 17 00:00:00 2001 From: silva guimaraes Date: Thu, 14 Sep 2023 11:18:32 -0300 Subject: [PATCH] barra de vida shake --- main.go | 49 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/main.go b/main.go index 29c4a6e..af5c391 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ const ( maxColumns = 5 enemyAcc = 0.01 maxSpeed = 0.1 + buildingsHealth = 3 ) type enemy struct { @@ -36,7 +37,7 @@ type building struct { type healthBar struct { life int - damageTaken int + damageTaken float32 lastUpdate float64 } @@ -48,27 +49,43 @@ func draw_plane() { ) } +func randomShake(amount float32) float32 { + return rand.Float32() * amount - amount/2 +} + func (hb *healthBar) update(buildings []*building) { - const margin = 20 - buildingsLife := 0 - maxCityLife := 3 * len(buildings) + const MARGIN = 20 + const BARWIDTH = (WIDTH - MARGIN*2) + + currentLife := 0 + maxLife := buildingsHealth * len(buildings) for _, b := range buildings { - buildingsLife += b.life + currentLife += b.life } - position := rl.NewVector2(margin, 10) - cityLife := (WIDTH-margin*2) * buildingsLife/maxCityLife + position := rl.NewVector2(MARGIN, 10) + barWidth := BARWIDTH * currentLife/maxLife - size := rl.NewVector2(float32(cityLife), 10) + size := rl.NewVector2(float32(barWidth), 10) - hb.damageTaken += cityLife - hb.life + hb.damageTaken += float32(hb.life - currentLife) - damageTaken := size - damageTaken.X += float32(hb.damageTaken) + damageTakenBar := size + damageTakenBar.X = BARWIDTH * + (float32(currentLife) + hb.damageTaken) / float32(maxLife) - rl.DrawRectangleV(position, damageTaken, rl.Orange) + shake := rl.NewVector2( + randomShake(0.4) * hb.damageTaken, + randomShake(0.4) * hb.damageTaken, + ) + position = rl.Vector2Add(position, shake) + + rl.DrawRectangleV(position, damageTakenBar, rl.Orange) rl.DrawRectangleV(position, size, rl.Red) hb.lastUpdate = rl.GetTime() + hb.life = currentLife + hb.damageTaken *= 0.95 + } func newHealthBar(buildings []*building) *healthBar { @@ -77,12 +94,12 @@ func newHealthBar(buildings []*building) *healthBar { } } -func (b *building) causeDamage() { +func (b *building) causeDamage(damage int) { if b.animate != nil { return } b.animate = shakeBuilding(*b) - b.life -= 1 + b.life -= damage fmt.Println("damage:", b.life) } @@ -113,7 +130,7 @@ func newBuilding(x, z float32) *building { return &building{ color: color, - life: 3, + life: buildingsHealth, pos: pos, size: size, boundingBox: rl.NewBoundingBox(min, max), @@ -224,7 +241,7 @@ func damage_building(camera rl.Camera, buildings []*building) { } if closest != nil { - closest.causeDamage() + closest.causeDamage(3) } }