Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksa2808 committed Nov 18, 2023
1 parent c3a5083 commit 6a5aba4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 43 deletions.
53 changes: 13 additions & 40 deletions src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use crate::{
},
resources::*,
types::Direction,
utils::{format_hud_time, generate_item_at_position, get_x, get_y, setup_round},
utils::{
format_hud_time, generate_item_at_position, get_x, get_y, setup_round, spawn_burning_item,
},
AppState, GgrsConfig,
};

Expand Down Expand Up @@ -383,21 +385,7 @@ pub fn pick_up_item(
(Some(_), Some(_)) => {
println!("Multiple players arrived at item position ({:?}) at the same time! In the ensuing chaos the item was destroyed...", ip);
commands.entity(ie).despawn_recursive();
commands.spawn((
SpriteBundle {
texture: game_textures.burning_item.clone(),
transform: Transform::from_xyz(get_x(ip.x), get_y(ip.y), 20.0),
sprite: Sprite {
custom_size: Some(Vec2::new(TILE_WIDTH as f32, TILE_HEIGHT as f32)),
..Default::default()
},
..Default::default()
},
*ip,
BurningItem {
expiration_frame: frame_count.frame + FPS / 2,
},
));
spawn_burning_item(&mut commands, &game_textures, *ip, frame_count.frame);
}
(None, Some(_)) => unreachable!(),
}
Expand Down Expand Up @@ -631,7 +619,7 @@ pub fn crumbling_tick(
// drop power-up
let roll = session_rng.0.gen_range(0..100);
if roll < ITEM_SPAWN_CHANCE_PERCENTAGE {
generate_item_at_position(&mut session_rng.0, *position, &mut commands, &game_textures);
generate_item_at_position(&mut session_rng.0, &mut commands, &game_textures, *position);
}
}
}
Expand All @@ -647,10 +635,11 @@ pub fn burning_item_tick(
return;
}

for (entity, burning_item) in query.iter() {
if frame_count.frame >= burning_item.expiration_frame {
commands.entity(entity).despawn_recursive();
}
for (entity, _) in query
.iter()
.filter(|(_, bi)| frame_count.frame >= bi.expiration_frame)
{
commands.entity(entity).despawn_recursive();
}
}

Expand Down Expand Up @@ -816,25 +805,9 @@ pub fn item_burn(

let fire_positions: HashSet<Position> = query2.iter().copied().collect();

for (entity, position) in query.iter() {
if fire_positions.contains(position) {
commands.entity(entity).despawn_recursive();
commands.spawn((
SpriteBundle {
texture: game_textures.burning_item.clone(),
transform: Transform::from_xyz(get_x(position.x), get_y(position.y), 20.0),
sprite: Sprite {
custom_size: Some(Vec2::new(TILE_WIDTH as f32, TILE_HEIGHT as f32)),
..Default::default()
},
..Default::default()
},
*position,
BurningItem {
expiration_frame: frame_count.frame + FPS / 2,
},
));
}
for (entity, &position) in query.iter().filter(|(_, p)| fire_positions.contains(*p)) {
commands.entity(entity).despawn_recursive();
spawn_burning_item(&mut commands, &game_textures, position, frame_count.frame);
}
}

Expand Down
30 changes: 27 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ use rand::{rngs::StdRng, seq::IteratorRandom, Rng};

use crate::{
components::{
BombSatchel, Destructible, GameTimerDisplay, HUDRoot, Item, Penguin, PenguinPortrait,
PenguinPortraitDisplay, Player, Position, Solid, UIComponent, UIRoot, Wall,
BombSatchel, BurningItem, Destructible, GameTimerDisplay, HUDRoot, Item, Penguin,
PenguinPortrait, PenguinPortraitDisplay, Player, Position, Solid, UIComponent, UIRoot,
Wall,
},
constants::{
COLORS, FPS, HUD_HEIGHT, PIXEL_SCALE, ROUND_DURATION_SECS, TILE_HEIGHT, TILE_WIDTH,
Expand Down Expand Up @@ -417,9 +418,9 @@ pub fn setup_round(

pub fn generate_item_at_position(
rng: &mut StdRng,
position: Position,
commands: &mut Commands,
game_textures: &GameTextures,
position: Position,
) {
let roll = rng.gen::<usize>() % 100;

Expand Down Expand Up @@ -451,3 +452,26 @@ pub fn generate_item_at_position(
))
.add_rollback();
}

pub fn spawn_burning_item(
commands: &mut Commands,
game_textures: &GameTextures,
position: Position,
current_frame: usize,
) {
commands.spawn((
SpriteBundle {
texture: game_textures.burning_item.clone(),
transform: Transform::from_xyz(get_x(position.x), get_y(position.y), 20.0),
sprite: Sprite {
custom_size: Some(Vec2::new(TILE_WIDTH as f32, TILE_HEIGHT as f32)),
..Default::default()
},
..Default::default()
},
position,
BurningItem {
expiration_frame: current_frame + FPS / 2,
},
));
}

0 comments on commit 6a5aba4

Please sign in to comment.