2020-07-01 14:29:58 +03:00
|
|
|
// Copyright(C) 2019 Lars Pontoppidan. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license file distributed with this software package
|
|
|
|
module particle
|
2020-07-02 15:31:42 +03:00
|
|
|
|
2022-12-27 16:19:08 +03:00
|
|
|
import math.vec
|
2020-07-01 14:29:58 +03:00
|
|
|
import rand
|
|
|
|
import sokol.sgl
|
|
|
|
|
|
|
|
pub struct SystemConfig {
|
2020-07-02 15:31:42 +03:00
|
|
|
pool int
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct System {
|
2020-07-02 15:31:42 +03:00
|
|
|
width int
|
|
|
|
height int
|
2020-07-01 14:29:58 +03:00
|
|
|
mut:
|
2021-04-20 17:16:35 +03:00
|
|
|
pool []&Particle
|
|
|
|
bin []&Particle
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut s System) init(sc SystemConfig) {
|
2022-03-25 00:37:13 +03:00
|
|
|
unsafe { s.pool.flags.set(.noslices | .noshrink) }
|
|
|
|
unsafe { s.bin.flags.set(.noslices | .noshrink) }
|
2020-07-01 14:29:58 +03:00
|
|
|
for i := 0; i < sc.pool; i++ {
|
2022-12-27 16:19:08 +03:00
|
|
|
p := new(vec.Vec2[f64]{f32(s.width) * 0.5, f32(s.height) * 0.5})
|
2020-10-13 13:16:51 +03:00
|
|
|
s.bin << p
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut s System) update(dt f64) {
|
2023-04-13 08:38:21 +03:00
|
|
|
mut p := &Particle(unsafe { nil })
|
2022-03-25 00:37:13 +03:00
|
|
|
mut moved := 0
|
2020-07-01 14:29:58 +03:00
|
|
|
for i := 0; i < s.pool.len; i++ {
|
|
|
|
p = s.pool[i]
|
|
|
|
p.update(dt)
|
|
|
|
if p.is_dead() {
|
|
|
|
s.bin << p
|
|
|
|
s.pool.delete(i)
|
2022-03-25 00:37:13 +03:00
|
|
|
moved++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$if trace_moves_spool_to_sbin ? {
|
|
|
|
if moved != 0 {
|
|
|
|
eprintln('${moved:4} particles s.pool -> s.bin')
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (s System) draw() {
|
|
|
|
sgl.begin_quads()
|
|
|
|
for p in s.pool {
|
|
|
|
p.draw()
|
|
|
|
}
|
|
|
|
sgl.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut s System) reset() {
|
2020-09-28 07:14:15 +03:00
|
|
|
for i in 0 .. s.pool.len {
|
2020-07-24 12:34:19 +03:00
|
|
|
mut p := s.pool[i]
|
2020-07-01 14:29:58 +03:00
|
|
|
p.reset()
|
|
|
|
p.life_time = 0
|
|
|
|
}
|
2020-09-28 07:14:15 +03:00
|
|
|
for i in 0 .. s.bin.len {
|
2020-07-24 12:34:19 +03:00
|
|
|
mut p := s.pool[i]
|
|
|
|
p.reset()
|
|
|
|
p.life_time = 0
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-18 13:45:13 +03:00
|
|
|
pub fn (mut s System) explode(x f32, y f32) {
|
2020-07-01 14:29:58 +03:00
|
|
|
mut reserve := 500
|
2022-12-27 16:19:08 +03:00
|
|
|
center := vec.Vec2[f64]{x, y}
|
2023-04-13 08:38:21 +03:00
|
|
|
mut p := &Particle(unsafe { nil })
|
2022-03-25 00:37:13 +03:00
|
|
|
mut moved := 0
|
2020-07-01 14:29:58 +03:00
|
|
|
for i := 0; i < s.bin.len && reserve > 0; i++ {
|
|
|
|
p = s.bin[i]
|
|
|
|
p.reset()
|
|
|
|
p.location.from(center)
|
2022-12-27 16:19:08 +03:00
|
|
|
p.acceleration = vec.Vec2[f64]{rand.f32_in_range(-0.5, 0.5) or { -0.5 }, rand.f32_in_range(-0.5,
|
2022-02-23 13:36:14 +03:00
|
|
|
0.5) or { -0.5 }}
|
2022-12-27 16:19:08 +03:00
|
|
|
p.velocity = vec.Vec2[f64]{rand.f32_in_range(-0.5, 0.5) or { -0.5 }, rand.f32_in_range(-0.5,
|
2022-02-23 13:36:14 +03:00
|
|
|
0.5) or { -0.5 }}
|
|
|
|
p.life_time = rand.f64_in_range(500, 2000) or { 500 }
|
2020-07-01 14:29:58 +03:00
|
|
|
s.pool << p
|
|
|
|
s.bin.delete(i)
|
2022-03-25 00:37:13 +03:00
|
|
|
moved++
|
2020-07-01 14:29:58 +03:00
|
|
|
reserve--
|
|
|
|
}
|
2022-03-25 00:37:13 +03:00
|
|
|
$if trace_moves_sbin_to_spool ? {
|
|
|
|
if moved != 0 {
|
|
|
|
eprintln('${moved:4} particles s.bin -> s.pool')
|
|
|
|
}
|
|
|
|
}
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut s System) free() {
|
|
|
|
for p in s.pool {
|
2022-05-20 18:30:16 +03:00
|
|
|
if unsafe { p == 0 } {
|
2020-07-02 15:31:42 +03:00
|
|
|
print(ptr_str(p) + ' ouch')
|
2020-07-01 14:29:58 +03:00
|
|
|
continue
|
|
|
|
}
|
2021-04-20 17:16:35 +03:00
|
|
|
unsafe { free(p) }
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
s.pool.clear()
|
|
|
|
for p in s.bin {
|
2022-05-20 18:30:16 +03:00
|
|
|
if unsafe { p == 0 } {
|
2020-07-02 15:31:42 +03:00
|
|
|
print(ptr_str(p) + ' ouch')
|
2020-07-01 14:29:58 +03:00
|
|
|
continue
|
|
|
|
}
|
2020-07-02 15:31:42 +03:00
|
|
|
unsafe {
|
|
|
|
// println('Freeing from bin')
|
2020-07-01 14:29:58 +03:00
|
|
|
free(p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.bin.clear()
|
2020-07-02 15:31:42 +03:00
|
|
|
}
|