Skip to content

Commit 7bde081

Browse files
Use my own particle system
1 parent 22f88db commit 7bde081

File tree

9 files changed

+436
-324
lines changed

9 files changed

+436
-324
lines changed

src/main.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,16 @@ mod screens {
4343

4444
pub use screens::*;
4545

46-
const WIDTH: f32 = 1000.0;
47-
const HEIGHT: f32 = 600.0;
46+
const WIDTH: f32 = 2000.0;
47+
const HEIGHT: f32 = 1000.0;
4848

4949
fn load_assets(ctx: &mut Context) -> AssetManager {
5050
let mut asset_manager = AssetManager::new();
5151

5252
let images_dir = fs::read_dir("./resources/images/").unwrap();
5353
let fonts_dir = fs::read_dir("./resources/fonts/").unwrap();
5454
let audio_dir = fs::read_dir("./resources/audio/").unwrap();
55+
let maps_dir = fs::read_dir("./resources/maps/").unwrap();
5556

5657
for image in images_dir {
5758
asset_manager.load_image(ctx, image.unwrap().file_name().to_str().unwrap());
@@ -65,6 +66,10 @@ fn load_assets(ctx: &mut Context) -> AssetManager {
6566
asset_manager.load_sound(ctx, audio.unwrap().file_name().to_str().unwrap());
6667
}
6768

69+
for map in maps_dir {
70+
asset_manager.load_file(ctx, "maps", map.unwrap().file_name().to_str().unwrap());
71+
}
72+
6873
asset_manager
6974
}
7075

src/screens/game/components/barrel.rs

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
use ggez::{graphics, nalgebra::Point2, Context, GameResult};
1+
use ggez::{audio::SoundSource, graphics, nalgebra::Point2, Context, GameResult};
22
use ggez_goodies::{camera::Camera, nalgebra_glm::Vec2};
33
use graphics::DrawParam;
44

55
use crate::{
66
game::physics::{isometry_to_point, Physics},
7-
utils::AssetManager,
7+
play,
8+
utils::{AssetManager, ParticleSystem},
89
HEIGHT,
910
};
1011

1112
const HEIGHT2: f32 = HEIGHT / 2.;
1213

1314
use nphysics2d::{nalgebra as na, object::DefaultBodyHandle};
1415

16+
use super::{bullet::PlayerWeapon, player::Player};
17+
1518
pub struct Barrel {
1619
body: DefaultBodyHandle,
1720
}
@@ -53,6 +56,56 @@ impl Barrel {
5356
Ok(())
5457
}
5558

59+
pub fn update(
60+
&mut self,
61+
physics: &mut Physics,
62+
asset_manager: &AssetManager,
63+
particles: &mut Vec<ParticleSystem>,
64+
player: &mut Player,
65+
) -> bool {
66+
let barrel = asset_manager.get_image("Some(barrel).png");
67+
68+
let position = self.position(physics);
69+
70+
for i in 0..player.weapons.len() {
71+
match &mut player.weapons[i] {
72+
PlayerWeapon::Turbofish(fish) => {
73+
if fish.is_touching(physics, self.handle()) {
74+
let explode_sound = asset_manager.get_sound("Some(explode).mp3");
75+
76+
// FIXME
77+
particles.push(ParticleSystem::new(
78+
physics,
79+
100,
80+
na::Point2::new(
81+
position.x - (barrel.width() / 2) as f32,
82+
position.y - (barrel.height() / 2) as f32,
83+
),
84+
na::Point2::new(
85+
position.x + (barrel.width() / 2) as f32,
86+
position.y + (barrel.height() / 2) as f32,
87+
),
88+
));
89+
90+
play!(explode_sound);
91+
92+
// Remove the enemy from the world
93+
self.destroy(physics);
94+
95+
// Remove the weapon from the world
96+
fish.destroy(physics);
97+
player.weapons.remove(i);
98+
99+
return true;
100+
}
101+
}
102+
PlayerWeapon::Grappling(_) => {}
103+
}
104+
}
105+
106+
false
107+
}
108+
56109
pub fn position(&self, physics: &mut Physics) -> na::Point2<f32> {
57110
let barrel_body = physics.get_rigid_body_mut(self.body);
58111
let barrel_position = isometry_to_point(barrel_body.position());

src/screens/game/components/cloud.rs

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,20 @@ use ggez::{
55
};
66
use graphics::DrawParam;
77

8-
use crate::{
9-
utils::{AssetManager, Position},
10-
WIDTH,
11-
};
8+
use crate::{utils::AssetManager, WIDTH};
9+
10+
use nphysics2d::nalgebra as na;
1211

1312
pub struct Cloud {
14-
position: Position,
13+
position: na::Point2<f32>,
1514

1615
scale: f32,
1716
speed: f32,
1817
}
1918

2019
impl Cloud {
21-
pub fn new(
22-
pos_x: f32,
23-
pos_y: f32,
24-
scale: f32,
25-
speed: f32,
26-
asset_manager: &AssetManager,
27-
) -> Self {
28-
let cloud = asset_manager.get_image("Some(cloud).png");
29-
let position = Position::new(pos_x, pos_y, cloud.width(), cloud.height());
20+
pub fn new(pos_x: f32, pos_y: f32, scale: f32, speed: f32) -> Self {
21+
let position = na::Point2::new(pos_x, pos_y);
3022

3123
Self {
3224
position,
@@ -47,27 +39,21 @@ impl Cloud {
4739
y: self.scale,
4840
})
4941
.dest(Point2 {
50-
x: self.position.pos_start.x,
51-
y: self.position.pos_start.y,
42+
x: self.position.x,
43+
y: self.position.y,
5244
}),
5345
)?;
5446

5547
Ok(())
5648
}
5749

58-
pub fn update(&mut self, ctx: &mut Context, asset_manager: &AssetManager) {
59-
let cloud = asset_manager.get_image("Some(cloud).png");
50+
pub fn update(&mut self, ctx: &mut Context) {
6051
let delta_time = ggez::timer::delta(ctx).as_secs_f32();
6152

62-
self.position.move_by("x+", delta_time * self.speed);
53+
self.position.x += delta_time * self.speed;
6354

64-
if self.position.pos_start.x > WIDTH + 100. {
65-
self.position = Position::new(
66-
-100.,
67-
self.position.pos_start.y,
68-
cloud.width(),
69-
cloud.height(),
70-
);
55+
if self.position.x > WIDTH + 100. {
56+
self.position = na::Point2::new(-100., self.position.y);
7157
}
7258
}
7359
}

src/screens/game/components/enemy.rs

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use ggez::{
2+
audio::SoundSource,
23
graphics::{self, DrawParam},
34
nalgebra::Point2,
45
Context, GameResult,
@@ -9,13 +10,14 @@ use nphysics2d::{nalgebra as na, object::DefaultBodyHandle};
910

1011
use crate::{
1112
game::physics::{isometry_to_point, Physics},
12-
utils::AssetManager,
13+
play,
14+
utils::{AssetManager, ParticleSystem},
1315
HEIGHT,
1416
};
1517

1618
const HEIGHT2: f32 = HEIGHT / 2.;
1719

18-
use super::player::Player;
20+
use super::{bullet::PlayerWeapon, player::Player};
1921

2022
pub struct Enemy {
2123
body: DefaultBodyHandle,
@@ -70,11 +72,57 @@ impl Enemy {
7072
Ok(())
7173
}
7274

73-
pub fn update(&mut self, physics: &mut Physics, player: &Player) {
75+
pub fn update(
76+
&mut self,
77+
physics: &mut Physics,
78+
asset_manager: &AssetManager,
79+
particles: &mut Vec<ParticleSystem>,
80+
player: &mut Player,
81+
) -> bool {
82+
let position = self.position(physics);
83+
84+
let gopher = asset_manager.get_image("gopher.png");
85+
let explode_sound = asset_manager.get_sound("Some(explode).mp3");
86+
87+
for i in 0..player.weapons.len() {
88+
match &mut player.weapons[i] {
89+
PlayerWeapon::Turbofish(fish) => {
90+
if fish.is_touching(physics, self.handle()) {
91+
particles.push(ParticleSystem::new(
92+
physics,
93+
50,
94+
na::Point2::new(
95+
position.x - (gopher.width() / 2) as f32,
96+
position.y - (gopher.height() / 2) as f32,
97+
),
98+
na::Point2::new(
99+
position.x + (gopher.width() / 2) as f32,
100+
position.y + (gopher.height() / 2) as f32,
101+
),
102+
));
103+
104+
play!(explode_sound);
105+
106+
// Remove the enemy from the world
107+
self.destroy(physics);
108+
109+
// Remove the weapon from the world
110+
fish.destroy(physics);
111+
player.weapons.remove(i);
112+
113+
return true;
114+
}
115+
}
116+
PlayerWeapon::Grappling(_) => {}
117+
}
118+
}
119+
74120
// Can the enemy see the player?
75121
if physics.distance(self.handle(), player.handle()) < 300.0 {
76122
// TODO: The enemy shoots the player as soon as it see's the player.
77123
}
124+
125+
return false;
78126
}
79127

80128
pub fn position(&self, physics: &mut Physics) -> na::Point2<f32> {

0 commit comments

Comments
 (0)