Skip to content

Commit 796208e

Browse files
Minor cleanups for asset management
1 parent f663dc5 commit 796208e

File tree

13 files changed

+488
-211
lines changed

13 files changed

+488
-211
lines changed

resources/images/Some(nil).png

475 Bytes
Loading

resources/images/gopher.png

-275 Bytes
Loading

src/components/barrel.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,53 @@
1-
use ggez::{graphics::Image, Context, GameResult};
1+
use ggez::{Context, GameResult};
22
use ggez_goodies::{
33
camera::{Camera, CameraDraw},
44
nalgebra_glm::Vec2,
55
};
66

7-
use crate::HEIGHT;
7+
use crate::{
8+
utils::{AssetManager, Position},
9+
HEIGHT,
10+
};
11+
12+
const HEIGHT2: f32 = HEIGHT / 2.;
813

914
pub struct Barrel {
10-
pub pos_x: f32,
15+
position: Position,
1116
}
1217

1318
impl Barrel {
14-
pub fn new(pos_x: f32) -> Self {
15-
Self { pos_x }
19+
pub fn new(pos_x: f32, asset_manager: &AssetManager) -> Self {
20+
let barrel = asset_manager.get_image("Some(barrel).png");
21+
22+
let position = Position::new(
23+
pos_x,
24+
-HEIGHT2 + (barrel.height() + 40) as f32,
25+
barrel.width(),
26+
barrel.height(),
27+
);
28+
29+
Self { position }
1630
}
1731

1832
pub fn draw(
1933
&mut self,
2034
ctx: &mut Context,
2135
camera: &Camera,
22-
resources: &Vec<Image>,
36+
asset_manager: &AssetManager,
2337
) -> GameResult<()> {
24-
const HEIGHT2: f32 = HEIGHT / 2.;
38+
let barrel = asset_manager.get_image("Some(barrel).png");
2539

26-
&resources[0].draw_camera(
40+
barrel.draw_camera(
2741
camera,
2842
ctx,
29-
Vec2::new(self.pos_x, -HEIGHT2 + (resources[0].height() + 40) as f32),
43+
Vec2::new(self.position.pos_start.x, self.position.pos_start.y),
3044
0.,
31-
);
45+
)?;
3246

3347
Ok(())
3448
}
49+
50+
pub fn position(&self) -> Position {
51+
self.position
52+
}
3553
}

src/components/bullet.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
1-
use ggez::{graphics::Image, Context, GameResult};
1+
use ggez::{Context, GameResult};
22
use ggez_goodies::{
33
camera::{Camera, CameraDraw},
44
nalgebra_glm::Vec2,
55
};
66

7-
pub struct Turbofish {
8-
pub pos_x: f32,
9-
pub pos_y: f32,
7+
use crate::utils::{AssetManager, Position};
108

9+
pub struct Turbofish {
10+
position: Position,
1111
traveled_count: i32,
1212
}
1313

1414
impl Turbofish {
15-
pub fn new(pos_x: f32, pos_y: f32) -> Self {
16-
Self {
15+
pub fn new(pos_x: f32, pos_y: f32, asset_manager: &AssetManager) -> Self {
16+
let turbofish_bullet = asset_manager.get_image("Some(turbofish).png");
17+
let position = Position::new(
1718
pos_x,
1819
pos_y,
20+
turbofish_bullet.width(),
21+
turbofish_bullet.height(),
22+
);
23+
24+
Self {
25+
position,
1926
traveled_count: 0,
2027
}
2128
}
@@ -24,21 +31,32 @@ impl Turbofish {
2431
&mut self,
2532
ctx: &mut Context,
2633
camera: &Camera,
27-
resources: &Vec<Image>,
34+
asset_manager: &AssetManager,
2835
) -> GameResult<()> {
29-
&resources[0].draw_camera(camera, ctx, Vec2::new(self.pos_x, self.pos_y), 1.5708);
36+
let turbofish_bullet = asset_manager.get_image("Some(turbofish).png");
37+
38+
turbofish_bullet.draw_camera(
39+
camera,
40+
ctx,
41+
Vec2::new(self.position.pos_start.x, self.position.pos_start.y),
42+
1.5708,
43+
)?;
3044

3145
Ok(())
3246
}
3347

3448
pub fn go_boom(&mut self) -> bool {
3549
if self.traveled_count < 100 {
3650
self.traveled_count += 1;
37-
self.pos_x += 10.;
51+
self.position.move_by("x+", 10.0);
3852

3953
false
4054
} else {
4155
true
4256
}
4357
}
58+
59+
pub fn position(&self) -> Position {
60+
self.position
61+
}
4462
}

src/components/cloud.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,59 @@
11
use ggez::{
2-
graphics::{self, Image},
2+
graphics,
33
mint::{Point2, Vector2},
44
Context, GameResult,
55
};
66
use graphics::DrawParam;
77

8-
use crate::WIDTH;
8+
use crate::{WIDTH, utils::{AssetManager, Position}};
99

1010
pub struct Cloud {
11-
pos_x: f32,
12-
pos_y: f32,
11+
position: Position,
12+
1313
scale: f32,
1414
speed: f32,
1515
}
1616

1717
impl Cloud {
18-
pub fn new(pos_x: f32, pos_y: f32, scale: f32, speed: f32) -> Self {
18+
pub fn new(pos_x: f32, pos_y: f32, scale: f32, speed: f32, asset_manager: &AssetManager) -> Self {
19+
let cloud = asset_manager.get_image("Some(cloud).png");
20+
let position = Position::new(pos_x, pos_y, cloud.width(), cloud.height());
21+
1922
Self {
20-
pos_x,
21-
pos_y,
23+
position,
2224
scale,
2325
speed,
2426
}
2527
}
2628

27-
pub fn draw(&mut self, ctx: &mut Context, resources: &Vec<Image>) -> GameResult<()> {
29+
pub fn draw(&mut self, ctx: &mut Context, asset_manager: &AssetManager) -> GameResult<()> {
30+
let cloud = asset_manager.get_image("Some(cloud).png");
31+
2832
graphics::draw(
2933
ctx,
30-
&resources[0],
34+
&cloud,
3135
DrawParam::default()
3236
.scale(Vector2 {
3337
x: self.scale,
3438
y: self.scale,
3539
})
3640
.dest(Point2 {
37-
x: self.pos_x,
38-
y: self.pos_y,
41+
x: self.position.pos_start.x,
42+
y: self.position.pos_start.y,
3943
}),
4044
)?;
4145

4246
Ok(())
4347
}
4448

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

48-
self.pos_x += delta_time * self.speed;
53+
self.position.move_by("x+", delta_time * self.speed);
4954

50-
if self.pos_x > WIDTH + 100. {
51-
self.pos_x = -100.;
55+
if self.position.pos_start.x > WIDTH + 100. {
56+
self.position = Position::new(-100., self.position.pos_start.y, cloud.width(), cloud.height());
5257
}
5358
}
5459
}

src/components/enemy.rs

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,67 @@
1-
use ggez::{graphics, Context, GameResult};
1+
use ggez::{Context, GameResult};
22
use ggez_goodies::{
33
camera::{Camera, CameraDraw},
44
nalgebra_glm::Vec2,
55
};
6-
use graphics::Image;
76

8-
use crate::HEIGHT;
7+
use crate::{
8+
utils::{AssetManager, Position},
9+
HEIGHT,
10+
};
11+
12+
const HEIGHT2: f32 = HEIGHT / 2.;
13+
14+
use super::player::Player;
915

1016
pub struct Enemy {
11-
pub pos_x: f32,
17+
position: Position,
1218
}
1319

1420
impl Enemy {
15-
pub fn new(pos_x: f32) -> Self {
16-
Self { pos_x }
21+
pub fn new(pos_x: f32, asset_manager: &AssetManager) -> Self {
22+
let gopher = asset_manager.get_image("gopher.png");
23+
24+
let position = Position::new(pos_x, -HEIGHT2 + 190., gopher.width(), gopher.height());
25+
26+
Self { position }
1727
}
1828

1929
pub fn draw(
2030
&mut self,
2131
ctx: &mut Context,
2232
camera: &Camera,
23-
resources: &Vec<Image>,
33+
asset_manager: &AssetManager,
2434
) -> GameResult<()> {
25-
const HEIGHT2: f32 = HEIGHT / 2.;
35+
let gopher = asset_manager.get_image("gopher.png");
36+
let gun = asset_manager.get_image("Some(gun).png");
2637

27-
&resources[0].draw_camera(&camera, ctx, Vec2::new(self.pos_x, -HEIGHT2 + 190.), 0.0);
38+
gopher.draw_camera(
39+
&camera,
40+
ctx,
41+
Vec2::new(self.position.pos_start.x, self.position.pos_start.y),
42+
0.0,
43+
)?;
2844

29-
&resources[1].draw_camera(
45+
gun.draw_camera(
3046
&camera,
3147
ctx,
32-
Vec2::new(self.pos_x - 50., -HEIGHT2 + 150.),
48+
Vec2::new(self.position.pos_start.x - 50., -HEIGHT2 + 150.),
3349
0.0,
34-
);
50+
)?;
3551

3652
Ok(())
3753
}
54+
55+
pub fn update(&mut self, player: &Player) {
56+
// Can the enemy see the player?
57+
if player.pos_x - self.position.pos_start.x > -457.0
58+
&& player.pos_x - self.position.pos_start.x < 457.0
59+
{
60+
// TODO: The enemy shoots the player as soon as it see's the player.
61+
}
62+
}
63+
64+
pub fn position(&self) -> Position {
65+
self.position
66+
}
3867
}

src/components/player.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
use ggez::{graphics::Image, Context, GameResult};
1+
use ggez::{Context, GameResult};
22
use ggez_goodies::{
33
camera::{Camera, CameraDraw},
44
nalgebra_glm::Vec2,
55
};
66

7-
use crate::{utils::lerp, HEIGHT};
7+
use crate::{HEIGHT, utils::{AssetManager, lerp}};
8+
9+
const HEIGHT2: f32 = HEIGHT / 2.;
810

911
use super::bullet::Turbofish;
1012

@@ -51,23 +53,24 @@ impl Player {
5153
&mut self,
5254
ctx: &mut Context,
5355
camera: &Camera,
54-
resources: &Vec<Image>,
56+
asset_manager: &AssetManager,
5557
) -> GameResult<()> {
56-
const HEIGHT2: f32 = HEIGHT / 2.;
58+
let ferris = asset_manager.get_image("Some(ferris).png");
59+
let turbofish_sniper = asset_manager.get_image("Some(sniper).png");
5760

58-
&resources[0].draw_camera(
61+
ferris.draw_camera(
5962
&camera,
6063
ctx,
6164
Vec2::new(self.pos_x, (-HEIGHT2 + 155.) + self.pos_y),
6265
0.0,
63-
);
66+
)?;
6467

65-
&resources[1].draw_camera(
68+
turbofish_sniper.draw_camera(
6669
&camera,
6770
ctx,
6871
Vec2::new(self.pos_x + 30., (-HEIGHT2 + 120.) + self.pos_y),
6972
0.0,
70-
);
73+
)?;
7174

7275
Ok(())
7376
}
@@ -119,13 +122,12 @@ impl Player {
119122
}
120123
}
121124

122-
pub fn shoot(&mut self) -> Option<Turbofish> {
123-
const HEIGHT2: f32 = HEIGHT / 2.;
124-
125+
pub fn shoot(&mut self, asset_manager: &AssetManager) -> Option<Turbofish> {
125126
if self.ammo as i32 != 0 {
126127
return Some(Turbofish::new(
127128
self.pos_x + 220.,
128129
(-HEIGHT2 + 106.) + self.pos_y,
130+
asset_manager
129131
));
130132
} else {
131133
None

0 commit comments

Comments
 (0)