This commit is contained in:
silva guimaraes 2025-08-23 19:53:45 -03:00
parent 2c2eaf9507
commit 730e697258

63
main.go
View file

@ -4,6 +4,8 @@ import (
"fmt"
"math"
"math/rand"
"slices"
rl "github.com/gen2brain/raylib-go/raylib"
)
@ -25,7 +27,7 @@ type enemy struct {
}
type remainingFrames int
type animationFunc func()remainingFrames
type animationFunc func() remainingFrames
type building struct {
pos rl.Vector3
@ -76,11 +78,16 @@ type emitter struct {
var city *scene
func update_emitters() {
for i, e := range city.emitters {
if remainingParticles := e.update(); remainingParticles == 0 {
city.emitters[i] = city.emitters[len(city.emitters)-1]
city.emitters = city.emitters[:len(city.emitters)-1]
for i := 0; i < len(city.emitters); i++ {
e := city.emitters[i]
remainingParticles := e.update()
if remainingParticles == 0 {
city.emitters = slices.Delete(city.emitters, i, i+1)
i--
}
}
}
@ -92,12 +99,12 @@ func newEmitter(pos rl.Vector3, particleAmount int) *emitter {
for i := 0; i < particleAmount; i++ {
direction := rl.NewVector3(
rand.Float32()*2 - 1,
rand.Float32()*2-1,
1,
rand.Float32()*2 - 1,
rand.Float32()*2-1,
)
direction = rl.Vector3Normalize(direction)
direction = rl.Vector3Scale(direction, 0.3 + rand.Float32()/3)
direction = rl.Vector3Scale(direction, 0.3+rand.Float32()/3)
particle := particle{
lifetime: rl.GetTime() + 2,
speed: direction,
@ -168,7 +175,6 @@ func update_projectiles() {
}
}
func (c *character) kill_aurea() {
const min = 10
for _, b := range city.buildings {
@ -178,7 +184,7 @@ func (c *character) kill_aurea() {
}
}
func (c *character) throw_bomb(){
func (c *character) throw_bomb() {
if !rl.IsMouseButtonPressed(rl.MouseRightButton) {
return
}
@ -195,9 +201,9 @@ func (c *character) throw_bomb(){
func (c *character) update() {
shake := rl.NewVector3(
randomShake(0.003) * city.healthBar.damageTaken,
randomShake(0.003) * city.healthBar.damageTaken,
randomShake(0.003) * city.healthBar.damageTaken,
randomShake(0.003)*city.healthBar.damageTaken,
randomShake(0.003)*city.healthBar.damageTaken,
randomShake(0.003)*city.healthBar.damageTaken,
)
c.camera.Target = rl.Vector3Add(c.camera.Target, shake)
@ -232,7 +238,7 @@ func draw_plane() {
}
func randomShake(amount float32) float32 {
return rand.Float32() * amount - amount/2
return rand.Float32()*amount - amount/2
}
func (hb *healthBar) update() {
@ -245,7 +251,7 @@ func (hb *healthBar) update() {
currentLife += b.life
}
position := rl.NewVector2(MARGIN, 10)
barWidth := BARWIDTH * currentLife/maxLife
barWidth := BARWIDTH * currentLife / maxLife
size := rl.NewVector2(float32(barWidth), 10)
@ -256,8 +262,8 @@ func (hb *healthBar) update() {
(float32(currentLife) + hb.damageTaken) / float32(maxLife)
shake := rl.NewVector2(
randomShake(0.4) * hb.damageTaken,
randomShake(0.4) * hb.damageTaken,
randomShake(0.4)*hb.damageTaken,
randomShake(0.4)*hb.damageTaken,
)
position = rl.Vector2Add(position, shake)
@ -292,21 +298,21 @@ func newBuilding(x, z float32) *building {
255,
)
pos := rl.NewVector3(x * buildingSpacing, 0, z * buildingSpacing)
pos := rl.NewVector3(x*buildingSpacing, 0, z*buildingSpacing)
size := rl.NewVector3(
buildingSize,
float32(rl.GetRandomValue(1, maxBuildingHeight)),
buildingSize,
)
min := rl.NewVector3(
pos.X - buildingSize/2,
pos.X-buildingSize/2,
0,
pos.Z - buildingSize/2,
pos.Z-buildingSize/2,
)
max := rl.NewVector3(
pos.X + buildingSize/2,
pos.X+buildingSize/2,
size.Y/2,
pos.Z + buildingSize/2,
pos.Z+buildingSize/2,
)
return &building{
@ -318,7 +324,6 @@ func newBuilding(x, z float32) *building {
}
}
func drawWireframe(building building) {
increasedSize := rl.Vector3Scale(building.size, 1.05)
@ -340,7 +345,7 @@ func shakeBuilding(b *building) animationFunc {
building := *b
if frame % 4 == 0 {
if frame%4 == 0 {
if b.life == 0 {
building.color = rl.NewColor(255, 0, 0, 255)
} else {
@ -351,9 +356,9 @@ func shakeBuilding(b *building) animationFunc {
drawWireframe(building)
shake := rl.NewVector3(
rand.Float32() * .2 - .1,
rand.Float32() * .1 - .05,
rand.Float32() * .2 - .1,
rand.Float32()*.2-.1,
rand.Float32()*.1-.05,
rand.Float32()*.2-.1,
)
// shake = rl.Vector3Scale(shake, float32(math.Sin(rl.GetTime()) + 1)/3)
rl.DrawCubeV(
@ -367,7 +372,6 @@ func shakeBuilding(b *building) animationFunc {
}
}
func draw_buildings() {
for _, building := range city.buildings {
@ -466,7 +470,6 @@ func main() {
for !rl.WindowShouldClose() {
rl.UpdateCamera(&character.camera, rl.CameraFirstPerson)
rl.BeginDrawing()
rl.ClearBackground(rl.LightGray)
@ -483,8 +486,6 @@ func main() {
update_emitters()
// direction = rl.Vector3Subtract(
// character.camera.Target, character.camera.Position)
// aim = rl.Vector3Lerp(direction, aim, 0.95)