From 8e370dd0a46a426b8093d823990625ce41adbc1a Mon Sep 17 00:00:00 2001 From: Uggla Date: Sun, 5 May 2024 01:48:50 +0200 Subject: [PATCH] feat: Use screen_map to move the camera to level start screen --- src/camera.rs | 30 ++++++++++++++++++++++++++---- src/level.rs | 36 ++++++++++++++++++++++++++---------- src/screen_map.rs | 10 +++++----- 3 files changed, 57 insertions(+), 19 deletions(-) diff --git a/src/camera.rs b/src/camera.rs index aaf1b05..db66ffb 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -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); } } @@ -17,7 +24,22 @@ fn setup_camera(mut commands: Commands) { }); } -fn move_camera(mut camera_query: Query<&mut Transform, With>) { +fn move_camera_to_center(mut camera_query: Query<&mut Transform, With>) { 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, + levels: Query<&Level, With>, + mut camera_query: Query<&mut Transform, With>, +) { + 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; + } + } } diff --git a/src/level.rs b/src/level.rs index eb6e68e..cfd1a35 100644 --- a/src/level.rs +++ b/src/level.rs @@ -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, + pub map: Map, +} #[derive(Component)] struct Menu; @@ -35,7 +46,7 @@ impl Plugin for LevelPlugin { toggle_menu_background_visibility, ), ) - .insert_resource(CurrentLevel(Level(1))); + .insert_resource(CurrentLevel { id: 1 }); } } @@ -54,10 +65,14 @@ fn setup_background(mut commands: Commands, asset_server: Res) { let map_handle: Handle = 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), + }, )); } @@ -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() { @@ -77,6 +92,7 @@ fn toggle_level_background_visibility( } } } + break; } } } diff --git a/src/screen_map.rs b/src/screen_map.rs index 1bfcc5d..41ae79a 100644 --- a/src/screen_map.rs +++ b/src/screen_map.rs @@ -3,7 +3,7 @@ use bevy::prelude::*; use std::ops::Range; #[derive(Debug, PartialEq)] -struct Screen { +pub struct Screen { x_range: Range, y_range: Range, start_screen: bool, @@ -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, @@ -24,7 +24,7 @@ impl Screen { } #[derive(Debug, PartialEq)] -struct Map { +pub struct Map { width: usize, height: usize, screen_width: usize, @@ -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, @@ -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()