aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/examples/fireworks/modules/objects/rocket.v
blob: ebdce0aa42566f0967157fa96d129c03478e6e8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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) {
	ctx.draw_circle(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)
		}
		vel: Vector{
			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)
	}
}