]> git.taranathan.com Git - asteroids.git/commitdiff
jfewoajfioew'
authormoo <moogoesmeow123@gmail.com>
Thu, 19 Mar 2026 22:09:06 +0000 (15:09 -0700)
committermoo <moogoesmeow123@gmail.com>
Thu, 19 Mar 2026 22:09:06 +0000 (15:09 -0700)
'

src/entities.rs
src/main.rs
src/systems.rs

index 1bc88dbd2173784bcc6f4560b5b08da6a6e27d82..c0d8c754e5bc3bd7df9d1b9deaf54e14317c7f65 100644 (file)
@@ -37,3 +37,6 @@ pub struct Velocity {
     ///degs per sec, positive is clockwise
     pub angular: f32,
 }
+
+#[derive(Component)]
+pub struct QuitButton;
index 4729732f7cd6545b2262afe5cf8d367d23dc6559..2af5efd311d9cdc3f8a64896bf7990935d31ecd9 100644 (file)
@@ -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::<AppState>()
         .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,
+}
index ada3820b73060b0ecefff2b66024ff45000ee5c8..c2a4d425c95086c85451ac3485589049c29b86e7 100644 (file)
@@ -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<WindowSize>, sprites: Res<
             },
         ));
     }
+
 }
 
 pub fn player_movement_system(
@@ -164,9 +165,12 @@ pub fn resolution(window: Single<&Window>, mut window_size: ResMut<WindowSize>)
     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<WindowSize>) {
-    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<Entity, With<Player>>,
     camera_query: Query<Entity, With<Camera2d>>,
     asteroid_query: Query<Entity, With<Asteroid>>,
+    query: Query<Entity>,
 ) {
     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<NextState<AppState>>,
+    current_state: Res<State<AppState>>,
+    input: Res<ButtonInput<KeyCode>>,
+) {
+    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<Interaction>, With<QuitButton>),
+    >,
+    mut writer: MessageWriter<AppExit>,
+) {
+    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<Entity, With<Node>>) {
+    cleanup(&mut commands, query);
+}
+