barra de vida shake

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

53
main.go
View file

@ -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 (hb *healthBar) update(buildings []*building) {
const margin = 20
buildingsLife := 0
maxCityLife := 3 * len(buildings)
for _, b := range buildings {
buildingsLife += b.life
func randomShake(amount float32) float32 {
return rand.Float32() * amount - amount/2
}
position := rl.NewVector2(margin, 10)
cityLife := (WIDTH-margin*2) * buildingsLife/maxCityLife
size := rl.NewVector2(float32(cityLife), 10)
func (hb *healthBar) update(buildings []*building) {
const MARGIN = 20
const BARWIDTH = (WIDTH - MARGIN*2)
hb.damageTaken += cityLife - hb.life
currentLife := 0
maxLife := buildingsHealth * len(buildings)
for _, b := range buildings {
currentLife += b.life
}
position := rl.NewVector2(MARGIN, 10)
barWidth := BARWIDTH * currentLife/maxLife
damageTaken := size
damageTaken.X += float32(hb.damageTaken)
size := rl.NewVector2(float32(barWidth), 10)
rl.DrawRectangleV(position, damageTaken, rl.Orange)
hb.damageTaken += float32(hb.life - currentLife)
damageTakenBar := size
damageTakenBar.X = BARWIDTH *
(float32(currentLife) + hb.damageTaken) / float32(maxLife)
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)
}
}