Skip to content

Commit b3260a3

Browse files
Ferris can shoot now
1 parent ea43a16 commit b3260a3

File tree

7 files changed

+123
-10
lines changed

7 files changed

+123
-10
lines changed

resources/images/Some(sniper).png

-1.36 KB
Loading

resources/images/Some(turbofish).png

750 Bytes
Loading

src/components/bullet.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use ggez::{Context, GameResult, graphics::Image};
2+
use ggez_goodies::{camera::{Camera, CameraDraw}, nalgebra_glm::Vec2};
3+
4+
pub struct Turbofish {
5+
pub pos_x: f32,
6+
pub pos_y: f32,
7+
8+
traveled_count: i32
9+
}
10+
11+
impl Turbofish {
12+
pub fn new(pos_x: f32, pos_y: f32) -> Self {
13+
Self {
14+
pos_x,
15+
pos_y,
16+
traveled_count: 0
17+
}
18+
}
19+
20+
pub fn draw(&mut self, ctx: &mut Context, camera: &Camera, resources: &Vec<Image>) -> GameResult<()> {
21+
&resources[0].draw_camera(
22+
camera,
23+
ctx,
24+
Vec2::new(self.pos_x, self.pos_y),
25+
1.5708
26+
);
27+
28+
Ok(())
29+
}
30+
31+
pub fn go_boom(&mut self) -> bool {
32+
if self.traveled_count < 100 {
33+
self.traveled_count += 1;
34+
self.pos_x += 10.;
35+
36+
false
37+
}
38+
39+
else {
40+
true
41+
}
42+
}
43+
}

src/components/enemy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use graphics::Image;
88
use crate::HEIGHT;
99

1010
pub struct Enemy {
11-
pos_x: f32,
11+
pub pos_x: f32,
1212
}
1313

1414
impl Enemy {

src/components/player.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use ggez_goodies::{
66

77
use crate::HEIGHT;
88

9+
use super::bullet::Turbofish;
10+
911
pub struct Player {
1012
pub pos_x: f32,
1113
pub pos_y: f32,
@@ -46,7 +48,7 @@ impl Player {
4648
&resources[1].draw_camera(
4749
&camera,
4850
ctx,
49-
Vec2::new(self.pos_x - 50., (-HEIGHT2 + 150.) + self.pos_y),
51+
Vec2::new(self.pos_x + 30., (-HEIGHT2 + 120.) + self.pos_y),
5052
0.0,
5153
);
5254

@@ -73,4 +75,18 @@ impl Player {
7375
self.pos_y -= self.velocity;
7476
}
7577
}
78+
79+
pub fn shoot(&mut self) -> Option<Turbofish> {
80+
const HEIGHT2: f32 = HEIGHT / 2.;
81+
82+
if self.ammo != 0 {
83+
self.ammo -= 1;
84+
85+
return Some(Turbofish::new(self.pos_x + 220., (-HEIGHT2 + 106.) + self.pos_y));
86+
}
87+
88+
else {
89+
None
90+
}
91+
}
7692
}

src/game.rs

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,18 @@ use ggez::{
99
use ggez_goodies::{camera::Camera, nalgebra_glm::Vec2};
1010
use graphics::{Font, Image, Mesh, TextFragment};
1111

12-
use crate::{
13-
components::{
14-
enemy::Enemy,
15-
player::Player,
16-
tile::{Tile, TileType},
17-
},
18-
HEIGHT, WIDTH,
19-
};
12+
use crate::{HEIGHT, Screen, WIDTH, components::{bullet::Turbofish, enemy::Enemy, player::Player, tile::{Tile, TileType}}};
2013

2114
pub struct Game {
2215
ground: Vec<Tile>,
2316
enemies: Vec<Enemy>,
17+
player_bullets: Vec<Turbofish>,
2418
player: Player,
2519

2620
ground_resources: Vec<Image>,
2721
enemy_resources: Vec<Image>,
2822
player_resources: Vec<Image>,
23+
bullet_resources: Vec<Image>,
2924

3025
consolas: Font,
3126

@@ -100,6 +95,7 @@ impl Game {
10095
ground,
10196
enemies,
10297
player,
98+
player_bullets: vec![],
10399

104100
ground_resources: vec![
105101
Image::new(ctx, "/images/ground_left.png").unwrap(),
@@ -117,6 +113,10 @@ impl Game {
117113
Image::new(ctx, "/images/Some(sniper).png").unwrap(),
118114
],
119115

116+
bullet_resources: vec![
117+
Image::new(ctx, "/images/Some(turbofish).png").unwrap(),
118+
],
119+
120120
ui_resources: vec![Image::new(ctx, "/images/Some(ammo).png").unwrap()],
121121

122122
camera,
@@ -164,6 +164,15 @@ impl Game {
164164
text: self.player.ammo.to_string(),
165165
font: Some(self.consolas),
166166
scale: Some(Scale::uniform(30.0)),
167+
color: {
168+
if self.player.ammo > 10 / 2 {
169+
Some(Color::from_rgb(255, 255, 255))
170+
}
171+
172+
else {
173+
Some(Color::from_rgb(255, 80, 76))
174+
}
175+
},
167176

168177
..Default::default()
169178
};
@@ -174,6 +183,10 @@ impl Game {
174183
DrawParam::default().dest(Point2::new(30.0, 25.0)),
175184
)?;
176185

186+
for fish in &mut self.player_bullets {
187+
fish.draw(ctx, &self.camera, &self.bullet_resources)?;
188+
}
189+
177190
graphics::present(ctx)
178191
}
179192

@@ -197,6 +210,41 @@ impl Game {
197210
self.camera
198211
.move_to(Vec2::new(self.player.pos_x, self.player.pos_y));
199212

213+
if self.player.pos_y < -800. {
214+
return Ok(Some(Screen::Dead));
215+
}
216+
217+
for i in 0..self.enemies.len() {
218+
let go = &self.enemies[i];
219+
220+
let go_start_x = go.pos_x;
221+
let go_end_x = go.pos_x + 100.;
222+
223+
let mut done: bool = false;
224+
225+
for fish in &mut self.player_bullets {
226+
if fish.pos_x >= go_start_x && fish.pos_x <= go_end_x {
227+
self.enemies.remove(i);
228+
229+
done = true;
230+
}
231+
}
232+
233+
if done {
234+
break;
235+
}
236+
}
237+
238+
for i in 0..self.player_bullets.len() {
239+
let fish = &mut self.player_bullets[i];
240+
241+
if fish.go_boom() {
242+
self.player_bullets.remove(i);
243+
244+
break;
245+
}
246+
}
247+
200248
Ok(None)
201249
}
202250

@@ -210,6 +258,11 @@ impl Game {
210258
}
211259
KeyCode::Space => {
212260
self.player.go_boom();
261+
},
262+
KeyCode::S => {
263+
if let Some(fish) = self.player.shoot() {
264+
self.player_bullets.push(fish);
265+
}
213266
}
214267
_ => (),
215268
}

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ mod components {
1616
pub mod enemy;
1717
pub mod player;
1818
pub mod tile;
19+
pub mod bullet;
1920
}
2021

2122
const WIDTH: f32 = 1000.0;

0 commit comments

Comments
 (0)