2021-01-26 22:13:11 +01:00
|
|
|
module objects
|
|
|
|
|
|
|
|
import gg
|
|
|
|
import gx
|
|
|
|
import rand
|
|
|
|
|
|
|
|
pub struct Rocket {
|
|
|
|
pub mut:
|
|
|
|
color gx.Color
|
|
|
|
pos Vector
|
|
|
|
vel Vector
|
|
|
|
accel Vector
|
|
|
|
exploded bool
|
|
|
|
particles []Particle
|
|
|
|
dead bool
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (rocket Rocket) draw(mut ctx gg.Context) {
|
2021-01-26 22:40:30 +01:00
|
|
|
ctx.draw_circle(rocket.pos.x, get_params().height - rocket.pos.y, get_params().rocket_radius,
|
|
|
|
rocket.color)
|
2021-01-26 22:13:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut rocket Rocket) explode() {
|
|
|
|
rocket.exploded = true
|
|
|
|
|
2021-01-26 22:40:30 +01:00
|
|
|
for _ in 0 .. get_params().offspring_count {
|
2021-01-26 22:13:11 +01:00
|
|
|
rocket.spawn_particle()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut rocket Rocket) tick(mut ctx gg.Context) {
|
|
|
|
if !rocket.exploded {
|
|
|
|
if rocket.vel.y <= 1 {
|
|
|
|
rocket.explode()
|
|
|
|
}
|
|
|
|
|
2021-01-26 22:40:30 +01:00
|
|
|
rocket.accel += get_params().gravity
|
2021-01-26 22:13:11 +01:00
|
|
|
rocket.vel += rocket.accel
|
|
|
|
rocket.pos += rocket.vel
|
|
|
|
rocket.draw(mut ctx)
|
|
|
|
|
|
|
|
rocket.accel = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
for mut particle in rocket.particles {
|
|
|
|
particle.tick(mut rocket, mut ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_rocket() Rocket {
|
|
|
|
return Rocket{
|
|
|
|
color: random_color()
|
|
|
|
pos: {
|
2021-01-26 22:40:30 +01:00
|
|
|
x: rand.f32_in_range(50, get_params().width - 50)
|
2021-01-26 22:13:11 +01:00
|
|
|
}
|
|
|
|
vel: {
|
|
|
|
x: rand.f32_in_range(-1.5, 1.5)
|
|
|
|
y: rand.f32_in_range(5, 7)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut rocket Rocket) spawn_particle() {
|
|
|
|
rocket.particles << Particle{
|
|
|
|
color: rocket.color
|
|
|
|
pos: rocket.pos
|
|
|
|
accel: random_vector_in_circle().mult(2)
|
|
|
|
}
|
|
|
|
}
|