diff --git a/Cargo.toml b/Cargo.toml index 195ef31..0da6bcf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,12 @@ version = "0.1.0" edition = "2024" [dependencies] +bevy = "0.18" +bevy_ui_text_input = "0.7.0" + +[profile.dev] +opt-level = 1 +debug = true + +[profile.dev.package."*"] +opt-level = 3 diff --git a/src/game_state/mod.rs b/src/game_state/mod.rs new file mode 100644 index 0000000..1bf35ef --- /dev/null +++ b/src/game_state/mod.rs @@ -0,0 +1,23 @@ +use bevy::{prelude::*}; + +#[derive(States, Debug, Clone, Eq, PartialEq, Hash)] +pub enum GameState { + Menu, + InGame, +} + +#[derive(States, Debug, Clone, Eq, PartialEq, Hash)] +pub enum MenuState { + Main, + Seed, +} + +pub struct GameStatePlagin; + +impl Plugin for GameStatePlagin{ + fn build(&self, app: &mut App) { + app.insert_state(GameState::Menu); + app.insert_state(MenuState::Main); + } +} + diff --git a/src/main.rs b/src/main.rs index e7a11a9..3457a08 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,21 @@ +use bevy::{prelude::*}; + +mod game_state; +use game_state::GameStatePlagin; +use game_state::{GameState, MenuState}; + +mod ui_menu; +use ui_menu::UIMenu; + fn main() { - println!("Hello, world!"); + App::new() + .add_plugins(DefaultPlugins) + .add_plugins(bevy_ui_text_input::TextInputPlugin) + .add_plugins(UIMenu) + .add_plugins(GameStatePlagin) + // .add_systems(Startup, setup) + .run(); } + + + diff --git a/src/ui_menu/mod.rs b/src/ui_menu/mod.rs new file mode 100644 index 0000000..ee1c79d --- /dev/null +++ b/src/ui_menu/mod.rs @@ -0,0 +1,112 @@ +use bevy::{prelude::*}; +use bevy_ui_text_input::TextInputNode; + +use super::GameState; +use super::MenuState; + +mod ui_systems; +use ui_systems::*; + +pub struct UIMenu; + +impl Plugin for UIMenu { + fn build(&self, app: &mut App) { + app.add_systems(Startup, setup); + + // запуск + app.add_systems(OnEnter(MenuState::Main), setup_main_menu); + app.add_systems(OnEnter(MenuState::Seed), setup_seed_menu); + + //очистка + app.add_systems(OnExit(MenuState::Main), cleanup_main_menu); + app.add_systems(OnExit(MenuState::Seed), cleanup_seed_menu); + + app.add_message::(); + + app.add_systems(Update, ( + button_to_message, + handle_button_events.after(button_to_message), + )); + } +} + +fn setup(mut commands: Commands, assets: Res){ + commands.spawn(Camera2d); + + + let booton_menu = ( + Button, + BackgroundColor(Color::srgb(0.3, 0.3, 0.3)), + Node { + width: Val::Px(200.0), + height: Val::Px(60.0), + border: UiRect::all(px(5)), + justify_content: JustifyContent::Center, + align_items: AlignItems::Center, + border_radius: BorderRadius::MAX, + display: Display::Flex, + ..default() + }, + ); + + commands.spawn(( + Node { + width: percent(100), + height: percent(100), + flex_direction: FlexDirection::Column, + align_items: AlignItems::Center, + justify_content: JustifyContent::Center, + // row_gap: Val::Px(10.0), + ..Default::default() + }, + children![( + booton_menu.clone(), + MenuMain, + Name::new("start"), + children![( + Text::new("StartNewGame"), + TextFont { + font: assets.load("fronts/IBMPlexMono-Medium.ttf"), + font_size: 33.0, + ..default() + }, + TextColor(Color::srgb(0.9, 0.9, 0.9)), + TextShadow::default(), + )], + ),( + MenuSead, + Node { + width: Val::Px(200.0), + height: Val::Px(60.0), + display: Display::None, + ..Default::default() + }, + BackgroundColor(Color::srgb(0.3, 0.3, 0.3)), + TextInputNode { + clear_on_submit: false, + unfocus_on_submit: true, + ..Default::default() + }, + TextFont { + font: assets.load("fronts/IBMPlexMono-Medium.ttf"), + font_size: 25., + ..Default::default() + }, + TextShadow::default(), + ),( + booton_menu.clone(), + MenuMain, + Name::new("exit"), + children![( + Text::new("Exit"), + TextFont { + font: assets.load("fronts/IBMPlexMono-Medium.ttf"), + font_size: 33.0, + ..default() + }, + TextColor(Color::srgb(0.9, 0.9, 0.9)), + TextShadow::default(), + )] + )], + )); +} diff --git a/src/ui_menu/ui_systems.rs b/src/ui_menu/ui_systems.rs new file mode 100644 index 0000000..f16cb3e --- /dev/null +++ b/src/ui_menu/ui_systems.rs @@ -0,0 +1,77 @@ +use bevy::{prelude::*}; + +use super::GameState; +use super::MenuState; + +#[derive(Component, Clone)] +pub struct MenuMain; + +#[derive(Component, Clone)] +pub struct MenuSead; + +#[derive(Message)] +pub struct MenuBootonEvent(String); + +#[derive(Message)] +pub enum ButtonEvent{ + Start, + ExitApp, + ExitSead, +} + +// запуск. +pub fn setup_main_menu(mut query: Query<(&MenuMain, &mut Node)>){ + for (_, mut node) in &mut query { + node.display = Display::Flex; + } +} +pub fn setup_seed_menu(mut query: Query<(&MenuSead, &mut Node)>){ + for (_, mut node) in &mut query { + node.display = Display::Flex; + } +} + +// отключение +pub fn cleanup_main_menu(mut query: Query<(&MenuMain, &mut Node)>){ + for (_, mut node) in &mut query { + node.display = Display::None; + } +} +pub fn cleanup_seed_menu(mut query: Query<(&MenuSead, &mut Node)>){ + for (_, mut node) in &mut query { + node.display = Display::None; + } +} + +pub fn button_to_message( + mut writer: MessageWriter, + query: Query<(&Interaction, &Name), Changed>, +){ + for (interaction, name) in &query { + if *interaction == Interaction::Pressed { + match name.as_str() { + "start" => {writer.write(ButtonEvent::Start);}, + _ => { + println!("Unknown button pressed: {}", name.as_str()); + } + } + } + } +} + + +pub fn handle_button_events( + mut reader: MessageReader, + mut next_menu_state: ResMut>, + mut next_game_state: ResMut>, +) { + for event in reader.read() { + match event { + ButtonEvent::Start => { + next_menu_state.set(MenuState::Seed); + println!("ss"); + }, + _ => {} + } + } +}