Skip to content

Commit

Permalink
feat: Use screen_map to move the camera to level start screen
Browse files Browse the repository at this point in the history
  • Loading branch information
uggla committed May 4, 2024
1 parent 9e39223 commit 8e370dd
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 19 deletions.
30 changes: 26 additions & 4 deletions src/camera.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
use bevy::prelude::*;

use crate::{state::AppState, WINDOW_WIDTH};
use crate::{
level::{CurrentLevel, Level},
state::AppState,
};
pub struct CameraPlugin;

impl Plugin for CameraPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, setup_camera);
app.add_systems(OnEnter(AppState::GameCreate), move_camera);
app.add_systems(
OnEnter(AppState::GameCreate),
move_camera_to_level_start_screen,
)
.add_systems(OnEnter(AppState::StartMenu), move_camera_to_center);
}
}

Expand All @@ -17,7 +24,22 @@ fn setup_camera(mut commands: Commands) {
});
}

fn move_camera(mut camera_query: Query<&mut Transform, With<Camera2d>>) {
fn move_camera_to_center(mut camera_query: Query<&mut Transform, With<Camera2d>>) {
let mut camera = camera_query.single_mut();
camera.translation.x = -WINDOW_WIDTH / 2.0;
camera.translation = Vec3::new(0.0, 0.0, 0.0);
}

fn move_camera_to_level_start_screen(
current_level: Res<CurrentLevel>,
levels: Query<&Level, With<Level>>,
mut camera_query: Query<&mut Transform, With<Camera2d>>,
) {
let mut camera = camera_query.single_mut();

for level in levels.iter() {
if level.id == current_level.id {
camera.translation = level.map.get_start_screen().get_center().extend(0.0);
break;
}
}
}
36 changes: 26 additions & 10 deletions src/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,27 @@ use bevy::prelude::*;
use bevy_ecs_tilemap::tiles::{TileStorage, TileVisible};

use crate::{
helpers::{self, tiled::TilesetLayerToStorageEntity},
helpers::{
self,
tiled::{TiledMap, TilesetLayerToStorageEntity},
},
state::AppState,
WINDOW_WIDTH,
WINDOW_HEIGHT, WINDOW_WIDTH,
};

#[derive(Resource, Eq, PartialEq)]
pub struct CurrentLevel(Level);
use crate::screen_map::Map;

#[derive(Component, Eq, PartialEq)]
pub struct Level(u8);
#[derive(Resource, PartialEq)]
pub struct CurrentLevel {
pub id: u8,
}

#[derive(Component, PartialEq)]
pub struct Level {
pub id: u8,
pub handle: Handle<TiledMap>,
pub map: Map,
}

#[derive(Component)]
struct Menu;
Expand All @@ -35,7 +46,7 @@ impl Plugin for LevelPlugin {
toggle_menu_background_visibility,
),
)
.insert_resource(CurrentLevel(Level(1)));
.insert_resource(CurrentLevel { id: 1 });
}
}

Expand All @@ -54,10 +65,14 @@ fn setup_background(mut commands: Commands, asset_server: Res<AssetServer>) {
let map_handle: Handle<helpers::tiled::TiledMap> = asset_server.load("level01.tmx");
commands.spawn((
helpers::tiled::TiledMapBundle {
tiled_map: map_handle,
tiled_map: map_handle.clone(),
..Default::default()
},
Level(1),
Level {
id: 1,
handle: map_handle.clone(),
map: Map::new("SO", WINDOW_WIDTH as usize, WINDOW_HEIGHT as usize),
},
));
}

Expand All @@ -68,7 +83,7 @@ fn toggle_level_background_visibility(
tile_storage_query: Query<(Entity, &TileStorage)>,
) {
for (level, tileset_layer_storage) in map_query.iter() {
if *level == current_level.0 {
if level.id == current_level.id {
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() {
Expand All @@ -77,6 +92,7 @@ fn toggle_level_background_visibility(
}
}
}
break;
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/screen_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bevy::prelude::*;
use std::ops::Range;

#[derive(Debug, PartialEq)]
struct Screen {
pub struct Screen {
x_range: Range<f32>,
y_range: Range<f32>,
start_screen: bool,
Expand All @@ -15,7 +15,7 @@ impl Screen {
self.x_range.contains(&point.x) && self.y_range.contains(&point.y)
}

fn get_center(&self) -> Vec2 {
pub fn get_center(&self) -> Vec2 {
Vec2::new(
(self.x_range.start + self.x_range.end) / 2.0,
(self.y_range.start + self.y_range.end) / 2.0 - 1f32,
Expand All @@ -24,7 +24,7 @@ impl Screen {
}

#[derive(Debug, PartialEq)]
struct Map {
pub struct Map {
width: usize,
height: usize,
screen_width: usize,
Expand Down Expand Up @@ -95,7 +95,7 @@ impl Map {
data,
}
}
fn tiled_to_bevy_coord(&self, tiled_coord: Vec2) -> Vec2 {
pub fn tiled_to_bevy_coord(&self, tiled_coord: Vec2) -> Vec2 {
Vec2::new(
tiled_coord.x - (self.width / 2) as f32,
(tiled_coord.y - (self.height / 2) as f32) + 1f32,
Expand Down Expand Up @@ -234,7 +234,7 @@ impl Map {
.contains(&camera_points[p2]))
}

fn get_start_screen(&self) -> &Screen {
pub fn get_start_screen(&self) -> &Screen {
self.data
.iter()
.flatten()
Expand Down

0 comments on commit 8e370dd

Please sign in to comment.