From 8c26fa7eb89165631e453df11bd0da84c498de73 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Mon, 29 Jul 2024 16:29:39 -0500 Subject: [PATCH] Menus, hurray! --- src/assets.rs | 11 +---- src/camera.rs | 33 ++++++-------- src/main.rs | 114 ++++++++++++++++++++++++++++++----------------- src/main_menu.rs | 43 ++++++++++++------ src/movement.rs | 25 +++-------- src/player.rs | 31 +++++++------ src/statue.rs | 17 ++++--- 7 files changed, 148 insertions(+), 126 deletions(-) diff --git a/src/assets.rs b/src/assets.rs index 48ca4c0..7a4382f 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -23,16 +23,7 @@ pub struct Fonts { pub iosevkalytemin: Handle, } -pub struct AssetPlugin; - -impl Plugin for AssetPlugin { - fn build(&self, app: &mut App) { - app.init_resource::() - .add_systems(Startup, load_assets); - } -} - -fn load_assets( +pub fn startup( mut assets: ResMut, asset_server: Res, mut texture_atlases: ResMut>, diff --git a/src/camera.rs b/src/camera.rs index b19e849..12de5b9 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -1,29 +1,24 @@ use bevy::prelude::*; -use bevy::render::camera::Camera as BevyCamera; +use bevy::render::camera::Camera; -pub struct CameraPlugin; - -#[derive(Component, Debug)] -pub struct CameraTarget; - -impl Plugin for CameraPlugin { - fn build(&self, app: &mut App) { - app.add_systems(Startup, spawn) - .add_systems(PostUpdate, follow); - } -} - -fn spawn(mut commands: Commands) { +pub fn startup(mut commands: Commands) { let mut bundle = (Camera2dBundle::default(), IsDefaultUiCamera); bundle.0.projection.scale = 0.5; commands.spawn(bundle); } -fn follow( - player: Query<&Transform, With>, - mut camera: Query<&mut Transform, (With, Without)>, +#[derive(Component, Debug)] +pub struct Watched; + +pub fn update( + watched: Query<&Transform, With>, + mut camera: Query<&mut Transform, (With, Without)>, ) { - if let Ok(player) = player.get_single() { - camera.single_mut().translation = player.translation + if let Ok(mut camera) = camera.get_single_mut() { + if let Ok(watched) = watched.get_single() { + camera.translation.x = watched.translation.x + } else { + camera.translation = Vec3::ZERO; + } } } diff --git a/src/main.rs b/src/main.rs index 8587e79..c3b436c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use bevy::audio::{AudioPlugin, SpatialScale}; -use bevy::prelude::*; +use bevy::prelude::{default, *}; mod assets; mod camera; @@ -8,50 +8,82 @@ mod movement; mod player; mod statue; +#[derive(States, Default, Debug, Clone, PartialEq, Eq, Hash)] +pub enum Game { + #[default] + Running, + Paused, +} + +#[derive(States, Default, Debug, Clone, PartialEq, Eq, Hash)] +pub enum View { + // #[default] + // LoadingScreen, + #[default] + MainMenu, + InGame, +} + +#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)] +struct MainMenuSet; + +#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)] +struct InGameSet; + fn main() { let mut app = App::new(); - app.add_plugins( - DefaultPlugins - .set(WindowPlugin { - primary_window: Some(Window { - title: "Kodo Tag".into(), - mode: bevy::window::WindowMode::BorderlessFullscreen, - // resolution: (640. * 2., 360. * 2.).into(), - ..default() - }), + app.init_resource::(); + + app.insert_state(View::default()); + app.insert_state(Game::default()); + + app.add_plugins((DefaultPlugins + .set(WindowPlugin { + primary_window: Some(Window { + title: "Kodo Tag".into(), + mode: bevy::window::WindowMode::BorderlessFullscreen, + // resolution: (640. * 2., 360. * 2.).into(), ..default() - }) - .set(AudioPlugin { - default_spatial_scale: SpatialScale::new_2d(1.), - global_volume: GlobalVolume::new(1.), - ..default() - }) - .set(ImagePlugin::default_nearest()), - ) - .add_plugins(( - statue::Statue, - camera::CameraPlugin, - assets::AssetPlugin, - player::Player, - main_menu::MainMenu, - movement::Movement, - )) - .add_systems(Update, exit_on_escape) - .insert_resource(ClearColor(Color::rgb(0.1, 0., 0.3))) - .insert_resource(AmbientLight { - color: Color::rgb(1., 1., 1.), - brightness: 1., - }); + }), + ..default() + }) + .set(AudioPlugin { + default_spatial_scale: SpatialScale::new_2d(1.), + global_volume: GlobalVolume::new(1.), + ..default() + }) + .set(ImagePlugin::default_nearest()),)) + .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)) + .add_systems(OnExit(View::InGame), (player::exit, statue::exit)) + .add_systems(Startup, (assets::startup, camera::startup)) + .add_systems( + Update, + ( + ( + player::sprite_select, + player::controls, + movement::update_velocity_by_heading, + movement::resolve_velocity.after(movement::update_velocity_by_heading), + movement::ysort.after(movement::update_velocity_by_heading), + camera::update + .after(player::controls) + .after(movement::resolve_velocity), + ) + .in_set(InGameSet) + .run_if(in_state(View::InGame)), + (main_menu::update) + .in_set(MainMenuSet) + .run_if(in_state(View::MainMenu)), + ), + ) + .insert_resource(ClearColor(Color::rgb(0.1, 0.1, 0.1))) + .insert_resource(AmbientLight { + color: Color::rgb(1., 1., 1.), + brightness: 1., + }); app.run() } - -fn exit_on_escape( - keyboard_input: Res>, - mut app_exit_events: ResMut>, -) { - if keyboard_input.pressed(KeyCode::Escape) { - app_exit_events.send(bevy::app::AppExit); - } -} diff --git a/src/main_menu.rs b/src/main_menu.rs index f45810b..ca8fa15 100644 --- a/src/main_menu.rs +++ b/src/main_menu.rs @@ -5,23 +5,20 @@ use crate::assets::AssetLoader; #[derive(Component, Debug)] pub struct MainMenu; -impl Plugin for MainMenu { - fn build(&self, app: &mut App) { - app.add_systems(PostStartup, setup_main_menu); - } -} - -fn setup_main_menu(mut commands: Commands, assets: Res) { +pub fn startup(mut commands: Commands, assets: Res) { commands - .spawn(NodeBundle { - style: Style { - width: Val::Percent(100.0), - height: Val::Percent(100.0), - justify_content: JustifyContent::SpaceBetween, + .spawn(( + MainMenu, + NodeBundle { + style: Style { + width: Val::Percent(100.0), + height: Val::Percent(100.0), + justify_content: JustifyContent::SpaceBetween, + ..default() + }, ..default() }, - ..default() - }) + )) .with_children(|parent| { // text parent.spawn(( @@ -59,3 +56,21 @@ fn setup_main_menu(mut commands: Commands, assets: Res) { }); }); } + +pub fn exit(mut commands: Commands, q: Query>) { + for id in q.iter() { + commands.entity(id).despawn_recursive(); + } +} + +pub fn update( + keyboard_input: Res>, + mut next_state: ResMut>, + mut app_exit_events: ResMut>, +) { + if keyboard_input.just_pressed(KeyCode::Escape) { + app_exit_events.send(bevy::app::AppExit); + } else if keyboard_input.pressed(KeyCode::Enter) { + next_state.set(crate::View::InGame) + } +} diff --git a/src/movement.rs b/src/movement.rs index db75d3c..4c10ee3 100644 --- a/src/movement.rs +++ b/src/movement.rs @@ -1,5 +1,8 @@ use bevy::prelude::*; +#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)] +struct SystemSet; + #[derive(Component, Deref, DerefMut, Debug, Default)] pub struct Velocity(pub Vec2); @@ -9,19 +12,19 @@ pub struct Heading(pub Vec2); #[derive(Component, Deref, DerefMut, Debug, Default)] pub struct Speed(pub f32); -fn update_position(mut query: Query<(&Velocity, &mut Transform)>, time: Res