Skip to content

Commit 81c21fd

Browse files
Boilerplate for grappling gun
1 parent 50df1eb commit 81c21fd

File tree

6 files changed

+395
-215
lines changed

6 files changed

+395
-215
lines changed

resources/maps/01.map

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
[-4------]_[--8---8---*-]
1+
.comment Define all of our variables
22

3-
.end We **rustaceans** love all animals and we do not want to disappoint them like the gophers. \nWe also have animals in our language too like Cow<>. \nWe just love the correct animals ⌐■_■
3+
.end We **rustaceans** love all animals and we do not want to disappoint them like the gophers. \nWe also have animals in our language too like Cow<>. \nWe just love the correct animals ⌐■_■
4+
.using Turbofish Gun
5+
6+
.comment The map
7+
[-4------]_[--8---8---*-]

src/components/bullet.rs

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1-
use ggez::{Context, GameResult};
1+
use ggez::{
2+
nalgebra::{distance, Point2},
3+
Context, GameResult,
4+
};
25
use ggez_goodies::{
36
camera::{Camera, CameraDraw},
47
nalgebra_glm::Vec2,
58
};
69

710
use crate::utils::{AssetManager, Position};
811

12+
use super::enemy::Enemy;
13+
14+
pub enum PlayerWeapon {
15+
Turbofish(Turbofish),
16+
Grappling(Grappling),
17+
}
18+
919
pub struct Turbofish {
1020
position: Position,
1121
traveled_count: i32,
@@ -60,3 +70,70 @@ impl Turbofish {
6070
self.position
6171
}
6272
}
73+
74+
pub struct Grappling {
75+
position: Position,
76+
traveled_count: i32,
77+
}
78+
79+
impl Grappling {
80+
pub fn new(
81+
pos_x: f32,
82+
pos_y: f32,
83+
_asset_manager: &AssetManager,
84+
enemies: &Vec<Enemy>,
85+
) -> Option<Self> {
86+
let mut position = None;
87+
88+
for enemy in enemies {
89+
if pos_x - enemy.position().pos_start.x < 100.0
90+
&& pos_x - enemy.position().pos_start.x > -300.0
91+
{
92+
position = Some(Position::new(
93+
pos_x,
94+
pos_y,
95+
distance(
96+
&Point2::new(pos_x, 0.0),
97+
&Point2::new(enemy.position().pos_start.x, 0.0),
98+
) as u16,
99+
10,
100+
));
101+
}
102+
}
103+
104+
if position.is_none() {
105+
return None;
106+
}
107+
108+
Some(Self {
109+
position: position.unwrap(),
110+
traveled_count: 0,
111+
})
112+
}
113+
114+
pub fn draw(
115+
&mut self,
116+
_ctx: &mut Context,
117+
_camera: &Camera,
118+
_asset_manager: &AssetManager,
119+
) -> GameResult<()> {
120+
// TODO
121+
122+
Ok(())
123+
}
124+
125+
pub fn go_boom(&mut self) -> bool {
126+
if self.traveled_count < 100 {
127+
self.traveled_count += 1;
128+
self.position.move_by("x+", 10.0);
129+
130+
false
131+
} else {
132+
true
133+
}
134+
}
135+
136+
pub fn _position(&self) -> Position {
137+
self.position
138+
}
139+
}

src/components/player.rs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ use crate::{
1111

1212
const HEIGHT2: f32 = HEIGHT / 2.;
1313

14-
use super::bullet::Turbofish;
14+
use super::{
15+
bullet::{Grappling, PlayerWeapon, Turbofish},
16+
enemy::Enemy,
17+
};
1518

1619
pub enum Direction {
1720
Left,
@@ -125,13 +128,39 @@ impl Player {
125128
}
126129
}
127130

128-
pub fn shoot(&mut self, asset_manager: &AssetManager) -> Option<Turbofish> {
131+
pub fn shoot(
132+
&mut self,
133+
asset_manager: &AssetManager,
134+
gun: &str,
135+
enemies: &Vec<Enemy>,
136+
) -> Option<PlayerWeapon> {
129137
if self.ammo as i32 != 0 {
130-
return Some(Turbofish::new(
131-
self.pos_x + 220.,
132-
(-HEIGHT2 + 106.) + self.pos_y,
133-
asset_manager,
134-
));
138+
match gun {
139+
"Turbofish Gun" => Some(PlayerWeapon::Turbofish(Turbofish::new(
140+
self.pos_x + 220.,
141+
(-HEIGHT2 + 106.) + self.pos_y,
142+
asset_manager,
143+
))),
144+
145+
"Grappling Gun" => {
146+
let gun = Grappling::new(
147+
self.pos_x + 150.,
148+
(-HEIGHT2 + 106.) + self.pos_y,
149+
asset_manager,
150+
enemies,
151+
);
152+
153+
if let Some(grapple) = gun {
154+
Some(PlayerWeapon::Grappling(grapple))
155+
} else {
156+
None
157+
}
158+
}
159+
160+
_ => {
161+
panic!()
162+
}
163+
}
135164
} else {
136165
None
137166
}

0 commit comments

Comments
 (0)