-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_objects.rs
355 lines (332 loc) · 11 KB
/
game_objects.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
use crate::Collider;
use bevy::prelude::*;
use crate::constants::{WALL_BOTTOM, WALL_LEFT, WALL_RIGHT, WALL_THICKNESS, WALL_TOP};
use crate::game_utils::{Bindings, BulletType, Direction, DirectionBlock, DirectionHelper};
use rand::prelude::*;
#[derive(Component)]
pub struct Player {
pub player_number: i32,
pub size: i32,
pub lifes: i32,
pub invulnerable: bool,
pub stunned: bool,
pub powerup: bool,
pub shoot: bool,
pub speed: f32,
pub direction: DirectionHelper,
pub direction_block: DirectionBlock,
pub name: String,
pub bindings: Bindings,
pub power_up_type: Option<BulletType>,
}
#[derive(Component)]
pub struct Bullet {
pub bullet_type: BulletType,
pub speed: f32,
pub area_of_effect: f32,
pub stuns: bool,
pub bounces: bool,
pub bounces_left: i32,
pub direction: DirectionHelper,
pub color: Color,
}
#[derive(Component)]
pub struct Explosion {
pub radius: f32,
}
#[derive(Component)]
pub struct PowerUp {
pub pickup_type: BulletType,
}
#[derive(Bundle)]
pub struct WallBundle {
pub direction: Direction,
pub sprite_bundle: SpriteBundle,
pub collider: Collider,
}
// create totem pls
#[derive(Component)]
pub struct Totem {}
#[derive(Component)]
pub struct Wall {}
#[derive(Component)]
pub struct UINode {}
#[derive(Component)]
pub struct UIText {}
impl Player {
#[allow(clippy::too_many_arguments)]
pub fn new(
number: i32,
entered_name: String,
entered_shootbind: KeyCode,
entered_shoot_specialbind: KeyCode,
entered_upbind: KeyCode,
entered_downbind: KeyCode,
entered_rightbind: KeyCode,
entered_leftbind: KeyCode,
entered_direction: DirectionHelper,
) -> Player {
Player {
player_number: number,
size: 50,
lifes: 3,
invulnerable: false,
stunned: false,
powerup: false,
shoot: true,
speed: 2.5,
direction: entered_direction,
direction_block: DirectionBlock {
up: false,
down: false,
right: false,
left: false,
},
name: entered_name,
bindings: Bindings {
shoot: entered_shootbind,
shoot_special: entered_shoot_specialbind,
up: entered_upbind,
down: entered_downbind,
right: entered_rightbind,
left: entered_leftbind,
},
power_up_type: None,
}
}
pub fn decrement_life(&mut self) {
self.lifes -= 1;
}
pub fn get_bullet_spawn_position(&self) -> (f32, f32) {
let bullet_x = match self.direction.direction_x {
Direction::Right => 43.0,
Direction::Left => -43.0,
_ => 0.0,
};
let bullet_y = match self.direction.direction_y {
Direction::Up => 43.0,
Direction::Down => -43.0,
_ => 0.0,
};
(bullet_x, bullet_y)
}
}
pub fn get_direction_sprite(x: &Direction, y: &Direction) -> &'static str {
match x {
Direction::Right => match y {
Direction::Up => "/assets/images/player/player_right_up.png",
Direction::Down => "/assets/images/player/player_right_down.png",
_ => "/assets/images/player/player_right.png",
},
Direction::Left => match y {
Direction::Up => "/assets/images/player/player_left_up.png",
Direction::Down => "/assets/images/player/player_left_down.png",
_ => "/assets/images/player/player_left.png",
},
_ => match y {
Direction::Up => "/assets/images/player/player_up.png",
Direction::Down => "/assets/images/player/player_down.png",
_ => "",
},
}
}
impl Bullet {
pub fn bullet_from_enum(
entered_bullet_type: Option<&BulletType>,
direction: &DirectionHelper,
) -> Bullet {
match entered_bullet_type.unwrap() {
BulletType::NormalBullet => Self::normal_bullet(direction.clone()),
BulletType::IceBullet => Self::ice_bullet(direction.clone()),
BulletType::ExplosiveBullet => Self::explosive_bullet(direction.clone()),
BulletType::BouncyBullet => Self::bouncy_bullet(direction.clone()),
}
}
pub fn normal_bullet(direction_entered: DirectionHelper) -> Bullet {
Bullet {
bullet_type: BulletType::NormalBullet,
speed: 20.0,
area_of_effect: 1.0,
stuns: false,
bounces: false,
bounces_left: 0,
direction: direction_entered,
color: Color::rgb(1.0, 0.0, 0.0),
}
}
pub fn ice_bullet(direction_entered: DirectionHelper) -> Bullet {
Bullet {
bullet_type: BulletType::IceBullet,
speed: 40.0,
area_of_effect: 1.0,
stuns: true,
bounces: false,
bounces_left: 0,
direction: direction_entered,
color: Color::rgb(0.0, 0.0, 1.0),
}
}
pub fn explosive_bullet(direction_entered: DirectionHelper) -> Bullet {
Bullet {
bullet_type: BulletType::ExplosiveBullet,
speed: 10.0,
area_of_effect: 5.0,
stuns: false,
bounces: false,
bounces_left: 0,
direction: direction_entered,
color: Color::rgb(1.0, 1.0, 0.0),
}
}
pub fn bouncy_bullet(direction_entered: DirectionHelper) -> Bullet {
Bullet {
bullet_type: BulletType::BouncyBullet,
speed: 20.0,
area_of_effect: 1.0,
stuns: false,
bounces: true,
bounces_left: 4,
direction: direction_entered,
color: Color::rgb(0.0, 1.0, 0.0),
}
}
}
impl PowerUp {
pub fn generate_random_position() -> Vec3 {
let mut rng = rand::thread_rng();
Vec3 {
x: rng.gen_range((WALL_LEFT + 15.0)..=(WALL_RIGHT - 15.0)),
y: rng.gen_range((WALL_TOP + 15.0)..=(WALL_BOTTOM - 15.0)),
z: 2.0,
}
}
}
impl WallBundle {
pub fn new(entered_direction: Direction, asset_server: &Res<AssetServer>) -> WallBundle {
WallBundle {
direction: entered_direction.clone(),
sprite_bundle: SpriteBundle {
transform: Transform {
translation: match entered_direction {
Direction::Up => Vec3 {
x: 0.0,
y: WALL_TOP,
z: (2.0),
},
Direction::Down => Vec3 {
x: 0.0,
y: WALL_BOTTOM,
z: (2.0),
},
Direction::Right => Vec3 {
x: WALL_RIGHT,
y: 0.0,
z: (2.0),
},
Direction::Left => Vec3 {
x: WALL_LEFT,
y: 0.0,
z: (2.0),
},
Direction::None => Vec3 {
x: 0.0,
y: 0.0,
z: 2.0,
},
},
scale: match entered_direction {
Direction::Up | Direction::Down => Vec3 {
x: 1616.0,
y: WALL_THICKNESS,
z: (0.0),
},
Direction::Right | Direction::Left => Vec3 {
x: WALL_THICKNESS,
y: 984.0,
z: (0.0),
},
Direction::None => Vec3 {
x: 0.0,
y: 0.0,
z: 0.0,
},
},
..default()
},
sprite: Sprite {
custom_size: Option::Some(Vec2 { x: 1.0, y: 1.0 }),
..default()
},
texture: match entered_direction {
Direction::Up | Direction::Down => {
asset_server.load("/assets/images/walls/bricks_808_8.png")
}
_ => asset_server.load("/assets/images/walls/bricks_8_492_rotate.png"),
},
..default()
},
collider: Collider,
}
}
pub fn new_random_wall() -> WallBundle {
let mut rng = rand::thread_rng();
let direction = Self::convert_int(rng.gen_range(0..=1));
match direction.unwrap() {
Direction::Up => WallBundle {
direction: Direction::Up,
sprite_bundle: SpriteBundle {
transform: Transform {
translation: Vec3 {
x: rng.gen_range(-500..=500) as f32,
y: rng.gen_range(-400..=400) as f32,
z: (2.0),
},
scale: Vec3 {
x: 500.0,
y: WALL_THICKNESS,
z: (0.0),
},
..default()
},
sprite: Sprite {
color: Color::rgb(1.0, 0.0, 0.0),
..default()
},
..default()
},
collider: Collider,
},
_ => WallBundle {
direction: Direction::Right,
sprite_bundle: SpriteBundle {
transform: Transform {
translation: Vec3 {
x: rng.gen_range(-500..=500) as f32,
y: rng.gen_range(-250..=250) as f32,
z: (2.0),
},
scale: Vec3 {
x: WALL_THICKNESS,
y: 500.0,
z: (0.0),
},
..default()
},
sprite: Sprite {
color: Color::rgb(1.0, 0.0, 0.0),
..default()
},
..default()
},
collider: Collider,
},
}
}
pub fn convert_int(number: i32) -> Option<Direction> {
match number {
0 => Some(Direction::Up),
1 => Some(Direction::Right),
_ => None,
}
}
}