From 3d5df31da7f29eb395bee9452d132364c98254c0 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Tue, 30 Jul 2024 12:11:31 -0500 Subject: [PATCH] Game runs again, but not playable --- Cargo.lock | 10 ++++ Cargo.toml | 2 +- src/camera.rs | 2 +- src/main.rs | 8 +-- src/player.rs | 149 +++++++++++++++++++++++++------------------------- 5 files changed, 91 insertions(+), 80 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b63b086..53913cf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -294,6 +294,7 @@ version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e938630e9f472b1899c78ef84aa907081b23bad8333140e2295c620485b6ee7" dependencies = [ + "bevy_dylib", "bevy_internal", ] @@ -469,6 +470,15 @@ dependencies = [ "const-fnv1a-hash", ] +[[package]] +name = "bevy_dylib" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8494bf550eb30f570da1563217bcea25530cf29b35d35887ca6c2d76a411d00" +dependencies = [ + "bevy_internal", +] + [[package]] name = "bevy_ecs" version = "0.14.0" diff --git a/Cargo.toml b/Cargo.toml index c03ea89..c4f1922 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ bevy = { version = "0.14.0", default-features = false, features = [ "wav", # sound files # TODO: would be nice to get this working while developing - # "dynamic_linking", + "dynamic_linking", # NOTE: Features we may want at some point. # "vorbis", # music -- maybe mp3? diff --git a/src/camera.rs b/src/camera.rs index ec57286..34f8fab 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -2,7 +2,7 @@ use bevy::prelude::*; use bevy::render::camera::Camera; pub fn startup(mut commands: Commands) { - let mut bundle = (Camera2dBundle::default(), IsDefaultUiCamera); + let bundle = (Camera2dBundle::default(), IsDefaultUiCamera); // bundle.0.projection.scale = 1; commands.spawn(bundle); } diff --git a/src/main.rs b/src/main.rs index a4def69..764f2c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,11 +35,6 @@ struct InGameSet; fn main() { let mut app = App::new(); - app.init_resource::(); - - app.insert_state(View::default()); - app.insert_state(Game::default()); - app.add_plugins(( DefaultPlugins .set(WindowPlugin { @@ -59,6 +54,9 @@ fn main() { .set(ImagePlugin::default_nearest()), FrameTimeDiagnosticsPlugin, )) + .init_resource::() + .insert_state(View::default()) + .insert_state(Game::default()) .add_systems(OnEnter(View::MainMenu), main_menu::startup) .add_systems(OnExit(View::MainMenu), main_menu::exit) .add_systems(OnEnter(View::InGame), (player::startup, statue::startup)) diff --git a/src/player.rs b/src/player.rs index 3c30f2f..0535735 100644 --- a/src/player.rs +++ b/src/player.rs @@ -101,91 +101,94 @@ pub fn controls( mut next_state: ResMut>, mut crosshair_q: Query<&mut Transform, With>, ) { - let (mut sprite, mut texture, mut heading, player_entity) = player_q.single_mut(); - let (camera, camera_transform) = cam_q.single(); + if let Ok((mut sprite, mut texture, mut heading, player_entity)) = player_q.get_single_mut() { + let (camera, camera_transform) = cam_q.single(); - let mouse_world_position = win_q - .single() - .cursor_position() - .and_then(|cursor| camera.viewport_to_world(&GlobalTransform::from_xyz(0., 0., 0.), cursor)) - .map(|ray| ray.origin.truncate()); + let mouse_world_position = win_q + .single() + .cursor_position() + .and_then(|cursor| { + camera.viewport_to_world(&GlobalTransform::from_xyz(0., 0., 0.), cursor) + }) + .map(|ray| ray.origin.truncate()); - if let Some(pos) = mouse_world_position { - if let Ok(mut t) = crosshair_q.get_single_mut() { - t.look_at(Vec3::ZERO, pos.extend(0.)); - t.translation = camera_transform.translation(); + if let Some(pos) = mouse_world_position { + if let Ok(mut t) = crosshair_q.get_single_mut() { + t.look_at(Vec3::ZERO, pos.extend(0.)); + t.translation = camera_transform.translation(); + } } - } - **heading = Vec2::ZERO; - for button in mouse_input.get_just_pressed() { - match button { - MouseButton::Left => { - if let Some(world_position) = win_q - .single() - .cursor_position() - .and_then(|cursor| camera.viewport_to_world(camera_transform, cursor)) - .map(|ray| ray.origin.truncate()) - { - let spos = world_position.extend(-world_position.y); - let statue_texture = asset_server.load("img/Props.png").clone(); - commands.spawn(( - Statue, - SpriteSheetBundle { - texture: statue_texture, - atlas: TextureAtlas { - layout: assets.images.statue.clone(), - index: 0, + **heading = Vec2::ZERO; + for button in mouse_input.get_just_pressed() { + match button { + MouseButton::Left => { + if let Some(world_position) = win_q + .single() + .cursor_position() + .and_then(|cursor| camera.viewport_to_world(camera_transform, cursor)) + .map(|ray| ray.origin.truncate()) + { + let spos = world_position.extend(-world_position.y); + let statue_texture = asset_server.load("img/Props.png").clone(); + commands.spawn(( + Statue, + SpriteSheetBundle { + texture: statue_texture, + atlas: TextureAtlas { + layout: assets.images.statue.clone(), + index: 0, + }, + transform: Transform::from_translation(spos), + ..Default::default() }, - transform: Transform::from_translation(spos), - ..Default::default() - }, - )); + )); + } } + _ => {} } - _ => {} } - } - for key in input.get_just_pressed() { - match key { - KeyCode::Escape => next_state.set(crate::View::MainMenu), - KeyCode::Enter => next_state.set(crate::View::MainMenu), - KeyCode::Space => { - let child = commands - .spawn(AudioSourceBundle { - source: assets.sounds.meow.clone(), - settings: PlaybackSettings::DESPAWN.with_spatial(false), - }) - .id(); - commands.entity(player_entity).push_children(&[child]); + for key in input.get_just_pressed() { + match key { + KeyCode::Escape => next_state.set(crate::View::MainMenu), + KeyCode::Enter => next_state.set(crate::View::MainMenu), + KeyCode::Space => { + let child = commands + .spawn(AudioSourceBundle { + source: assets.sounds.meow.clone(), + settings: PlaybackSettings::DESPAWN.with_spatial(false), + }) + .id(); + commands.entity(player_entity).push_children(&[child]); + } + _ => {} } - _ => {} } - } - for key in input.get_pressed() { - match key { - KeyCode::KeyA | KeyCode::ArrowLeft => { - **heading -= Vec2::X; + for key in input.get_pressed() { + match key { + KeyCode::KeyA | KeyCode::ArrowLeft => { + **heading -= Vec2::X; + } + KeyCode::KeyD | KeyCode::ArrowRight => { + **heading += Vec2::X; + } + KeyCode::KeyW | KeyCode::ArrowUp => { + **heading += Vec2::Y; + } + KeyCode::KeyS | KeyCode::ArrowDown => { + **heading -= Vec2::Y; + } + _ => {} } - KeyCode::KeyD | KeyCode::ArrowRight => { - **heading += Vec2::X; - } - KeyCode::KeyW | KeyCode::ArrowUp => { - **heading += Vec2::Y; - } - KeyCode::KeyS | KeyCode::ArrowDown => { - **heading -= Vec2::Y; - } - _ => {} } - } - if heading.y < 0. { - texture.index = 0; - } else if heading.y > 0. { - texture.index = 1; - } else if heading.x != 0. { - texture.index = 2; - sprite.flip_x = heading.x > 0.; + if heading.y < 0. { + texture.index = 0; + } else if heading.y > 0. { + texture.index = 1; + } else if heading.x != 0. { + texture.index = 2; + sprite.flip_x = heading.x > 0.; + } } }