From 000659fe0769c474635acecec8a556cbd32452f0 Mon Sep 17 00:00:00 2001 From: moo Date: Thu, 19 Mar 2026 15:09:06 -0700 Subject: [PATCH] jfewoajfioew' ' --- src/entities.rs | 3 ++ src/main.rs | 20 +++++++-- src/systems.rs | 107 ++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 124 insertions(+), 6 deletions(-) diff --git a/src/entities.rs b/src/entities.rs index 1bc88db..c0d8c75 100644 --- a/src/entities.rs +++ b/src/entities.rs @@ -37,3 +37,6 @@ pub struct Velocity { ///degs per sec, positive is clockwise pub angular: f32, } + +#[derive(Component)] +pub struct QuitButton; diff --git a/src/main.rs b/src/main.rs index 4729732..2af5efd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,9 @@ mod entities; mod systems; -use bevy::{prelude::*, window::WindowResolution}; +use bevy::{ + diagnostic::LogDiagnosticsPlugin, ecs::query::QueryIter, prelude::*, window::WindowResolution, +}; use entities::*; use systems::*; @@ -15,13 +17,16 @@ fn main() { }), ..default() })) + .init_state::() .insert_resource(WindowSize(Vec2::new(800.0, 600.0))) .insert_resource(Sprites { ship_sprite: Handle::default(), asteroid_sprite: Handle::default(), }) .add_systems(Startup, (load_sprites, setup).chain()) - .add_systems(Update, resolution) + .add_systems(Update, (resolution, toggle_pause_system, button_quit)) + .add_systems(OnEnter(AppState::Paused), spawn_menu) + .add_systems(OnEnter(AppState::InGame), despawn_menu) .add_systems( Update, ( @@ -33,7 +38,16 @@ fn main() { collision_system, restart_system, ) - .chain(), + .chain() + .run_if(in_state(AppState::InGame)), ) .run(); } + +#[derive(Debug, Clone, Eq, PartialEq, Hash, Default, States)] +pub enum AppState { + MainMenu, + #[default] + InGame, + Paused, +} diff --git a/src/systems.rs b/src/systems.rs index ada3820..c2a4d42 100644 --- a/src/systems.rs +++ b/src/systems.rs @@ -7,7 +7,7 @@ use bevy::{ use rand::Rng; use std::f32::consts::PI; -use crate::entities::*; +use crate::{AppState, entities::*}; const PLAYER_SIZE: f32 = 30.0; const PLAYER_ROTATION_SPEED: f32 = 3.0 * PI / 180.0; @@ -77,6 +77,7 @@ pub fn setup(mut commands: Commands, window_size: Res, sprites: Res< }, )); } + } pub fn player_movement_system( @@ -164,9 +165,12 @@ pub fn resolution(window: Single<&Window>, mut window_size: ResMut) window_size.0 = window.size(); } +/// for screen wrapping +const BUFFER: f32 = 20.0; + pub fn screen_wrap_system(mut query: Query<&mut Transform>, window_size: Res) { - let half_width = window_size.0.x as f32 / 2.0; - let half_height = window_size.0.y as f32 / 2.0; + let half_width = (window_size.0.x as f32 / 2.0) + BUFFER; + let half_height = (window_size.0.y as f32 / 2.0) + BUFFER; for mut transform in &mut query { if transform.translation.x > half_width { @@ -190,6 +194,7 @@ pub fn restart_system( player_query: Query>, camera_query: Query>, asteroid_query: Query>, + query: Query, ) { if keyboard_input.just_pressed(KeyCode::KeyR) { cleanup(&mut commands, player_query); @@ -265,3 +270,99 @@ pub fn collision_system( } } } + +pub fn toggle_pause_system( + mut next_state: ResMut>, + current_state: Res>, + input: Res>, +) { + if input.just_pressed(KeyCode::KeyP) { + match **current_state { + AppState::InGame => next_state.set(AppState::Paused), + AppState::Paused => next_state.set(AppState::InGame), + _ => (), + }; + } +} + +pub fn spawn_menu(mut commands: Commands) { + let container = Node { + width: Val::Percent(50.0), + height: Val::Percent(70.0), + justify_content: JustifyContent::Center, + align_content: AlignContent::Center, + ..Default::default() + }; + + let paused = ( + Button, + Node { + width: px(180), + height: px(65), + border: UiRect::all(px(5)), + bottom: Val::Percent(60.0), + justify_content: JustifyContent::Center, + position_type: PositionType::Absolute, + align_items: AlignItems::Center, + ..default() + }, + BorderColor::all(Color::WHITE), + BackgroundColor(Color::BLACK), + children![( + Text::new("PAUSED"), + TextColor(Color::srgb(0.9, 0.9, 0.9)), + TextShadow::default(), + )], + ); + + let quit_button = ( + Button, + QuitButton, + Node { + width: px(150), + height: px(65), + border: UiRect::all(px(5)), + bottom: Val::Percent(-45.0), + justify_content: JustifyContent::Center, + align_items: AlignItems::Center, + ..default() + }, + BorderColor::all(Color::WHITE), + BackgroundColor(Color::BLACK), + children![( + Text::new("QUIT"), + TextColor(Color::srgb(0.9, 0.9, 0.9)), + TextShadow::default(), + )], + ); + + commands.spawn((container, children![quit_button, paused])); +} + +pub fn button_quit( + mut interaction_query: Query< + ( + Entity, + &Interaction, + &mut Button, + &Children, + &mut Node, + &mut BorderColor, + ), + (Changed, With), + >, + mut writer: MessageWriter, +) { + for (entity, interaction, button, children, node, mut border_color) in interaction_query { + match interaction { + Interaction::Pressed => { writer.write(AppExit::Success); }, + Interaction::Hovered => *border_color = BorderColor::all(Color::srgb(166.0 / 255., 255. / 255., 196. / 255.)), + Interaction::None => (), + } + } +} + +pub fn despawn_menu(mut commands: Commands, query: Query>) { + cleanup(&mut commands, query); +} + -- 2.47.3