diff --git a/assets/menu.jpg b/assets/menu.jpg index f33d7f7..ffa01a8 100644 Binary files a/assets/menu.jpg and b/assets/menu.jpg differ diff --git a/src/camera.rs b/src/camera.rs new file mode 100644 index 0000000..f0adceb --- /dev/null +++ b/src/camera.rs @@ -0,0 +1,26 @@ +use bevy::prelude::*; + +use crate::{state::AppState, WINDOW_WIDTH}; +pub struct CameraPlugin; + +#[derive(SystemSet, Clone, Hash, Debug, PartialEq, Eq)] +pub struct CollisionState; + +impl Plugin for CameraPlugin { + fn build(&self, app: &mut App) { + app.add_systems(Startup, setup_camera); + app.add_systems(OnEnter(AppState::GameCreate), move_camera); + } +} + +fn setup_camera(mut commands: Commands) { + commands.spawn(Camera2dBundle { + transform: Transform::from_xyz(0.0, 0.0, 0.0), + ..Default::default() + }); +} + +fn move_camera(mut camera_query: Query<&mut Transform, With>) { + let mut camera = camera_query.single_mut(); + camera.translation.x = -WINDOW_WIDTH / 2.0; +} diff --git a/src/main.rs b/src/main.rs index a851a3c..7002912 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +mod camera; mod collision; mod helpers; mod menu; @@ -14,6 +15,7 @@ use bevy_rapier2d::prelude::*; use text_syllable::TextSyllablePlugin; use crate::{ + camera::CameraPlugin, collision::CollisionPlugin, helpers::tiled::{TiledMap, TilesetLayerToStorageEntity}, menu::MenuPlugin, @@ -42,6 +44,7 @@ fn main() { // prevents blurry sprites .set(ImagePlugin::default_nearest()), StatesPlugin, + CameraPlugin, MenuPlugin, TilemapPlugin, helpers::tiled::TiledMapPlugin, @@ -72,17 +75,27 @@ fn main() { OnEnter(AppState::StartMenu), toggle_level_background_visibility, ) + .insert_resource(CurrentLevel(Level(1))) .run(); } -#[derive(Component)] -struct Level01; +#[derive(Resource, Eq, PartialEq)] +pub struct CurrentLevel(Level); + +#[derive(Component, Eq, PartialEq)] +pub struct Level(u8); fn setup_background(mut commands: Commands, asset_server: Res) { - commands.spawn(Camera2dBundle { - transform: Transform::from_xyz(-WINDOW_WIDTH / 2.0, 0.0, 0.0), - ..Default::default() - }); + let sprite_handle: Handle = asset_server.load("menu.jpg"); + + commands.spawn(( + SpriteBundle { + texture: sprite_handle, + transform: Transform::from_xyz(-(WINDOW_WIDTH / 2.0 - 720.0 / 2.0), 0.0, 0.0), + ..default() + }, + Level(0), + )); let map_handle: Handle = asset_server.load("level01.tmx"); commands.spawn(( @@ -90,14 +103,32 @@ fn setup_background(mut commands: Commands, asset_server: Res) { tiled_map: map_handle, ..Default::default() }, - Level01, + Level(1), )); } -fn toggle_level_background_visibility(mut tile_query: Query<&mut TileVisible>) { - for mut tile_visible in tile_query.iter_mut() { - tile_visible.0 = !tile_visible.0; +fn toggle_level_background_visibility( + current_level: Res, + mut tile_query: Query<&mut TileVisible>, + map_query: Query<(&Level, &TilesetLayerToStorageEntity), With>, + tile_storage_query: Query<(Entity, &TileStorage)>, +) { + for (level, tileset_layer_storage) in map_query.iter() { + if *level == current_level.0 { + for layer_entity in tileset_layer_storage.get_entities() { + if let Ok((_, layer_tile_storage)) = tile_storage_query.get(*layer_entity) { + for tile in layer_tile_storage.iter().flatten() { + let mut tile_visible = tile_query.get_mut(*tile).unwrap(); + tile_visible.0 = !tile_visible.0; + } + } + } + } } + + // for mut tile_visible in tile_query.iter_mut() { + // tile_visible.0 = !tile_visible.0; + // } } #[derive(Component)] @@ -205,7 +236,7 @@ fn update_text( &mut TilesetLayerToStorageEntity, &TilemapRenderSettings, ), - With, + With, >, tile_storage_query: Query<(Entity, &TileStorage)>,