From b1ae4b572ab5f1d98523de313ccfb2c85809a0ef Mon Sep 17 00:00:00 2001 From: Daniel Thompson Date: Sat, 13 Jan 2024 22:07:14 +0000 Subject: [PATCH] Level 2 --- src/assets/level2.tmx | 361 ++++++++++++++++++++++++++++++++++++++ src/assets/mod.rs | 1 + src/main.rs | 87 +++++---- tiled/level2.tmx | 320 +++++++++++++++++++++++++++++++++ tiled/racer.tiled-session | 25 ++- 5 files changed, 755 insertions(+), 39 deletions(-) create mode 100644 src/assets/level2.tmx create mode 100644 tiled/level2.tmx diff --git a/src/assets/level2.tmx b/src/assets/level2.tmx new file mode 100644 index 0000000..41ceeee --- /dev/null +++ b/src/assets/level2.tmx @@ -0,0 +1,361 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101, +0,0,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101, +0,0,101,101,101,101,0,0,0,0,0,0,0,0,0,0,0,0,101,101,101,101, +0,0,101,101,101,0,0,0,0,0,0,0,0,0,0,0,0,0,0,101,101,101, +0,0,101,101,0,0,102,103,103,103,103,103,103,103,103,103,103,104,0,0,101,101, +0,0,101,101,0,0,93,101,101,101,101,101,101,101,101,101,101,95,0,0,101,101, +0,0,101,101,0,0,93,101,101,101,101,101,101,101,101,101,101,95,0,0,101,101, +0,0,101,101,0,0,93,101,101,101,101,101,101,101,101,101,101,95,0,0,101,101, +0,0,101,101,0,0,93,101,101,101,101,101,101,101,101,101,101,95,0,0,101,101, +0,0,101,101,0,0,98,99,99,99,99,99,99,99,99,99,99,100,0,0,101,101, +0,0,101,101,101,0,0,0,0,0,0,0,0,0,0,0,0,0,0,101,101,101, +0,0,101,101,101,101,0,0,0,0,0,0,0,0,0,0,0,0,101,101,101,101, +0,0,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101, +0,0,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101 + + + + +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,8,9,4,4,4,4,4,4,4,4,4,4,4,4,10,11,0,0, +0,0,0,0,26,27,40,40,40,40,40,40,40,40,40,40,40,40,28,29,0,0, +0,0,0,0,21,23,0,0,0,0,0,0,0,0,0,0,0,0,21,23,0,0, +0,0,0,0,21,23,0,0,0,0,0,0,0,0,0,0,0,0,21,23,0,0, +0,0,0,0,21,23,0,0,0,0,0,0,0,0,0,0,0,0,21,23,0,0, +0,0,0,0,21,23,0,0,0,0,0,0,0,0,0,0,0,0,21,23,0,0, +0,0,0,0,21,23,0,0,0,0,0,0,0,0,0,0,0,0,21,23,0,0, +0,0,0,0,21,23,0,0,0,0,0,0,0,0,0,0,0,0,21,23,0,0, +0,0,0,0,44,45,4,4,4,4,4,4,4,4,4,4,4,4,46,47,0,0, +0,0,0,0,62,63,40,40,40,40,40,40,40,40,40,40,40,40,64,65,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 + + + diff --git a/src/assets/mod.rs b/src/assets/mod.rs index 140fc1b..3cce1cd 100644 --- a/src/assets/mod.rs +++ b/src/assets/mod.rs @@ -11,6 +11,7 @@ impl bevy::prelude::Plugin for Plugin { let p = if cfg!(windows) { "src\\" } else { "src/" }; embedded_asset!(app, p, "level1.tmx"); + embedded_asset!(app, p, "level2.tmx"); embedded_asset!(app, p, "kenney_racing-pack/PNG/Cars/car_red_5.png"); embedded_asset!(app, p, "kenney_racing-pack/PNG/Cars/car_blue_1.png"); diff --git a/src/main.rs b/src/main.rs index ecd85f7..6541b84 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,16 +18,30 @@ mod helpers; mod util; use util::IteratorToArrayExt; -#[derive(Parser)] +#[derive(Clone, Debug, Parser, Resource)] #[command(author, version, about, long_about = None)] struct Args { + /// Turn debugging visualizations on + #[arg(short, long, action = clap::ArgAction::Count)] + debug: u8, + + /// Jump to the selected level + #[arg(short, long, default_value_t = 1)] + level: u32, + /// Enable windowed mode (for debugging try: -wdd) #[arg(short, long)] window: bool, +} - /// Turn debugging visualizations on - #[arg(short, long, action = clap::ArgAction::Count)] - debug: u8, +impl Args { + fn debug_low(&self) -> bool { + self.debug >= 1 + } + + fn debug_high(&self) -> bool { + self.debug >= 2 + } } fn main() { @@ -60,8 +74,8 @@ fn main() { )) .register_type::() .register_type::() - .insert_resource(ClearColor(Color::rgb(0.053, 0.782, 0.276))) - .insert_resource(DebugLevel(args.debug)) + .insert_resource(ClearColor(Color::rgb_linear(0.153, 0.682, 0.376))) + .insert_resource(args) .add_systems( Startup, (load_maps, spawn_camera, spawn_player, spawn_ai_players), @@ -72,7 +86,9 @@ fn main() { generate_guidance_field, handle_keyboard, handle_ai_players, - apply_velocity, + apply_velocity + .after(handle_ai_players) + .after(handle_keyboard), apply_friction.after(apply_velocity), track_player.after(apply_velocity), collision_detection @@ -84,19 +100,6 @@ fn main() { .run(); } -#[derive(Resource)] -struct DebugLevel(u8); - -impl DebugLevel { - fn low(&self) -> bool { - self.0 >= 1 - } - - fn high(&self) -> bool { - self.0 >= 2 - } -} - #[derive(Component, Debug)] struct Player; @@ -134,9 +137,9 @@ fn spawn_camera(mut commands: Commands) { commands.spawn(camera); } -fn load_maps(mut commands: Commands, asset_server: Res) { - let map_handle: Handle = - asset_server.load("embedded://tdr2024/assets/level1.tmx"); +fn load_maps(mut commands: Commands, asset_server: Res, args: Res) { + let p = format!("embedded://tdr2024/assets/level{}.tmx", args.level); + let map_handle: Handle = asset_server.load(p); commands.spawn(helpers::tiled::TiledMapBundle { tiled_map: map_handle, @@ -151,7 +154,12 @@ struct GuidanceField { impl GuidanceField { fn from_map(map: &tiled::Map) -> Option { - let layer = map.get_layer(1)?.as_tile_layer()?; + let layer = map + .get_layer(1) + .unwrap_or(map.get_layer(0)?) + .as_tile_layer()?; + dbg!(map.get_layer(1)); + dbg!(&layer); let w = layer.width()?; let h = layer.height()?; @@ -271,7 +279,7 @@ fn spawn_ai_players( commands.spawn(( Racer, - Angle(0.0), + Angle(PI / 12.0), Velocity(Vec2::new(0.0, 20.0)), SpriteSheetBundle { texture_atlas: texture_atlas.add(atlas), @@ -289,7 +297,7 @@ fn spawn_ai_players( let atlas = TextureAtlas::from_grid(handle, Vec2::new(70., 121.), 1, 1, None, None); commands.spawn(( Racer, - Angle(0.0), + Angle(PI / 12.0), Velocity(Vec2::new(0.0, 20.0)), SpriteSheetBundle { texture_atlas: texture_atlas.add(atlas), @@ -307,7 +315,7 @@ fn spawn_ai_players( let atlas = TextureAtlas::from_grid(handle, Vec2::new(70., 121.), 1, 1, None, None); commands.spawn(( Racer, - Angle(0.0), + Angle(PI / 12.0), Velocity(Vec2::new(0.0, 20.0)), SpriteSheetBundle { texture_atlas: texture_atlas.add(atlas), @@ -321,10 +329,23 @@ fn spawn_ai_players( )); } -fn apply_friction(mut query: Query<&mut Velocity>, time: Res