Skip to content

Commit e4a91d5

Browse files
Add the user profile ui stuff in the game.rs file
1 parent 94e7030 commit e4a91d5

File tree

7 files changed

+98
-29
lines changed

7 files changed

+98
-29
lines changed

resources/images/Some(fish).png

2.75 KB
Loading

resources/images/Some(profile).png

14 KB
Loading
-1.78 KB
Binary file not shown.

resources/images/apple_reference.png

-1.97 KB
Binary file not shown.

src/components/player.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ pub enum Direction {
1717
pub struct Player {
1818
pub pos_x: f32,
1919
pub pos_y: f32,
20+
2021
pub ammo: i32,
22+
pub health: i32,
2123

2224
gravity: f32,
2325
velocity: f32,
@@ -37,6 +39,7 @@ impl Player {
3739
going_boom: false,
3840
lerp_to: None,
3941
direction: Direction::None,
42+
health: 100,
4043
}
4144
}
4245

src/game.rs

Lines changed: 91 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{io::Read, sync::Mutex};
33
use ggez::{
44
audio::{SoundSource, Source},
55
event::KeyCode,
6-
graphics::{self, Color, DrawParam, Scale, Shader, Text},
6+
graphics::{self, Color, DrawParam, Shader},
77
mint,
88
nalgebra::Point2,
99
timer, Context, GameResult,
@@ -13,7 +13,8 @@ use ggez_goodies::{
1313
nalgebra_glm::Vec2,
1414
particle::{EmissionShape, ParticleSystem, ParticleSystemBuilder, Transition},
1515
};
16-
use graphics::{Font, GlBackendSpec, Image, ShaderGeneric, TextFragment};
16+
use graphics::{Font, GlBackendSpec, Image, ShaderGeneric};
17+
use mint::Vector2;
1718
use rand::Rng;
1819

1920
use crate::{
@@ -25,7 +26,7 @@ use crate::{
2526
player::{Direction, Player},
2627
tile::{Tile, TileType},
2728
},
28-
utils::lerp,
29+
utils::{lerp, remap},
2930
Screen, HEIGHT, WIDTH,
3031
};
3132

@@ -55,6 +56,7 @@ pub struct Game {
5556
barrel_resources: Vec<Image>,
5657
cloud_resources: Vec<Image>,
5758

59+
#[allow(dead_code)]
5860
consolas: Font,
5961

6062
camera: Camera,
@@ -189,7 +191,10 @@ impl Game {
189191

190192
bullet_resources: vec![Image::new(ctx, "/images/Some(turbofish).png").unwrap()],
191193

192-
ui_resources: vec![Image::new(ctx, "/images/Some(ammo).png").unwrap()],
194+
ui_resources: vec![
195+
Image::new(ctx, "/images/Some(profile).png").unwrap(),
196+
Image::new(ctx, "/images/Some(fish).png").unwrap(),
197+
],
193198

194199
barrel_resources: vec![Image::new(ctx, "/images/Some(barrel).png").unwrap()],
195200

@@ -249,43 +254,100 @@ impl Game {
249254
self.player
250255
.draw(ctx, &self.camera, &self.player_resources)?;
251256

257+
// Player Bullets
258+
for fish in &mut self.player_bullets {
259+
fish.draw(ctx, &self.camera, &self.bullet_resources)?;
260+
}
261+
262+
// Particles
263+
for sys in &mut self.particles {
264+
&sys.0
265+
.draw_camera(&self.camera, ctx, Vec2::new(sys.1, sys.2), 0.);
266+
}
267+
268+
// User Profile, etc..
269+
self.draw_ui(ctx)?;
270+
271+
graphics::present(ctx)
272+
}
273+
274+
fn draw_ui(&mut self, ctx: &mut Context) -> GameResult<()> {
252275
graphics::draw(
253276
ctx,
254277
&self.ui_resources[0],
255-
DrawParam::default().dest(Point2::new(10.0, 10.0)),
278+
DrawParam::default()
279+
.dest(Point2::new(10.0, 10.0))
280+
.scale(Vector2 { x: 0.5, y: 0.5 }),
256281
)?;
257282

258-
let ammo_frag = TextFragment {
259-
text: self.player.ammo.to_string(),
260-
font: Some(self.consolas),
261-
scale: Some(Scale::uniform(30.0)),
262-
color: {
263-
if self.player.ammo > 10 / 2 {
264-
Some(Color::from_rgb(255, 255, 255))
265-
} else {
266-
Some(Color::from_rgb(255, 80, 76))
267-
}
268-
},
283+
let ammo_rect = graphics::Mesh::new_rectangle(
284+
ctx,
285+
graphics::DrawMode::fill(),
286+
graphics::Rect::new(
287+
((self.ui_resources[0].width() / 2) + 10) as f32,
288+
(self.ui_resources[0].height() / 3) as f32,
289+
150.,
290+
15.,
291+
),
292+
Color::from_rgb(54, 50, 49),
293+
)?;
269294

270-
..Default::default()
271-
};
295+
let hp_rect = graphics::Mesh::new_rectangle(
296+
ctx,
297+
graphics::DrawMode::fill(),
298+
graphics::Rect::new(
299+
((self.ui_resources[0].width() / 2) + 10) as f32,
300+
(self.ui_resources[0].height() / 5) as f32,
301+
150.,
302+
15.,
303+
),
304+
Color::from_rgb(54, 50, 49),
305+
)?;
272306

273-
graphics::draw(
307+
let cur_ammo_rect = graphics::Mesh::new_rectangle(
274308
ctx,
275-
&Text::new(ammo_frag),
276-
DrawParam::default().dest(Point2::new(30.0, 25.0)),
309+
graphics::DrawMode::fill(),
310+
graphics::Rect::new(
311+
((self.ui_resources[0].width() / 2) + 10) as f32,
312+
(self.ui_resources[0].height() / 3) as f32,
313+
remap(self.player.ammo as f32, 0., 10., 0., 150.),
314+
15.,
315+
),
316+
Color::from_rgb(21, 156, 228),
277317
)?;
278318

279-
for fish in &mut self.player_bullets {
280-
fish.draw(ctx, &self.camera, &self.bullet_resources)?;
281-
}
319+
let cur_hp_rect = graphics::Mesh::new_rectangle(
320+
ctx,
321+
graphics::DrawMode::fill(),
322+
graphics::Rect::new(
323+
((self.ui_resources[0].width() / 2) + 10) as f32,
324+
(self.ui_resources[0].height() / 5) as f32,
325+
remap(self.player.health as f32, 0., 100., 0., 150.),
326+
15.,
327+
),
328+
Color::from_rgb(34, 205, 124),
329+
)?;
282330

283-
for sys in &mut self.particles {
284-
&sys.0
285-
.draw_camera(&self.camera, ctx, Vec2::new(sys.1, sys.2), 0.);
286-
}
331+
graphics::draw(ctx, &ammo_rect, DrawParam::default())?;
287332

288-
graphics::present(ctx)
333+
graphics::draw(ctx, &hp_rect, DrawParam::default())?;
334+
335+
graphics::draw(ctx, &cur_ammo_rect, DrawParam::default())?;
336+
337+
graphics::draw(ctx, &cur_hp_rect, DrawParam::default())?;
338+
339+
graphics::draw(
340+
ctx,
341+
&self.ui_resources[1],
342+
DrawParam::default()
343+
.dest(Point2::new(
344+
((self.ui_resources[0].width() / 2) - 10) as f32,
345+
(self.ui_resources[0].height() / 3) as f32,
346+
))
347+
.scale(Vector2 { x: 0.7, y: 0.7 }),
348+
)?;
349+
350+
Ok(())
289351
}
290352

291353
pub fn update(&mut self, ctx: &mut Context) -> GameResult<Option<crate::Screen>> {

src/utils.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
pub fn lerp(from: f32, to: f32, dt: f32) -> f32 {
22
return from + dt * (to - from);
33
}
4+
5+
pub fn remap(n: f32, start1: f32, stop1: f32, start2: f32, stop2: f32) -> f32 {
6+
((n - start1) / (stop1 - start1)) * (stop2 - start2) + start2
7+
}

0 commit comments

Comments
 (0)