Initial project import
This commit is contained in:
commit
23f2d29e32
11957 files changed
+1754577
No files matched your search
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2019 Lars Pontoppidan
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,12 @@
|
||||
// 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
|
||||
|
||||
// * Color
|
||||
pub struct Color {
|
||||
mut:
|
||||
r u8
|
||||
g u8
|
||||
b u8
|
||||
a u8
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
// 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
|
||||
|
||||
import math.vec
|
||||
import sokol.sgl
|
||||
|
||||
const default_life_time = 1000
|
||||
const default_v_color = Color{93, 136, 193, 255}
|
||||
|
||||
// * Module public
|
||||
pub fn new(location vec.Vec2[f64]) &Particle {
|
||||
p := &Particle{
|
||||
location: location
|
||||
velocity: vec.Vec2[f64]{0, 0}
|
||||
acceleration: vec.Vec2[f64]{0, 0}
|
||||
color: default_v_color
|
||||
life_time: default_life_time
|
||||
life_time_init: default_life_time
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
fn remap(v f64, min f64, max f64, new_min f64, new_max f64) f64 {
|
||||
return (((v - min) * (new_max - new_min)) / (max - min)) + new_min
|
||||
}
|
||||
|
||||
// Particle
|
||||
pub struct Particle {
|
||||
pub mut:
|
||||
location vec.Vec2[f64]
|
||||
velocity vec.Vec2[f64]
|
||||
acceleration vec.Vec2[f64]
|
||||
color Color
|
||||
life_time f64
|
||||
life_time_init f64
|
||||
}
|
||||
|
||||
pub fn (mut p Particle) update(dt f64) {
|
||||
mut acc := p.acceleration
|
||||
acc.multiply_scalar(dt)
|
||||
p.velocity = p.velocity.add(acc)
|
||||
p.location = p.location.add(p.velocity)
|
||||
lt := p.life_time - (1000 * dt)
|
||||
if lt > 0 {
|
||||
p.life_time = lt
|
||||
p.color.r = p.color.r - 1 // u8(remap(p.life_time,0.0,p.life_time_init,0,p.color.r))
|
||||
p.color.g = p.color.g - 1 // u8(remap(p.life_time,0.0,p.life_time_init,0,p.color.g))
|
||||
p.color.b = p.color.b - 1 // u8(remap(p.life_time,0.0,p.life_time_init,0,p.color.b))
|
||||
p.color.a = u8(int(remap(p.life_time, 0.0, p.life_time_init, 0, 255))) - 10
|
||||
} else {
|
||||
p.life_time = 0
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (p Particle) is_dead() bool {
|
||||
return p.life_time <= 0.0
|
||||
}
|
||||
|
||||
pub fn (p Particle) draw() {
|
||||
l := p.location
|
||||
sgl.c4b(p.color.r, p.color.g, p.color.b, p.color.a)
|
||||
lx := f32(l.x)
|
||||
ly := f32(l.y)
|
||||
sgl.v2f(lx, ly)
|
||||
sgl.v2f(lx + 2, ly)
|
||||
sgl.v2f(lx + 2, ly + 2)
|
||||
sgl.v2f(lx, ly + 2)
|
||||
}
|
||||
|
||||
pub fn (mut p Particle) reset() {
|
||||
p.location.zero()
|
||||
p.acceleration.zero()
|
||||
p.velocity.zero()
|
||||
// p.color = Color{93, 136, 193, 255}
|
||||
p.color = default_v_color
|
||||
p.life_time = default_life_time
|
||||
p.life_time_init = p.life_time
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
// 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
|
||||
|
||||
import math.vec
|
||||
import rand
|
||||
import sokol.sgl
|
||||
|
||||
pub struct SystemConfig {
|
||||
pub:
|
||||
pool int
|
||||
}
|
||||
|
||||
pub struct System {
|
||||
pub:
|
||||
width int
|
||||
height int
|
||||
mut:
|
||||
pool []&Particle
|
||||
bin []&Particle
|
||||
}
|
||||
|
||||
pub fn (mut s System) init(sc SystemConfig) {
|
||||
unsafe { s.pool.flags.set(.noslices | .noshrink) }
|
||||
unsafe { s.bin.flags.set(.noslices | .noshrink) }
|
||||
for i := 0; i < sc.pool; i++ {
|
||||
p := new(vec.Vec2[f64]{f32(s.width) * 0.5, f32(s.height) * 0.5})
|
||||
s.bin << p
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (mut s System) update(dt f64) {
|
||||
mut p := &Particle(unsafe { nil })
|
||||
mut moved := 0
|
||||
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)
|
||||
moved++
|
||||
}
|
||||
}
|
||||
if moved != 0 {
|
||||
$if trace_moves_spool_to_sbin ? {
|
||||
eprintln('${moved:4} particles s.pool -> s.bin')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (s System) draw() {
|
||||
sgl.begin_quads()
|
||||
for p in s.pool {
|
||||
p.draw()
|
||||
}
|
||||
sgl.end()
|
||||
}
|
||||
|
||||
pub fn (mut s System) reset() {
|
||||
for i in 0 .. s.pool.len {
|
||||
mut p := s.pool[i]
|
||||
p.reset()
|
||||
p.life_time = 0
|
||||
}
|
||||
for i in 0 .. s.bin.len {
|
||||
mut p := s.pool[i]
|
||||
p.reset()
|
||||
p.life_time = 0
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (mut s System) explode(x f32, y f32) {
|
||||
mut reserve := 500
|
||||
center := vec.Vec2[f64]{x, y}
|
||||
mut p := &Particle(unsafe { nil })
|
||||
mut moved := 0
|
||||
for i := 0; i < s.bin.len && reserve > 0; i++ {
|
||||
p = s.bin[i]
|
||||
p.reset()
|
||||
p.location.from(center)
|
||||
p.acceleration = vec.Vec2[f64]{rand.f32_in_range(-0.5, 0.5) or { -0.5 }, rand.f32_in_range(-0.5, 0.5) or {
|
||||
-0.5
|
||||
}}
|
||||
p.velocity = vec.Vec2[f64]{rand.f32_in_range(-0.5, 0.5) or { -0.5 }, rand.f32_in_range(-0.5, 0.5) or {
|
||||
-0.5
|
||||
}}
|
||||
p.life_time = rand.f64_in_range(500, 2000) or { 500 }
|
||||
s.pool << p
|
||||
s.bin.delete(i)
|
||||
moved++
|
||||
reserve--
|
||||
}
|
||||
if moved != 0 {
|
||||
$if trace_moves_sbin_to_spool ? {
|
||||
eprintln('${moved:4} particles s.bin -> s.pool')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (mut s System) free() {
|
||||
for p in s.pool {
|
||||
if unsafe { p == 0 } {
|
||||
print(ptr_str(p) + ' ouch')
|
||||
continue
|
||||
}
|
||||
unsafe { free(p) }
|
||||
}
|
||||
s.pool.clear()
|
||||
for p in s.bin {
|
||||
if unsafe { p == 0 } {
|
||||
print(ptr_str(p) + ' ouch')
|
||||
continue
|
||||
}
|
||||
unsafe {
|
||||
// println('Freeing from bin')
|
||||
free(p)
|
||||
}
|
||||
}
|
||||
s.bin.clear()
|
||||
}
|
||||
Reference in new issue
Block a user