mod entities;
mod systems;
-use bevy::{prelude::*, window::WindowResolution};
+use bevy::{
+ diagnostic::LogDiagnosticsPlugin, ecs::query::QueryIter, prelude::*, window::WindowResolution,
+};
use entities::*;
use systems::*;
}),
..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,
(
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,
+}
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;
},
));
}
+
}
pub fn player_movement_system(
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 {
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);
}
}
}
+
+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);
+}
+