xels/simulation/simulation.go

35 lines
810 B
Go
Raw Permalink Normal View History

2025-08-21 18:16:10 +02:00
package simulation
import (
"math/rand/v2"
)
type Simulation struct {
StepsPerUpdate int
2025-08-21 18:16:10 +02:00
Grid *XelGrid
}
func (sim *Simulation) Update() {
for range sim.StepsPerUpdate {
2025-08-21 18:16:10 +02:00
// get all available xels with energy != 0
available_positions := []Vector2{}
for i, xel := range sim.Grid.Xels {
if xel.Energy != 0 {
available_positions = append(available_positions, Vector2{X: i % sim.Grid.Width, Y: i/sim.Grid.Height})
}
}
// choose random pos
pos := available_positions[rand.IntN(len(available_positions))]
xel := sim.Grid.GetXel(pos)
if xel.Energy == 0 { panic("xel energy null") }
xel.Step(pos, sim.Grid)
}
}
func NewSimulation(width int, height int, stepsPerUpdate int) *Simulation {
2025-08-21 18:16:10 +02:00
return &Simulation{
Grid: NewXelGrid(width, height),
StepsPerUpdate: stepsPerUpdate,
2025-08-21 18:16:10 +02:00
}
}