Main Menu
This commit is contained in:
parent
5ea5d9e2de
commit
241078096f
5 changed files with 105 additions and 9 deletions
25
src/main.rs
25
src/main.rs
|
|
@ -1,3 +1,4 @@
|
|||
use bevy::input_focus::InputFocus;
|
||||
use bevy::prelude::*;
|
||||
use bevy::window::WindowResolution;
|
||||
|
||||
|
|
@ -6,6 +7,7 @@ use crate::collision::CollisionPlugin;
|
|||
use crate::damagable::DamagablePlugin;
|
||||
use crate::projectile::ProjectilePlugin;
|
||||
use crate::ships::ShipsPlugin;
|
||||
use crate::ui::UIPlugin;
|
||||
use crate::velocity::VelocityPlugin;
|
||||
|
||||
mod asteroid;
|
||||
|
|
@ -13,6 +15,7 @@ mod collision;
|
|||
mod damagable;
|
||||
mod projectile;
|
||||
mod ships;
|
||||
mod ui;
|
||||
mod velocity;
|
||||
|
||||
const FIRST_CORNER_X: f32 = -512.;
|
||||
|
|
@ -29,6 +32,8 @@ pub enum GameState {
|
|||
|
||||
fn main() {
|
||||
App::new()
|
||||
.insert_resource(ClearColor(Color::srgb(0.0, 0.0, 0.0)))
|
||||
.init_resource::<InputFocus>()
|
||||
.add_plugins(
|
||||
DefaultPlugins
|
||||
.set(WindowPlugin {
|
||||
|
|
@ -44,12 +49,18 @@ fn main() {
|
|||
})
|
||||
.set(ImagePlugin::default_nearest()),
|
||||
)
|
||||
.insert_state(GameState::Game)
|
||||
.add_plugins(VelocityPlugin)
|
||||
.add_plugins(CollisionPlugin)
|
||||
.add_plugins(ShipsPlugin)
|
||||
.add_plugins(DamagablePlugin)
|
||||
.add_plugins(ProjectilePlugin)
|
||||
.add_plugins(AsteroidPlugin)
|
||||
.insert_state(GameState::InMenu)
|
||||
.add_plugins((
|
||||
VelocityPlugin,
|
||||
CollisionPlugin,
|
||||
ShipsPlugin,
|
||||
DamagablePlugin,
|
||||
ProjectilePlugin,
|
||||
AsteroidPlugin,
|
||||
UIPlugin,
|
||||
))
|
||||
.add_systems(Startup, |mut commands: Commands| {
|
||||
commands.spawn(Camera2d);
|
||||
})
|
||||
.run();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,8 +49,6 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|||
let player_sprite: Handle<Image> = asset_server.load("player.png");
|
||||
let enemy_sprite: Handle<Image> = asset_server.load("enemy.png");
|
||||
|
||||
commands.spawn(Camera2d);
|
||||
|
||||
player::spawn_player(&mut commands, player_sprite, Vec2::new(0., 0.));
|
||||
enemy::spawn_enemy(&mut commands, enemy_sprite.clone(), Vec2::new(100., 100.));
|
||||
}
|
||||
|
|
|
|||
0
src/ui/game_over.rs
Normal file
0
src/ui/game_over.rs
Normal file
68
src/ui/main_menu.rs
Normal file
68
src/ui/main_menu.rs
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
use bevy::{input_focus::InputFocus, prelude::*};
|
||||
|
||||
use crate::GameState;
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Menu;
|
||||
|
||||
pub fn setup_menu(mut commands: Commands) {
|
||||
//setup menu text
|
||||
commands.spawn((
|
||||
Text::new("NOW IT'S YOUR CHANCE TO BE\nA\nBIG\nFELORONI"),
|
||||
TextFont {
|
||||
font_size: 32.,
|
||||
..default()
|
||||
},
|
||||
TextLayout::new_with_justify(Justify::Center),
|
||||
Menu,
|
||||
Node {
|
||||
position_type: PositionType::Absolute,
|
||||
top: percent(25),
|
||||
right: percent(33),
|
||||
..default()
|
||||
},
|
||||
));
|
||||
//setup button
|
||||
commands.spawn((
|
||||
Button,
|
||||
Node {
|
||||
width: px(150),
|
||||
height: px(65),
|
||||
border: UiRect::all(px(5)),
|
||||
justify_content: JustifyContent::Center,
|
||||
align_items: AlignItems::Center,
|
||||
..default()
|
||||
},
|
||||
Menu,
|
||||
children![(Text::new("Igron"))],
|
||||
));
|
||||
}
|
||||
|
||||
pub fn button_system(
|
||||
mut commands: Commands,
|
||||
mut input_focus: ResMut<InputFocus>,
|
||||
interaction_query: Query<(Entity, &Interaction, &mut Button), Changed<Interaction>>,
|
||||
) {
|
||||
for (entity, interaction, mut button) in interaction_query {
|
||||
match interaction {
|
||||
Interaction::Pressed => {
|
||||
input_focus.set(entity);
|
||||
button.set_changed();
|
||||
commands.set_state(GameState::Game);
|
||||
}
|
||||
Interaction::Hovered => {
|
||||
input_focus.set(entity);
|
||||
button.set_changed();
|
||||
}
|
||||
Interaction::None => {
|
||||
input_focus.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cleanup_menu(mut commands: Commands, query: Query<Entity, With<Menu>>) {
|
||||
for entity in query {
|
||||
commands.get_entity(entity).unwrap().despawn();
|
||||
}
|
||||
}
|
||||
19
src/ui/mod.rs
Normal file
19
src/ui/mod.rs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
use bevy::prelude::*;
|
||||
|
||||
use crate::GameState;
|
||||
|
||||
use game_over::*;
|
||||
use main_menu::*;
|
||||
|
||||
mod game_over;
|
||||
mod main_menu;
|
||||
|
||||
pub struct UIPlugin;
|
||||
|
||||
impl Plugin for UIPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(OnEnter(GameState::InMenu), setup_menu)
|
||||
.add_systems(Update, button_system.run_if(in_state(GameState::InMenu)))
|
||||
.add_systems(OnExit(GameState::InMenu), cleanup_menu);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue