Skip to content

Commit 1b96b4e

Browse files
Add particles
1 parent de400c0 commit 1b96b4e

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

src/game.rs

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{io::Read, sync::Mutex};
22

3-
use ggez::{Context, GameResult, event::KeyCode, graphics::{self, Color, DrawMode, DrawParam, Scale, Text}, nalgebra::Point2, timer};
4-
use ggez_goodies::{camera::Camera, nalgebra_glm::Vec2};
3+
use ggez::{Context, GameResult, event::KeyCode, graphics::{self, Color, DrawMode, DrawParam, Scale, Text}, mint, nalgebra::Point2, timer};
4+
use ggez_goodies::{camera::{Camera, CameraDraw}, nalgebra_glm::Vec2, particle::{EmissionShape, ParticleSystem, ParticleSystemBuilder, Transition}};
55
use graphics::{Font, Image, Mesh, TextFragment};
66
use rand::Rng;
77

@@ -24,7 +24,8 @@ pub struct Game {
2424

2525
camera: Camera,
2626
elapsed_shake: Option<(f32, Vec2)>,
27-
tics: Option<i32>
27+
tics: Option<i32>,
28+
particles: Vec<(ParticleSystem, f32, f32, i32)>
2829
}
2930

3031
impl Game {
@@ -122,6 +123,7 @@ impl Game {
122123
consolas: graphics::Font::new(ctx, "/fonts/Consolas.ttf").unwrap(),
123124
elapsed_shake: None,
124125
tics: None,
126+
particles: vec![]
125127
})
126128
}
127129

@@ -187,6 +189,15 @@ impl Game {
187189
fish.draw(ctx, &self.camera, &self.bullet_resources)?;
188190
}
189191

192+
for sys in &mut self.particles {
193+
&sys.0.draw_camera(
194+
&self.camera,
195+
ctx,
196+
Vec2::new(sys.1, sys.2),
197+
0.
198+
);
199+
}
200+
190201
graphics::present(ctx)
191202
}
192203

@@ -204,7 +215,7 @@ impl Game {
204215
Ok(None)
205216
}
206217

207-
pub fn inner_update(&mut self, _ctx: &mut Context) -> GameResult<Option<crate::Screen>> {
218+
pub fn inner_update(&mut self, ctx: &mut Context) -> GameResult<Option<crate::Screen>> {
208219
let ferris_pos_x = self.player.pos_x;
209220
let mut ferris_is_falling_down: bool = true;
210221

@@ -238,6 +249,26 @@ impl Game {
238249

239250
for fish in &self.player_bullets {
240251
if fish.pos_x >= go_start_x && fish.pos_x <= go_end_x {
252+
const HEIGHT2: f32 = HEIGHT / 2.;
253+
254+
self.particles.push(
255+
(ParticleSystemBuilder::new(ctx)
256+
.count(100)
257+
.emission_rate(100.0)
258+
.start_max_age(5.0)
259+
.start_size_range(2.0, 15.0)
260+
.start_color_range(
261+
graphics::Color::from((0, 0, 0)),
262+
graphics::Color::from((255, 255, 255)),
263+
)
264+
.delta_color(Transition::range(
265+
ggez::graphics::Color::from((255, 0, 0)),
266+
ggez::graphics::Color::from((255, 255, 0)),
267+
))
268+
.emission_shape(EmissionShape::Circle(mint::Point2 { x: 0.0, y: 0.0 }, 100.0))
269+
.build(), go_start_x, -HEIGHT2 + 70., 0)
270+
);
271+
241272
self.enemies.remove(i);
242273

243274
done = true;
@@ -276,6 +307,19 @@ impl Game {
276307
}
277308
}
278309

310+
for sys in &mut self.particles {
311+
sys.0.update(0.5);
312+
sys.3 += 1;
313+
}
314+
315+
for i in 0..self.particles.len() {
316+
let sys = &self.particles[i];
317+
318+
if sys.3 > 6 {
319+
self.particles.remove(i);
320+
}
321+
}
322+
279323
Ok(None)
280324
}
281325

0 commit comments

Comments
 (0)