diff options
author | Indrajith K L | 2022-12-03 17:00:20 +0530 |
---|---|---|
committer | Indrajith K L | 2022-12-03 17:00:20 +0530 |
commit | f5c4671bfbad96bf346bd7e9a21fc4317b4959df (patch) | |
tree | 2764fc62da58f2ba8da7ed341643fc359873142f /v_windows/v/examples/fireworks/modules/objects | |
download | cli-tools-windows-master.tar.gz cli-tools-windows-master.tar.bz2 cli-tools-windows-master.zip |
Diffstat (limited to 'v_windows/v/examples/fireworks/modules/objects')
5 files changed, 165 insertions, 0 deletions
diff --git a/v_windows/v/examples/fireworks/modules/objects/color.v b/v_windows/v/examples/fireworks/modules/objects/color.v new file mode 100644 index 0000000..6761f26 --- /dev/null +++ b/v_windows/v/examples/fireworks/modules/objects/color.v @@ -0,0 +1,12 @@ +module objects + +import gx +import rand + +pub fn random_color() gx.Color { + return gx.Color{ + r: byte(rand.int_in_range(0, 256)) + g: byte(rand.int_in_range(0, 256)) + b: byte(rand.int_in_range(0, 256)) + } +} diff --git a/v_windows/v/examples/fireworks/modules/objects/constants.v b/v_windows/v/examples/fireworks/modules/objects/constants.v new file mode 100644 index 0000000..a8e5abd --- /dev/null +++ b/v_windows/v/examples/fireworks/modules/objects/constants.v @@ -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 objects.params +} diff --git a/v_windows/v/examples/fireworks/modules/objects/particle.v b/v_windows/v/examples/fireworks/modules/objects/particle.v new file mode 100644 index 0000000..8f84a39 --- /dev/null +++ b/v_windows/v/examples/fireworks/modules/objects/particle.v @@ -0,0 +1,36 @@ +module objects + +import gg +import gx + +pub struct Particle { +pub mut: + color gx.Color + pos Vector + vel Vector + accel Vector + lifespan f32 = 255 +} + +pub fn (particle Particle) draw(mut ctx gg.Context) { + ctx.draw_circle(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 = byte(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{} +} diff --git a/v_windows/v/examples/fireworks/modules/objects/rocket.v b/v_windows/v/examples/fireworks/modules/objects/rocket.v new file mode 100644 index 0000000..ebdce0a --- /dev/null +++ b/v_windows/v/examples/fireworks/modules/objects/rocket.v @@ -0,0 +1,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) + } +} diff --git a/v_windows/v/examples/fireworks/modules/objects/vector.v b/v_windows/v/examples/fireworks/modules/objects/vector.v new file mode 100644 index 0000000..0e29283 --- /dev/null +++ b/v_windows/v/examples/fireworks/modules/objects/vector.v @@ -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) + y := rand.f32() + + return Vector{ + x: f32(y * math.sin(theta)) + y: f32(y * math.cos(theta)) + } +} |