main menu

This commit is contained in:
AmadeusWM
2025-04-05 19:13:20 +02:00
parent 988bb741ac
commit f1788fc942
7 changed files with 179 additions and 5 deletions

Binary file not shown.

View File

@@ -1,3 +1,4 @@
({ ({
"lebron": File (path: "images/KingLebron.png"), "lebron": File (path: "images/KingLebron.png"),
"wall": File (path: "meshes/wall.glb"),
}) })

BIN
assets/meshes/wall.glb Normal file

Binary file not shown.

View File

@@ -28,7 +28,10 @@ pub(super) fn plugin(app: &mut App) {
pub(crate) struct AudioAssets {} pub(crate) struct AudioAssets {}
#[derive(AssetCollection, Resource, Clone)] #[derive(AssetCollection, Resource, Clone)]
pub(crate) struct GltfAssets {} pub(crate) struct GltfAssets {
#[asset(key = "wall")]
pub(crate) wall: Handle<Gltf>,
}
#[derive(AssetCollection, Resource, Clone)] #[derive(AssetCollection, Resource, Clone)]
pub(crate) struct TextureAssets {} pub(crate) struct TextureAssets {}

View File

@@ -0,0 +1,19 @@
use bevy::prelude::*;
use crate::{asset_loading::GltfAssets, GameState};
pub fn map_plugin(app: &mut App) {
// app.add_systems(OnEnter(GameState::Playing), spawn_level);
}
fn spawn_level(
mut commands: Commands,
models: Res<Assets<Gltf>>,
gltf_assets: Res<GltfAssets>) {
let gltf = models.get(&gltf_assets.wall).unwrap();
// commands.spawn(SceneBundle {
// scene: gltf.scenes[0].clone(),
// ..default()
// });
}

View File

@@ -1,3 +1,5 @@
use std::default;
use asset_loading::ImageAssets; use asset_loading::ImageAssets;
use bevy::prelude::*; use bevy::prelude::*;
use player_plugin::PlayerPlugin; use player_plugin::PlayerPlugin;
@@ -6,18 +8,25 @@ use physics_plugin::PhysicsPlugin;
mod asset_loading; mod asset_loading;
mod player_plugin; mod player_plugin;
mod physics_plugin; mod physics_plugin;
mod level_instantiation;
mod main_menu;
fn main() { fn main() {
App::new() App::new()
.add_plugins((DefaultPlugins, asset_loading::plugin, PlayerPlugin, PhysicsPlugin))
.init_state::<GameState>() .init_state::<GameState>()
// We need to register components to make them visible to Blenvy .add_plugins((
DefaultPlugins,
asset_loading::plugin,
PlayerPlugin,
PhysicsPlugin,
level_instantiation::map_plugin,
main_menu::plugin
))
.add_systems(Startup, setup) .add_systems(Startup, setup)
.add_systems(OnExit(GameState::Loading), debug_our_king)
.run(); .run();
} }
#[derive(States, Default, Clone, Eq, PartialEq, Debug, Hash)] #[derive(States, Debug, Clone, Copy, Eq, PartialEq, Hash, Default)]
enum GameState { enum GameState {
/// During the loading State the loading_plugin will load our assets /// During the loading State the loading_plugin will load our assets
#[default] #[default]

142
src/main_menu.rs Normal file
View File

@@ -0,0 +1,142 @@
use bevy::{color::palettes::css::{BLACK, CRIMSON, DARK_GREY}, prelude::*};
use crate::GameState;
#[derive(Component)]
struct OnMainMenuScreen;
pub fn plugin(app: &mut App) {
app
.add_systems(OnEnter(GameState::Menu), setup_main_menu)
.add_systems(OnExit(GameState::Menu), despawn_screen::<OnMainMenuScreen>)
.add_systems(Update, button_system.run_if(in_state(GameState::Menu)));
}
#[derive(Component)]
struct MainMenu;
#[derive(Component)]
enum MenuButton {
Play,
Settings,
Quit
}
const TEXT_COLOR: Color = Color::srgb(0.9, 0.9, 0.9);
const NORMAL_BUTTON: Color = Color::srgb(0.15, 0.15, 0.15);
const HOVERED_BUTTON: Color = Color::srgb(0.25, 0.25, 0.25);
const HOVERED_PRESSED_BUTTON: Color = Color::srgb(0.25, 0.65, 0.25);
const PRESSED_BUTTON: Color = Color::srgb(0.35, 0.75, 0.35);
fn setup_main_menu(mut commands: Commands) {
let button_node = Node {
width: Val::Px(300.0),
height: Val::Px(65.0),
margin: UiRect::all(Val::Px(20.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
};
let button_text_font = TextFont {
font_size: 33.0,
..default()
};
commands
.spawn((
Node {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
flex_direction: FlexDirection::Column,
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
..default()
},
OnMainMenuScreen
))
.insert(MainMenu)
.with_children(|parent| {
parent.spawn((
Node {
flex_direction: FlexDirection::Column,
align_items: AlignItems::Center,
..default()
},
BackgroundColor(BLACK.into()),
))
.with_children(|parent| {
// Display the game name
parent.spawn((
Text::new("Among Me"),
TextFont {
font_size: 67.0,
..default()
},
TextColor(TEXT_COLOR),
Node {
margin: UiRect::all(Val::Px(50.0)),
..default()
},
));
// Display three buttons for each action available from the main menu:
// - new game
// - settings
// - quit
parent
.spawn((
Button,
button_node.clone(),
BackgroundColor(NORMAL_BUTTON),
MenuButton::Play,
))
.with_children(|parent| {
parent.spawn((
Text::new("New Game"),
button_text_font.clone(),
TextColor(TEXT_COLOR),
));
});
parent
.spawn((
Button,
button_node.clone(),
BackgroundColor(NORMAL_BUTTON),
MenuButton::Quit,
))
.with_children(|parent| {
parent.spawn((
Text::new("Quit"),
button_text_font.clone(),
TextColor(TEXT_COLOR),
));
});
});
});
}
fn button_system(
mut interaction_query: Query<
(&Interaction, &MenuButton),
(Changed<Interaction>, With<Button>),
>,
mut game_state: ResMut<NextState<GameState>>,
) {
for (interaction, button) in interaction_query.iter() {
if *interaction == Interaction::Pressed {
match button {
MenuButton::Play => game_state.set(GameState::Playing),
MenuButton::Settings => println!("Settings pressed"),
MenuButton::Quit => std::process::exit(0),
}
}
}
}
fn despawn_screen<T: Component>(to_despawn: Query<Entity, With<T>>, mut commands: Commands) {
for entity in &to_despawn {
commands.entity(entity).despawn_recursive();
}
}