Installer/background.go

57 lines
1.7 KiB
Go
Raw Normal View History

2024-12-20 18:58:53 +01:00
package main
import (
"math/rand"
"time"
rl "github.com/gen2brain/raylib-go/raylib"
)
type Particle struct {
Pos rl.Vector2
Vel rl.Vector2
Size float32
}
var particles []Particle
var topColor = rl.Color{R: 0x20, G: 0x0F, B: 0x3C, A: 0xFF} // #200F3C top
var bottomColor = rl.Color{R: 0x3B, G: 0x0B, B: 0x42, A: 0xFF} // #3B0B42 bottom
var particleColor = rl.Color{R: 0xD4, G: 0xB0, B: 0xB5, A: 0x80} // D4B0B5 with some transparency
func InitBackground(width, height int) {
rand.Seed(time.Now().UnixNano())
particles = make([]Particle, 100)
for i := range particles {
particles[i].Pos = rl.Vector2{X: float32(rand.Intn(width)), Y: float32(rand.Intn(height))}
particles[i].Vel = rl.Vector2{X: (rand.Float32() - 0.5) * 0.2, Y: (rand.Float32() - 0.5) * 0.2}
particles[i].Size = rand.Float32()*1.5 + 0.5 // Particles size ~0.5-2.0
}
}
func UpdateBackground(screenWidth, screenHeight int) {
for i := range particles {
particles[i].Pos.X += particles[i].Vel.X
particles[i].Pos.Y += particles[i].Vel.Y
// Wrap around screen
if particles[i].Pos.X < 0 {
particles[i].Pos.X += float32(screenWidth)
} else if particles[i].Pos.X > float32(screenWidth) {
particles[i].Pos.X -= float32(screenWidth)
}
if particles[i].Pos.Y < 0 {
particles[i].Pos.Y += float32(screenHeight)
} else if particles[i].Pos.Y > float32(screenHeight) {
particles[i].Pos.Y -= float32(screenHeight)
}
}
}
func DrawBackground(screenWidth, screenHeight int) {
// Draw vertical gradient
rl.DrawRectangleGradientV(0, 0, int32(screenWidth), int32(screenHeight), topColor, bottomColor)
// Draw particles
for _, p := range particles {
rl.DrawCircleV(p.Pos, p.Size, particleColor)
}
}