bevyjam/src/levels.rs

65 lines
1.5 KiB
Rust
Raw Normal View History

2022-08-23 10:02:58 +00:00
#![allow(clippy::too_many_arguments)]
mod game_over;
mod level0;
mod level1;
use crate::game::*;
use bevy::prelude::*;
pub fn setup_level(
commands: &mut Commands,
current_level: &mut ResMut<CurrentLevel>,
level_startup_event: &mut EventWriter<LevelStartupEvent>,
2022-08-23 22:45:57 +00:00
camera_query: &mut Query<&mut Transform, With<Camera>>,
2022-08-23 10:02:58 +00:00
level_id: LevelId,
) {
let level_entity = commands
.spawn()
.insert(Level)
.id();
current_level.0 = Some(level_id);
2022-08-23 22:45:57 +00:00
camera_query.single_mut().translation = Default::default();
2022-08-23 10:02:58 +00:00
level_startup_event.send(LevelStartupEvent(level_entity));
}
pub fn despawn_level(mut commands: Commands, level_query: Query<Entity, With<Level>>) {
for entity in level_query.iter() {
commands.entity(entity).despawn_recursive();
}
}
// This is a bad design, to be refactored some days
pub fn post_setup_level(
mut commands: Commands,
character_meshes: Res<CharacterMeshes>,
mut materials: ResMut<Assets<ColorMaterial>>,
current_level: Res<CurrentLevel>,
mut level_startup_event: EventReader<LevelStartupEvent>,
asset_server: Res<AssetServer>,
2022-08-23 15:02:13 +00:00
audio: Res<crossbeam_channel::Sender<AudioMsg>>,
2022-08-23 10:02:58 +00:00
) {
for LevelStartupEvent(level_entity) in level_startup_event.iter() {
if let Some(level_id) = current_level.0 {
match level_id.0 {
0 => level0::setup(
&mut commands,
&character_meshes,
&mut materials,
&audio,
),
1 => level1::setup(
&mut commands,
&character_meshes,
&mut materials,
&audio,
),
_ => game_over::setup(&mut commands, &asset_server),
2022-08-23 10:02:58 +00:00
}
}
}
}