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
|
animate animationFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type healthBar struct {
|
||||||
|
life int
|
||||||
|
damageTaken int
|
||||||
|
lastUpdate float64
|
||||||
|
}
|
||||||
|
|
||||||
func draw_plane() {
|
func draw_plane() {
|
||||||
rl.DrawPlane(
|
rl.DrawPlane(
|
||||||
rl.NewVector3(16, 0.0, 16),
|
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 {
|
func newBuilding(x, z float32) *building {
|
||||||
color := rl.NewColor(
|
color := rl.NewColor(
|
||||||
uint8(rl.GetRandomValue(0, 180)),
|
uint8(rl.GetRandomValue(0, 180)),
|
||||||
|
|
@ -69,7 +113,7 @@ func newBuilding(x, z float32) *building {
|
||||||
|
|
||||||
return &building{
|
return &building{
|
||||||
color: color,
|
color: color,
|
||||||
life: 100,
|
life: 3,
|
||||||
pos: pos,
|
pos: pos,
|
||||||
size: size,
|
size: size,
|
||||||
boundingBox: rl.NewBoundingBox(min, max),
|
boundingBox: rl.NewBoundingBox(min, max),
|
||||||
|
|
@ -102,7 +146,7 @@ func shakeBuilding(b building) animationFunc {
|
||||||
building.color = rl.RayWhite
|
building.color = rl.RayWhite
|
||||||
}
|
}
|
||||||
|
|
||||||
drawWireframe(building)
|
drawWireframe(b)
|
||||||
|
|
||||||
shake := rl.NewVector3(
|
shake := rl.NewVector3(
|
||||||
rand.Float32() * .2 - .1,
|
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 {
|
for _, building := range buildings {
|
||||||
|
|
||||||
hasAnimation := building.animate != nil
|
hasAnimation := building.animate != nil
|
||||||
hit := building == selected && !hasAnimation
|
|
||||||
|
|
||||||
if !hit {
|
|
||||||
building.animate = shakeBuilding(*building)
|
|
||||||
}
|
|
||||||
|
|
||||||
if hasAnimation {
|
if hasAnimation {
|
||||||
|
|
||||||
|
|
@ -143,26 +182,33 @@ func draw_buildings(buildings []*building, selected *building) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if building.life == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
drawWireframe(*building)
|
drawWireframe(*building)
|
||||||
rl.DrawCubeV(building.pos, building.size, building.color)
|
rl.DrawCubeV(building.pos, building.size, building.color)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func demage_building(camera rl.Camera, buildings []*building) *building {
|
func damage_building(camera rl.Camera, buildings []*building) {
|
||||||
if !rl.IsMouseButtonPressed(rl.MouseLeftButton) {
|
if !rl.IsMouseButtonDown(rl.MouseLeftButton) {
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
ray := rl.Ray{
|
ray := rl.Ray{
|
||||||
Position: camera.Position,
|
Position: camera.Position,
|
||||||
Direction: rl.Vector3Subtract(camera.Target, camera.Position),
|
Direction: rl.Vector3Subtract(camera.Target, camera.Position),
|
||||||
}
|
}
|
||||||
|
|
||||||
// var collision rl.RayCollision
|
|
||||||
closestDistance := math.Inf(1)
|
closestDistance := math.Inf(1)
|
||||||
var closest *building
|
var closest *building
|
||||||
|
|
||||||
for _, building := range buildings {
|
for _, building := range buildings {
|
||||||
|
|
||||||
|
if building.life == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
playerDistance := rl.Vector3Distance(camera.Position, building.pos)
|
playerDistance := rl.Vector3Distance(camera.Position, building.pos)
|
||||||
|
|
||||||
if playerDistance < float32(closestDistance) {
|
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() {
|
func main() {
|
||||||
|
|
@ -214,6 +263,7 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
healthBar := newHealthBar(buildings)
|
||||||
|
|
||||||
for !rl.WindowShouldClose() {
|
for !rl.WindowShouldClose() {
|
||||||
|
|
||||||
|
|
@ -232,11 +282,11 @@ func main() {
|
||||||
rl.BeginMode3D(camera)
|
rl.BeginMode3D(camera)
|
||||||
{
|
{
|
||||||
|
|
||||||
selected := demage_building(camera, buildings)
|
damage_building(camera, buildings)
|
||||||
|
|
||||||
draw_plane()
|
draw_plane()
|
||||||
|
|
||||||
draw_buildings(buildings, selected)
|
draw_buildings(buildings)
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -253,8 +303,9 @@ func main() {
|
||||||
rl.Red,
|
rl.Red,
|
||||||
)
|
)
|
||||||
|
|
||||||
rl.DrawFPS(10, 10)
|
rl.DrawFPS(0, HEIGHT-30)
|
||||||
|
|
||||||
|
healthBar.update(buildings)
|
||||||
|
|
||||||
rl.EndDrawing()
|
rl.EndDrawing()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue