barra de vida
This commit is contained in:
parent
be34596f1d
commit
5ac9ce8814
1 changed files with 67 additions and 16 deletions
83
main.go
83
main.go
|
|
@ -34,6 +34,12 @@ type building struct {
|
|||
animate animationFunc
|
||||
}
|
||||
|
||||
type healthBar struct {
|
||||
life int
|
||||
damageTaken int
|
||||
lastUpdate float64
|
||||
}
|
||||
|
||||
func draw_plane() {
|
||||
rl.DrawPlane(
|
||||
rl.NewVector3(16, 0.0, 16),
|
||||
|
|
@ -42,6 +48,44 @@ 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
|
||||
}
|
||||
position := rl.NewVector2(margin, 10)
|
||||
cityLife := (WIDTH-margin*2) * buildingsLife/maxCityLife
|
||||
|
||||
size := rl.NewVector2(float32(cityLife), 10)
|
||||
|
||||
hb.damageTaken += cityLife - hb.life
|
||||
|
||||
damageTaken := size
|
||||
damageTaken.X += float32(hb.damageTaken)
|
||||
|
||||
rl.DrawRectangleV(position, damageTaken, rl.Orange)
|
||||
rl.DrawRectangleV(position, size, rl.Red)
|
||||
|
||||
hb.lastUpdate = rl.GetTime()
|
||||
}
|
||||
|
||||
func newHealthBar(buildings []*building) *healthBar {
|
||||
return &healthBar{
|
||||
life: 3 * len(buildings),
|
||||
}
|
||||
}
|
||||
|
||||
func (b *building) causeDamage() {
|
||||
if b.animate != nil {
|
||||
return
|
||||
}
|
||||
b.animate = shakeBuilding(*b)
|
||||
b.life -= 1
|
||||
fmt.Println("damage:", b.life)
|
||||
}
|
||||
|
||||
func newBuilding(x, z float32) *building {
|
||||
color := rl.NewColor(
|
||||
uint8(rl.GetRandomValue(0, 180)),
|
||||
|
|
@ -69,7 +113,7 @@ func newBuilding(x, z float32) *building {
|
|||
|
||||
return &building{
|
||||
color: color,
|
||||
life: 100,
|
||||
life: 3,
|
||||
pos: pos,
|
||||
size: size,
|
||||
boundingBox: rl.NewBoundingBox(min, max),
|
||||
|
|
@ -102,7 +146,7 @@ func shakeBuilding(b building) animationFunc {
|
|||
building.color = rl.RayWhite
|
||||
}
|
||||
|
||||
drawWireframe(building)
|
||||
drawWireframe(b)
|
||||
|
||||
shake := rl.NewVector3(
|
||||
rand.Float32() * .2 - .1,
|
||||
|
|
@ -122,15 +166,10 @@ func shakeBuilding(b building) animationFunc {
|
|||
}
|
||||
|
||||
|
||||
func draw_buildings(buildings []*building, selected *building) {
|
||||
func draw_buildings(buildings []*building) {
|
||||
for _, building := range buildings {
|
||||
|
||||
hasAnimation := building.animate != nil
|
||||
hit := building == selected && !hasAnimation
|
||||
|
||||
if !hit {
|
||||
building.animate = shakeBuilding(*building)
|
||||
}
|
||||
|
||||
if hasAnimation {
|
||||
|
||||
|
|
@ -143,26 +182,33 @@ func draw_buildings(buildings []*building, selected *building) {
|
|||
continue
|
||||
}
|
||||
|
||||
if building.life == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
drawWireframe(*building)
|
||||
rl.DrawCubeV(building.pos, building.size, building.color)
|
||||
}
|
||||
}
|
||||
|
||||
func demage_building(camera rl.Camera, buildings []*building) *building {
|
||||
if !rl.IsMouseButtonPressed(rl.MouseLeftButton) {
|
||||
return nil
|
||||
func damage_building(camera rl.Camera, buildings []*building) {
|
||||
if !rl.IsMouseButtonDown(rl.MouseLeftButton) {
|
||||
return
|
||||
}
|
||||
ray := rl.Ray{
|
||||
Position: camera.Position,
|
||||
Direction: rl.Vector3Subtract(camera.Target, camera.Position),
|
||||
}
|
||||
|
||||
// var collision rl.RayCollision
|
||||
closestDistance := math.Inf(1)
|
||||
var closest *building
|
||||
|
||||
for _, building := range buildings {
|
||||
|
||||
if building.life == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
playerDistance := rl.Vector3Distance(camera.Position, building.pos)
|
||||
|
||||
if playerDistance < float32(closestDistance) {
|
||||
|
|
@ -176,7 +222,10 @@ func demage_building(camera rl.Camera, buildings []*building) *building {
|
|||
}
|
||||
}
|
||||
}
|
||||
return closest
|
||||
|
||||
if closest != nil {
|
||||
closest.causeDamage()
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
|
@ -214,6 +263,7 @@ func main() {
|
|||
}
|
||||
}
|
||||
|
||||
healthBar := newHealthBar(buildings)
|
||||
|
||||
for !rl.WindowShouldClose() {
|
||||
|
||||
|
|
@ -232,11 +282,11 @@ func main() {
|
|||
rl.BeginMode3D(camera)
|
||||
{
|
||||
|
||||
selected := demage_building(camera, buildings)
|
||||
damage_building(camera, buildings)
|
||||
|
||||
draw_plane()
|
||||
|
||||
draw_buildings(buildings, selected)
|
||||
draw_buildings(buildings)
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -253,8 +303,9 @@ func main() {
|
|||
rl.Red,
|
||||
)
|
||||
|
||||
rl.DrawFPS(10, 10)
|
||||
rl.DrawFPS(0, HEIGHT-30)
|
||||
|
||||
healthBar.update(buildings)
|
||||
|
||||
rl.EndDrawing()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue