Skip to content

Commit

Permalink
feat: Add a camera plugin, add draft level component
Browse files Browse the repository at this point in the history
  • Loading branch information
uggla committed Apr 30, 2024
1 parent 96dea15 commit f3b08fd
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
Binary file modified assets/menu.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions src/camera.rs
Original file line number Diff line number Diff line change
@@ -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<Camera2d>>) {
let mut camera = camera_query.single_mut();
camera.translation.x = -WINDOW_WIDTH / 2.0;
}
53 changes: 42 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod camera;
mod collision;
mod helpers;
mod menu;
Expand All @@ -14,6 +15,7 @@ use bevy_rapier2d::prelude::*;
use text_syllable::TextSyllablePlugin;

use crate::{
camera::CameraPlugin,
collision::CollisionPlugin,
helpers::tiled::{TiledMap, TilesetLayerToStorageEntity},
menu::MenuPlugin,
Expand Down Expand Up @@ -42,6 +44,7 @@ fn main() {
// prevents blurry sprites
.set(ImagePlugin::default_nearest()),
StatesPlugin,
CameraPlugin,
MenuPlugin,
TilemapPlugin,
helpers::tiled::TiledMapPlugin,
Expand Down Expand Up @@ -72,32 +75,60 @@ 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<AssetServer>) {
commands.spawn(Camera2dBundle {
transform: Transform::from_xyz(-WINDOW_WIDTH / 2.0, 0.0, 0.0),
..Default::default()
});
let sprite_handle: Handle<Image> = 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<helpers::tiled::TiledMap> = asset_server.load("level01.tmx");
commands.spawn((
helpers::tiled::TiledMapBundle {
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<CurrentLevel>,
mut tile_query: Query<&mut TileVisible>,
map_query: Query<(&Level, &TilesetLayerToStorageEntity), With<Level>>,
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)]
Expand Down Expand Up @@ -205,7 +236,7 @@ fn update_text(
&mut TilesetLayerToStorageEntity,
&TilemapRenderSettings,
),
With<Level01>,
With<Level>,
>,

tile_storage_query: Query<(Entity, &TileStorage)>,
Expand Down

0 comments on commit f3b08fd

Please sign in to comment.