Initial project import
This commit is contained in:
commit
23f2d29e32
11957 files changed
+1754577
No files matched your search
@@ -0,0 +1,107 @@
|
||||
import objects
|
||||
import gg
|
||||
import rand
|
||||
|
||||
struct App {
|
||||
mut:
|
||||
gg &gg.Context = unsafe { nil }
|
||||
ui &objects.UIParams = unsafe { nil }
|
||||
rockets []objects.Rocket
|
||||
frames [][]objects.Rocket
|
||||
// i thought about using a fixed fifo queue for the frames but the array
|
||||
// seemed to work fine, if you'd like a challenge try implementing it with the queue :)
|
||||
draw_flag bool = true
|
||||
}
|
||||
|
||||
fn on_frame(mut app App) {
|
||||
if !app.draw_flag {
|
||||
return
|
||||
}
|
||||
app.gg.begin()
|
||||
|
||||
// drawing previous frames
|
||||
for mut frame in app.frames {
|
||||
for mut rocket in frame {
|
||||
if !rocket.exploded {
|
||||
rocket.color.a = u8(f32_max(rocket.color.a - 8, 0))
|
||||
rocket.draw(mut app.gg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// chance of firing new rocket
|
||||
if rand.intn(30) or { 0 } == 0 {
|
||||
app.rockets << objects.new_rocket()
|
||||
}
|
||||
// simulating rockets
|
||||
app.rockets = app.rockets.filter(!it.dead)
|
||||
|
||||
for mut rocket in app.rockets {
|
||||
rocket.tick(mut app.gg)
|
||||
}
|
||||
|
||||
// adding frame
|
||||
mut frame := app.rockets.clone()
|
||||
|
||||
for mut rocket in frame {
|
||||
rocket.particles = []
|
||||
}
|
||||
|
||||
app.frames << frame
|
||||
|
||||
// trimming out frames
|
||||
if app.frames.len > 30 {
|
||||
app.frames.delete(0)
|
||||
}
|
||||
|
||||
app.gg.end()
|
||||
}
|
||||
|
||||
fn on_event(e &gg.Event, mut app App) {
|
||||
match e.typ {
|
||||
.resized, .resumed {
|
||||
app.resize()
|
||||
}
|
||||
.iconified {
|
||||
app.draw_flag = false
|
||||
}
|
||||
.restored {
|
||||
app.draw_flag = true
|
||||
app.resize()
|
||||
}
|
||||
else {
|
||||
// println("Type ${e.typ}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn (mut app App) resize() {
|
||||
size := gg.window_size()
|
||||
// avoid calls when minimized
|
||||
if size.width < 2 && size.height < 2 {
|
||||
return
|
||||
}
|
||||
mut s := gg.dpi_scale()
|
||||
|
||||
if s == 0.0 {
|
||||
s = 1.0
|
||||
}
|
||||
app.ui.dpi_scale = s
|
||||
app.ui.width = size.width
|
||||
app.ui.height = size.height
|
||||
}
|
||||
|
||||
fn main() {
|
||||
mut app := &App{}
|
||||
app.ui = objects.get_params()
|
||||
app.gg = gg.new_context(
|
||||
width: app.ui.width
|
||||
height: app.ui.height
|
||||
window_title: 'Fireworks!'
|
||||
bg_color: gg.black
|
||||
user_data: app
|
||||
frame_fn: on_frame
|
||||
event_fn: on_event
|
||||
)
|
||||
app.gg.run()
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
module objects
|
||||
|
||||
import gg
|
||||
import rand
|
||||
|
||||
pub fn random_color() gg.Color {
|
||||
return gg.Color{
|
||||
r: rand.u8()
|
||||
g: rand.u8()
|
||||
b: rand.u8()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
module objects
|
||||
|
||||
pub struct UIParams {
|
||||
pub mut:
|
||||
dpi_scale f32 = 1.0
|
||||
width int = 800
|
||||
height int = 800
|
||||
gravity Vector = Vector{0, -0.03}
|
||||
age_rate int = 1
|
||||
offspring_count int = 100
|
||||
rocket_radius int = 5
|
||||
particle_radius f32 = 2.5
|
||||
drag f32 = 0.98
|
||||
}
|
||||
|
||||
const params = &UIParams{}
|
||||
|
||||
pub fn get_params() &UIParams {
|
||||
return params
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
module objects
|
||||
|
||||
import gg
|
||||
|
||||
pub struct Particle {
|
||||
pub mut:
|
||||
color gg.Color
|
||||
pos Vector
|
||||
vel Vector
|
||||
accel Vector
|
||||
lifespan f32 = 255
|
||||
}
|
||||
|
||||
pub fn (particle Particle) draw(mut ctx gg.Context) {
|
||||
ctx.draw_circle_filled(particle.pos.x, get_params().height - particle.pos.y,
|
||||
get_params().particle_radius, particle.color)
|
||||
}
|
||||
|
||||
pub fn (mut particle Particle) tick(mut rocket Rocket, mut ctx gg.Context) {
|
||||
particle.lifespan -= get_params().age_rate
|
||||
particle.color.a = u8(particle.lifespan)
|
||||
|
||||
if particle.lifespan <= 0 {
|
||||
rocket.dead = true
|
||||
return
|
||||
}
|
||||
|
||||
particle.accel += get_params().gravity
|
||||
particle.vel += particle.accel
|
||||
particle.vel = particle.vel.mult(get_params().drag)
|
||||
particle.pos += particle.vel
|
||||
particle.draw(mut ctx)
|
||||
|
||||
particle.accel = Vector{}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
module objects
|
||||
|
||||
import gg
|
||||
import rand
|
||||
|
||||
pub struct Rocket {
|
||||
pub mut:
|
||||
color gg.Color
|
||||
pos Vector
|
||||
vel Vector
|
||||
accel Vector
|
||||
exploded bool
|
||||
particles []Particle
|
||||
dead bool
|
||||
}
|
||||
|
||||
pub fn (rocket Rocket) draw(mut ctx gg.Context) {
|
||||
ctx.draw_circle_filled(rocket.pos.x, get_params().height - rocket.pos.y,
|
||||
get_params().rocket_radius, rocket.color)
|
||||
}
|
||||
|
||||
pub fn (mut rocket Rocket) explode() {
|
||||
rocket.exploded = true
|
||||
|
||||
for _ in 0 .. get_params().offspring_count {
|
||||
rocket.spawn_particle()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (mut rocket Rocket) tick(mut ctx gg.Context) {
|
||||
if !rocket.exploded {
|
||||
if rocket.vel.y <= 1 {
|
||||
rocket.explode()
|
||||
}
|
||||
|
||||
rocket.accel += get_params().gravity
|
||||
rocket.vel += rocket.accel
|
||||
rocket.pos += rocket.vel
|
||||
rocket.draw(mut ctx)
|
||||
|
||||
rocket.accel = Vector{}
|
||||
}
|
||||
|
||||
for mut particle in rocket.particles {
|
||||
particle.tick(mut rocket, mut ctx)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_rocket() Rocket {
|
||||
return Rocket{
|
||||
color: random_color()
|
||||
pos: Vector{
|
||||
x: rand.f32_in_range(50, get_params().width - 50) or { 50 }
|
||||
}
|
||||
vel: Vector{
|
||||
x: rand.f32_in_range(-1.5, 1.5) or { -1.5 }
|
||||
y: rand.f32_in_range(5, 7) or { 5 }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (mut rocket Rocket) spawn_particle() {
|
||||
rocket.particles << Particle{
|
||||
color: rocket.color
|
||||
pos: rocket.pos
|
||||
accel: random_vector_in_circle().mult(2)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
module objects
|
||||
|
||||
import math
|
||||
import rand
|
||||
|
||||
pub struct Vector {
|
||||
pub mut:
|
||||
x f32
|
||||
y f32
|
||||
}
|
||||
|
||||
pub fn (a Vector) + (b Vector) Vector {
|
||||
return Vector{a.x + b.x, a.y + b.y}
|
||||
}
|
||||
|
||||
pub fn (vector Vector) mult(scalar f32) Vector {
|
||||
return Vector{vector.x * scalar, vector.y * scalar}
|
||||
}
|
||||
|
||||
pub fn random_vector_in_circle() Vector {
|
||||
theta := rand.f32n(2 * math.pi) or { 0 }
|
||||
y := rand.f32()
|
||||
|
||||
return Vector{
|
||||
x: f32(y * math.sin(theta))
|
||||
y: f32(y * math.cos(theta))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user