Compare commits
5 Commits
33828d6a85
...
a74b99deb4
| Author | SHA1 | Date | |
|---|---|---|---|
| a74b99deb4 | |||
| 2f9401e93f | |||
| 54ef257ab4 | |||
| 69bef24913 | |||
| df4479bf49 |
@@ -5,6 +5,10 @@ use bevy::color::Color;
|
||||
|
||||
pub const WINDOW_SIZE: bevy::prelude::Vec2 = bevy::prelude::Vec2::new(800.0, 600.0);
|
||||
|
||||
pub const UI_BUTTON_NORMAL: Color = Color::srgb(0.15, 0.15, 0.15); // Button color when it's just hanging out
|
||||
pub const UI_BUTTON_HOVERED: Color = Color::srgb(0.25, 0.25, 0.25); // ... when it's hovered
|
||||
pub const UI_BUTTON_PRESSED: Color = Color::srgb(0.35, 0.75, 0.35); // ... when it's pressed
|
||||
|
||||
pub(crate) const BACKGROUND_COLOR: Color = Color::srgb(0.3, 0.3, 0.3);
|
||||
pub(crate) const PLAYER_SHIP_COLOR: Color = Color::srgb(1.0, 1.0, 1.0);
|
||||
pub(crate) const SHIP_THRUSTER_COLOR_ACTIVE: Color = Color::srgb(1.0, 0.2, 0.2);
|
||||
|
||||
17
src/lib.rs
17
src/lib.rs
@@ -13,7 +13,7 @@ mod widgets;
|
||||
use crate::config::{
|
||||
ASTEROID_SMALL_COLOR, BACKGROUND_COLOR, BULLET_COLOR, BULLET_LIFETIME, BULLET_SPEED,
|
||||
PLAYER_SHIP_COLOR, SHIP_ROTATION, SHIP_THRUST, SHIP_THRUSTER_COLOR_ACTIVE,
|
||||
SHIP_THRUSTER_COLOR_INACTIVE, WINDOW_SIZE,
|
||||
SHIP_THRUSTER_COLOR_INACTIVE,
|
||||
};
|
||||
use crate::machinery::AsteroidSpawner;
|
||||
use crate::objects::{Bullet, Ship};
|
||||
@@ -34,8 +34,9 @@ pub struct AsteroidPlugin;
|
||||
impl Plugin for AsteroidPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_plugins((
|
||||
widgets::GameMenuPlugin,
|
||||
widgets::preparation_widget_plugin,
|
||||
widgets::PluginGameMenu,
|
||||
widgets::PluginGameOver,
|
||||
widgets::PluginGetReady,
|
||||
RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(10.0),
|
||||
RapierDebugRenderPlugin::default(),
|
||||
))
|
||||
@@ -64,8 +65,6 @@ impl Plugin for AsteroidPlugin {
|
||||
objects::bullet_impact_listener,
|
||||
objects::ship_impact_listener,
|
||||
physics::collision_listener,
|
||||
// TODO: Remove debug printing
|
||||
debug_collision_event_printer,
|
||||
machinery::tick_lifetimes,
|
||||
)
|
||||
.run_if(in_state(GameState::Playing)),
|
||||
@@ -82,13 +81,7 @@ impl Plugin for AsteroidPlugin {
|
||||
.add_event::<events::AsteroidDestroy>()
|
||||
.add_event::<events::ShipDestroy>()
|
||||
.add_event::<events::BulletDestroy>();
|
||||
app.insert_state(GameState::Playing);
|
||||
}
|
||||
}
|
||||
|
||||
fn debug_collision_event_printer(mut collision_events: EventReader<CollisionEvent>) {
|
||||
for event in collision_events.read() {
|
||||
dbg!(event);
|
||||
app.insert_state(GameState::GameOver);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
192
src/widgets.rs
192
src/widgets.rs
@@ -1,34 +1,75 @@
|
||||
use crate::{
|
||||
GameState,
|
||||
config::{UI_BUTTON_HOVERED, UI_BUTTON_NORMAL, UI_BUTTON_PRESSED},
|
||||
resources::{Lives, Score},
|
||||
};
|
||||
|
||||
use bevy::{
|
||||
color::palettes::css::{BLACK, GREEN, LIGHT_BLUE, RED},
|
||||
color::palettes::css::{BLACK, GREEN, LIGHT_BLUE, RED, WHITE},
|
||||
prelude::*,
|
||||
};
|
||||
|
||||
pub fn spawn_ui(mut commands: Commands, score: Res<Score>, lives: Res<Lives>) {
|
||||
commands.spawn((
|
||||
Text::new(format!("Score: {score:?} | Lives: {lives:?}")),
|
||||
TextFont::from_font_size(25.0),
|
||||
));
|
||||
/// Plugin for the main menu
|
||||
pub struct PluginGameMenu;
|
||||
|
||||
impl Plugin for PluginGameMenu {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(OnEnter(GameState::TitleScreen), spawn_menu)
|
||||
.add_systems(OnExit(GameState::TitleScreen), despawn::<TitleUI>)
|
||||
.add_systems(
|
||||
Update,
|
||||
handle_spacebar.run_if(in_state(GameState::TitleScreen)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn preparation_widget_plugin(app: &mut App) {
|
||||
app.add_systems(OnEnter(GameState::GetReady), spawn_get_ready)
|
||||
.add_systems(OnExit(GameState::GetReady), despawn_get_ready)
|
||||
.add_systems(
|
||||
Update,
|
||||
(animate_get_ready_widget).run_if(in_state(GameState::GetReady)),
|
||||
)
|
||||
.insert_resource(ReadySetGoTimer(Timer::from_seconds(3.0, TimerMode::Once)));
|
||||
/// Plugin for the "get ready" period as gameplay starts.
|
||||
pub struct PluginGetReady;
|
||||
|
||||
impl Plugin for PluginGetReady {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(OnEnter(GameState::GetReady), spawn_get_ready)
|
||||
.add_systems(OnExit(GameState::GetReady), despawn::<OnReadySetGo>)
|
||||
.add_systems(
|
||||
Update,
|
||||
(animate_get_ready_widget).run_if(in_state(GameState::GetReady)),
|
||||
)
|
||||
.insert_resource(ReadySetGoTimer(Timer::from_seconds(3.0, TimerMode::Once)));
|
||||
}
|
||||
}
|
||||
|
||||
/// Plugin for the game-over screen (TODO)
|
||||
pub struct PluginGameOver;
|
||||
|
||||
impl Plugin for PluginGameOver {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(OnEnter(GameState::GameOver), spawn_gameover_ui)
|
||||
.add_systems(OnExit(GameState::GameOver), despawn::<MarkerGameOver>)
|
||||
.add_systems(
|
||||
Update,
|
||||
operate_gameover_ui.run_if(in_state(GameState::GameOver)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Marker component for things on the get-ready indicator
|
||||
#[derive(Component)]
|
||||
struct OnReadySetGo;
|
||||
|
||||
/// Marker for things on the game-over screen
|
||||
#[derive(Component)]
|
||||
struct MarkerGameOver;
|
||||
|
||||
/// Action specifier for the game-over menu's buttons.
|
||||
///
|
||||
/// Attach this component to a button and [`PluginGameOver`] will use it to
|
||||
/// decide what to do when that button is pressed.
|
||||
#[derive(Component)]
|
||||
enum GameOverMenuAction {
|
||||
ToMainMenu,
|
||||
Quit,
|
||||
}
|
||||
|
||||
/// Newtype wrapper for `Timer`. Used to count down during the "get ready" phase.
|
||||
#[derive(Deref, DerefMut, Resource)]
|
||||
struct ReadySetGoTimer(Timer);
|
||||
@@ -41,6 +82,14 @@ struct CountdownText;
|
||||
#[derive(Component)]
|
||||
struct CountdownBar;
|
||||
|
||||
/// Despawns entities matching the generic argument. Intended to remove UI
|
||||
/// elements.
|
||||
fn despawn<T: Component>(mut commands: Commands, to_despawn: Query<Entity, With<T>>) {
|
||||
for entity in to_despawn {
|
||||
commands.entity(entity).despawn();
|
||||
}
|
||||
}
|
||||
|
||||
fn spawn_get_ready(mut commands: Commands) {
|
||||
commands.spawn((
|
||||
OnReadySetGo, // marker, so this can be de-spawned properly
|
||||
@@ -75,12 +124,63 @@ fn spawn_get_ready(mut commands: Commands) {
|
||||
));
|
||||
}
|
||||
|
||||
// TODO: Replace this with a generic somewhere else in the crate
|
||||
// want: `despawn_screen::<OnReadySetGo>>()`
|
||||
fn despawn_get_ready(mut commands: Commands, to_despawn: Query<Entity, With<OnReadySetGo>>) {
|
||||
for entity in to_despawn {
|
||||
commands.entity(entity).despawn();
|
||||
}
|
||||
/// Spawns the game over screen.
|
||||
///
|
||||
/// Used by [`PluginGameOver`] when entering the [`GameState::GameOver`] state.
|
||||
fn spawn_gameover_ui(mut commands: Commands) {
|
||||
commands.spawn((
|
||||
MarkerGameOver, // Marker, so `despawn<T>` can remove this on state exit.
|
||||
Node {
|
||||
width: Val::Percent(100.0),
|
||||
height: Val::Percent(100.0),
|
||||
align_items: AlignItems::Center,
|
||||
justify_content: JustifyContent::Center,
|
||||
flex_direction: FlexDirection::Column,
|
||||
..default()
|
||||
},
|
||||
children![
|
||||
(
|
||||
Button,
|
||||
Node {
|
||||
width: Val::Px(150.0),
|
||||
height: Val::Px(65.0),
|
||||
border: UiRect::all(Val::Px(2.0)),
|
||||
justify_content: JustifyContent::Center,
|
||||
align_items: AlignItems::Center,
|
||||
margin: UiRect::all(Val::Px(5.0)),
|
||||
..default()
|
||||
},
|
||||
BorderColor(Color::BLACK),
|
||||
BorderRadius::MAX,
|
||||
BackgroundColor(UI_BUTTON_NORMAL),
|
||||
children![(
|
||||
Text::new("Main Menu"),
|
||||
TextColor(Color::srgb(0.9, 0.9, 0.9)),
|
||||
TextShadow::default(),
|
||||
)]
|
||||
),
|
||||
(
|
||||
Button,
|
||||
Node {
|
||||
width: Val::Px(150.0),
|
||||
height: Val::Px(65.0),
|
||||
border: UiRect::all(Val::Px(2.0)),
|
||||
justify_content: JustifyContent::Center,
|
||||
align_items: AlignItems::Center,
|
||||
margin: UiRect::all(Val::Px(5.0)),
|
||||
..default()
|
||||
},
|
||||
BorderColor(Color::BLACK),
|
||||
BorderRadius::MAX,
|
||||
BackgroundColor(UI_BUTTON_NORMAL),
|
||||
children![(
|
||||
Text::new("Quit"),
|
||||
TextColor(Color::srgb(0.9, 0.9, 0.9)),
|
||||
TextShadow::default(),
|
||||
)]
|
||||
)
|
||||
],
|
||||
));
|
||||
}
|
||||
|
||||
fn animate_get_ready_widget(
|
||||
@@ -108,16 +208,35 @@ fn animate_get_ready_widget(
|
||||
}
|
||||
}
|
||||
|
||||
pub struct GameMenuPlugin;
|
||||
|
||||
impl Plugin for GameMenuPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(OnEnter(GameState::TitleScreen), spawn_menu)
|
||||
.add_systems(OnExit(GameState::TitleScreen), despawn_menu)
|
||||
.add_systems(
|
||||
Update,
|
||||
handle_spacebar.run_if(in_state(GameState::TitleScreen)),
|
||||
);
|
||||
/// Handles interaction and performs updates to the game-over UI.
|
||||
///
|
||||
/// Used by [`PluginGameOver`] while in the [`GameState::GameOver`] state.
|
||||
///
|
||||
/// Mostly a button input handler, but it also makes for a convenient single
|
||||
/// place to keep all system logic for this plugin.
|
||||
fn operate_gameover_ui(
|
||||
mut interactions: Query<
|
||||
(&Interaction, &mut BackgroundColor, &mut BorderColor),
|
||||
(Changed<Interaction>, With<Button>),
|
||||
>,
|
||||
) {
|
||||
// TODO: Better colors. These are taken from the example and they're ugly.
|
||||
// TODO: Read the menu action enum component and take that action.
|
||||
for (interaction, mut color, mut border_color) in &mut interactions {
|
||||
match *interaction {
|
||||
Interaction::Pressed => {
|
||||
*color = UI_BUTTON_PRESSED.into();
|
||||
border_color.0 = RED.into();
|
||||
}
|
||||
Interaction::Hovered => {
|
||||
*color = UI_BUTTON_HOVERED.into();
|
||||
border_color.0 = WHITE.into();
|
||||
}
|
||||
Interaction::None => {
|
||||
*color = UI_BUTTON_NORMAL.into();
|
||||
border_color.0 = BLACK.into();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,14 +266,15 @@ fn spawn_menu(mut commands: Commands) {
|
||||
});
|
||||
}
|
||||
|
||||
fn despawn_menu(mut commands: Commands, to_despawn: Query<Entity, With<TitleUI>>) {
|
||||
for entity in &to_despawn {
|
||||
commands.entity(entity).despawn();
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_spacebar(input: Res<ButtonInput<KeyCode>>, mut game_state: ResMut<NextState<GameState>>) {
|
||||
if input.just_pressed(KeyCode::Space) {
|
||||
game_state.set(GameState::GetReady);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn spawn_ui(mut commands: Commands, score: Res<Score>, lives: Res<Lives>) {
|
||||
commands.spawn((
|
||||
Text::new(format!("Score: {score:?} | Lives: {lives:?}")),
|
||||
TextFont::from_font_size(25.0),
|
||||
));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user