Compare commits
12 Commits
fc43be0777
...
wasm
| Author | SHA1 | Date | |
|---|---|---|---|
| 9d9b25d1df | |||
| 89ee328807 | |||
| 90d8142855 | |||
| 10366b642c | |||
| 94e0cd0999 | |||
| 18d026637b | |||
| 51e6989ef4 | |||
| 2ffc0e8861 | |||
| d15b96ef48 | |||
| 0aefc96f7a | |||
| 4102446e90 | |||
| f4df2ae33a |
4
.cargo/config.toml
Normal file
4
.cargo/config.toml
Normal file
@@ -0,0 +1,4 @@
|
||||
|
||||
[target.wasm32-unknown-unknown]
|
||||
runner = "wasm-server-runner"
|
||||
rustflags = ["--cfg", "getrandom_backend=\"wasm_js\""]
|
||||
29
Cargo.toml
29
Cargo.toml
@@ -1,10 +1,33 @@
|
||||
[package]
|
||||
name = "asteroids"
|
||||
version = "0.2.0"
|
||||
version = "0.3.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
bevy = { version = "0.16", features = ["dynamic_linking"] }
|
||||
bevy = "0.16"
|
||||
bevy-inspector-egui = "0.32.0"
|
||||
bevy_rapier2d = { version = "0.31.0", features = ["debug-render-2d"] }
|
||||
bevy_rapier2d = "0.31.0"
|
||||
rand = "0.9.2"
|
||||
|
||||
[features]
|
||||
dynamic_linking = ["bevy/dynamic_linking"]
|
||||
debug_ui = ["bevy_rapier2d/debug-render-2d"]
|
||||
|
||||
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||
getrandom = { version = "0.3.3", features = ["wasm_js"] }
|
||||
|
||||
[profile.speedy]
|
||||
inherits = "release"
|
||||
codegen-units = 1
|
||||
lto = "fat"
|
||||
opt-level = 3
|
||||
strip = "symbols"
|
||||
panic = "abort"
|
||||
|
||||
[profile.tiny]
|
||||
inherits = "release"
|
||||
codegen-units = 1
|
||||
lto = "fat"
|
||||
opt-level = "z"
|
||||
strip = "symbols"
|
||||
panic = "abort"
|
||||
|
||||
@@ -7,7 +7,7 @@ pub const WINDOW_SIZE: bevy::prelude::Vec2 = bevy::prelude::Vec2::new(800.0, 600
|
||||
|
||||
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 const UI_BUTTON_PRESSED: Color = Color::srgb(0.55, 0.55, 0.55); // ... 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);
|
||||
|
||||
@@ -81,7 +81,7 @@ impl Plugin for AsteroidPlugin {
|
||||
.add_event::<events::AsteroidDestroy>()
|
||||
.add_event::<events::ShipDestroy>()
|
||||
.add_event::<events::BulletDestroy>();
|
||||
app.insert_state(GameState::GameOver);
|
||||
app.insert_state(GameState::TitleScreen);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
171
src/widgets.rs
171
src/widgets.rs
@@ -5,7 +5,7 @@ use crate::{
|
||||
};
|
||||
|
||||
use bevy::{
|
||||
color::palettes::css::{BLACK, GREEN, LIGHT_BLUE, RED, WHITE},
|
||||
color::palettes::css::{BLACK, DARK_GRAY, GREEN, LIGHT_BLUE, RED, WHITE},
|
||||
prelude::*,
|
||||
};
|
||||
|
||||
@@ -15,10 +15,10 @@ 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(OnExit(GameState::TitleScreen), despawn::<MarkerMainMenu>)
|
||||
.add_systems(
|
||||
Update,
|
||||
handle_spacebar.run_if(in_state(GameState::TitleScreen)),
|
||||
(handle_spacebar, operate_buttons).run_if(in_state(GameState::TitleScreen)),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -38,7 +38,7 @@ impl Plugin for PluginGetReady {
|
||||
}
|
||||
}
|
||||
|
||||
/// Plugin for the game-over screen (TODO)
|
||||
/// Plugin for the game-over screen
|
||||
pub struct PluginGameOver;
|
||||
|
||||
impl Plugin for PluginGameOver {
|
||||
@@ -47,11 +47,16 @@ impl Plugin for PluginGameOver {
|
||||
.add_systems(OnExit(GameState::GameOver), despawn::<MarkerGameOver>)
|
||||
.add_systems(
|
||||
Update,
|
||||
operate_gameover_ui.run_if(in_state(GameState::GameOver)),
|
||||
operate_buttons.run_if(in_state(GameState::GameOver)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Marker component for the title screen UI entity.
|
||||
// This way, a query for the TitleUI can be used to despawn the title screen
|
||||
#[derive(Component)]
|
||||
struct MarkerMainMenu;
|
||||
|
||||
/// Marker component for things on the get-ready indicator
|
||||
#[derive(Component)]
|
||||
struct OnReadySetGo;
|
||||
@@ -65,8 +70,9 @@ struct MarkerGameOver;
|
||||
/// 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 {
|
||||
enum ButtonMenuAction {
|
||||
ToMainMenu,
|
||||
StartGame,
|
||||
Quit,
|
||||
}
|
||||
|
||||
@@ -90,6 +96,62 @@ fn despawn<T: Component>(mut commands: Commands, to_despawn: Query<Entity, With<
|
||||
}
|
||||
}
|
||||
|
||||
/// Utility function for creating a standard button.
|
||||
fn button_bundle(text: &str) -> impl Bundle {
|
||||
(
|
||||
Button,
|
||||
// TODO: Generic action
|
||||
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(text),
|
||||
TextColor(Color::srgb(0.9, 0.9, 0.9)),
|
||||
TextShadow::default(),
|
||||
)],
|
||||
)
|
||||
}
|
||||
|
||||
fn spawn_menu(mut commands: Commands) {
|
||||
commands
|
||||
.spawn((
|
||||
MarkerMainMenu,
|
||||
Node {
|
||||
width: Val::Percent(100.0),
|
||||
height: Val::Percent(100.0),
|
||||
align_items: AlignItems::Center,
|
||||
justify_content: JustifyContent::Center,
|
||||
flex_direction: FlexDirection::Column,
|
||||
..Default::default()
|
||||
},
|
||||
))
|
||||
.with_children(|cmds| {
|
||||
cmds.spawn((
|
||||
Text::new("Robert's Bad Asteroids Game"),
|
||||
TextFont::from_font_size(50.0),
|
||||
TextLayout::new_with_justify(JustifyText::Center),
|
||||
TextShadow::default(),
|
||||
));
|
||||
cmds.spawn((
|
||||
Text::new("Press space to begin"),
|
||||
TextFont::from_font_size(20.0),
|
||||
TextColor(Color::srgb(0.7, 0.7, 0.7)),
|
||||
TextShadow::default(),
|
||||
));
|
||||
cmds.spawn((button_bundle("Start Game"), ButtonMenuAction::StartGame));
|
||||
cmds.spawn((button_bundle("Quit"), ButtonMenuAction::Quit));
|
||||
});
|
||||
}
|
||||
|
||||
fn spawn_get_ready(mut commands: Commands) {
|
||||
commands.spawn((
|
||||
OnReadySetGo, // marker, so this can be de-spawned properly
|
||||
@@ -139,48 +201,8 @@ fn spawn_gameover_ui(mut commands: Commands) {
|
||||
..default()
|
||||
},
|
||||
children![
|
||||
(
|
||||
Button,
|
||||
GameOverMenuAction::ToMainMenu,
|
||||
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,
|
||||
GameOverMenuAction::Quit,
|
||||
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(),
|
||||
)]
|
||||
)
|
||||
(button_bundle("Main Menu"), ButtonMenuAction::ToMainMenu,),
|
||||
(button_bundle("Quit"), ButtonMenuAction::Quit),
|
||||
],
|
||||
));
|
||||
}
|
||||
@@ -210,19 +232,24 @@ fn animate_get_ready_widget(
|
||||
}
|
||||
}
|
||||
|
||||
/// Handles interaction and performs updates to the game-over UI.
|
||||
/// Handles interactions with the menu buttons.
|
||||
///
|
||||
/// Used by [`PluginGameOver`] while in the [`GameState::GameOver`] state.
|
||||
/// The buttons are used by the main menu and the game-over menu to change
|
||||
/// between game states.
|
||||
///
|
||||
/// 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(
|
||||
/// Button animation and action handling is done entirely within this system.
|
||||
///
|
||||
/// There are no checks for current state. If a "quit" button was put somewhere
|
||||
/// on the HUD, this system would quit the game. The same will happen for
|
||||
/// returning to the title screen. This should be useful for making a pause
|
||||
/// menu, too.
|
||||
fn operate_buttons(
|
||||
mut interactions: Query<
|
||||
(
|
||||
&Interaction,
|
||||
&mut BackgroundColor,
|
||||
&mut BorderColor,
|
||||
&GameOverMenuAction,
|
||||
&ButtonMenuAction,
|
||||
),
|
||||
(Changed<Interaction>, With<Button>),
|
||||
>,
|
||||
@@ -234,12 +261,15 @@ fn operate_gameover_ui(
|
||||
match *interaction {
|
||||
Interaction::Pressed => {
|
||||
*color = UI_BUTTON_PRESSED.into();
|
||||
border_color.0 = RED.into();
|
||||
border_color.0 = DARK_GRAY.into();
|
||||
match menu_action {
|
||||
GameOverMenuAction::ToMainMenu => {
|
||||
ButtonMenuAction::ToMainMenu => {
|
||||
game_state.set(GameState::TitleScreen);
|
||||
}
|
||||
GameOverMenuAction::Quit => {
|
||||
ButtonMenuAction::StartGame => {
|
||||
game_state.set(GameState::Playing);
|
||||
}
|
||||
ButtonMenuAction::Quit => {
|
||||
app_exit_events.write(AppExit::Success);
|
||||
}
|
||||
}
|
||||
@@ -256,32 +286,7 @@ fn operate_gameover_ui(
|
||||
}
|
||||
}
|
||||
|
||||
// Marker component for the title screen UI entity.
|
||||
// This way, a query for the TitleUI can be used to despawn the title screen
|
||||
#[derive(Component)]
|
||||
struct TitleUI;
|
||||
|
||||
fn spawn_menu(mut commands: Commands) {
|
||||
commands
|
||||
.spawn((
|
||||
TitleUI,
|
||||
Node {
|
||||
flex_direction: FlexDirection::Column,
|
||||
..Default::default()
|
||||
},
|
||||
))
|
||||
.with_children(|cmds| {
|
||||
cmds.spawn((
|
||||
Text::new("Robert's Bad Asteroids Game"),
|
||||
TextFont::from_font_size(50.0),
|
||||
));
|
||||
cmds.spawn((
|
||||
Text::new("Press space to begin"),
|
||||
TextFont::from_font_size(40.0),
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
/// Main menu input listener. Starts game when the spacebar is pressed.
|
||||
fn handle_spacebar(input: Res<ButtonInput<KeyCode>>, mut game_state: ResMut<NextState<GameState>>) {
|
||||
if input.just_pressed(KeyCode::Space) {
|
||||
game_state.set(GameState::GetReady);
|
||||
|
||||
Reference in New Issue
Block a user