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
|
|
|
|
|
2022-12-27 16:19:08 +03:00
|
|
|
import math.vec
|
2020-07-01 14:29:58 +03:00
|
|
|
import sokol.sgl
|
|
|
|
|
|
|
|
const (
|
|
|
|
default_life_time = 1000
|
2020-07-02 15:31:42 +03:00
|
|
|
default_v_color = Color{93, 136, 193, 255}
|
2020-07-01 14:29:58 +03:00
|
|
|
)
|
|
|
|
|
2020-07-02 15:31:42 +03:00
|
|
|
// * Module public
|
2022-12-27 16:19:08 +03:00
|
|
|
pub fn new(location vec.Vec2[f64]) &Particle {
|
2020-07-02 15:31:42 +03:00
|
|
|
p := &Particle{
|
|
|
|
location: location
|
2022-12-27 16:19:08 +03:00
|
|
|
velocity: vec.Vec2[f64]{0, 0}
|
|
|
|
acceleration: vec.Vec2[f64]{0, 0}
|
2021-04-20 17:16:35 +03:00
|
|
|
color: particle.default_v_color
|
|
|
|
life_time: particle.default_life_time
|
|
|
|
life_time_init: particle.default_life_time
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-10-18 13:45:13 +03:00
|
|
|
fn remap(v f64, min f64, max f64, new_min f64, new_max f64) f64 {
|
2020-07-01 14:29:58 +03:00
|
|
|
return (((v - min) * (new_max - new_min)) / (max - min)) + new_min
|
|
|
|
}
|
|
|
|
|
2020-09-28 07:14:15 +03:00
|
|
|
// Particle
|
2020-07-01 14:29:58 +03:00
|
|
|
pub struct Particle {
|
|
|
|
mut:
|
2022-12-27 16:19:08 +03:00
|
|
|
location vec.Vec2[f64]
|
|
|
|
velocity vec.Vec2[f64]
|
|
|
|
acceleration vec.Vec2[f64]
|
2020-07-02 15:31:42 +03:00
|
|
|
color Color
|
|
|
|
life_time f64
|
|
|
|
life_time_init f64
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut p Particle) update(dt f64) {
|
|
|
|
mut acc := p.acceleration
|
2022-12-27 16:19:08 +03:00
|
|
|
acc.multiply_scalar(dt)
|
2020-07-01 14:29:58 +03:00
|
|
|
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
|
2022-04-15 14:58:56 +03:00
|
|
|
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
|
2020-07-01 14:29:58 +03:00
|
|
|
} 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()
|
2020-07-02 15:31:42 +03:00
|
|
|
// p.color = Color{93, 136, 193, 255}
|
2021-04-20 17:16:35 +03:00
|
|
|
p.color = particle.default_v_color
|
|
|
|
p.life_time = particle.default_life_time
|
2020-07-01 14:29:58 +03:00
|
|
|
p.life_time_init = p.life_time
|
|
|
|
}
|