barra de vida shake

This commit is contained in:
silva guimaraes 2023-09-14 11:18:32 -03:00
parent 5ac9ce8814
commit 6d25ee261b

49
main.go
View file

@ -15,6 +15,7 @@ const (
maxColumns = 5 maxColumns = 5
enemyAcc = 0.01 enemyAcc = 0.01
maxSpeed = 0.1 maxSpeed = 0.1
buildingsHealth = 3
) )
type enemy struct { type enemy struct {
@ -36,7 +37,7 @@ type building struct {
type healthBar struct { type healthBar struct {
life int life int
damageTaken int damageTaken float32
lastUpdate float64 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) { func (hb *healthBar) update(buildings []*building) {
const margin = 20 const MARGIN = 20
buildingsLife := 0 const BARWIDTH = (WIDTH - MARGIN*2)
maxCityLife := 3 * len(buildings)
currentLife := 0
maxLife := buildingsHealth * len(buildings)
for _, b := range buildings { for _, b := range buildings {
buildingsLife += b.life currentLife += b.life
} }
position := rl.NewVector2(margin, 10) position := rl.NewVector2(MARGIN, 10)
cityLife := (WIDTH-margin*2) * buildingsLife/maxCityLife 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 damageTakenBar := size
damageTaken.X += float32(hb.damageTaken) 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) rl.DrawRectangleV(position, size, rl.Red)
hb.lastUpdate = rl.GetTime() hb.lastUpdate = rl.GetTime()
hb.life = currentLife
hb.damageTaken *= 0.95
} }
func newHealthBar(buildings []*building) *healthBar { 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 { if b.animate != nil {
return return
} }
b.animate = shakeBuilding(*b) b.animate = shakeBuilding(*b)
b.life -= 1 b.life -= damage
fmt.Println("damage:", b.life) fmt.Println("damage:", b.life)
} }
@ -113,7 +130,7 @@ func newBuilding(x, z float32) *building {
return &building{ return &building{
color: color, color: color,
life: 3, life: buildingsHealth,
pos: pos, pos: pos,
size: size, size: size,
boundingBox: rl.NewBoundingBox(min, max), boundingBox: rl.NewBoundingBox(min, max),
@ -224,7 +241,7 @@ func damage_building(camera rl.Camera, buildings []*building) {
} }
if closest != nil { if closest != nil {
closest.causeDamage() closest.causeDamage(3)
} }
} }