Begin work on a main menu & UI systems

This commit is contained in:
2025-10-18 15:04:54 -05:00
parent 09e05c9f8e
commit 9b71fff4eb
2 changed files with 129 additions and 2 deletions

View File

@@ -8,10 +8,21 @@ use bevy::{
use thiserror::Error;
use tungstenite::{WebSocket, http::Response, stream::MaybeTlsStream};
use crate::ui::{despawn_main_menu, spawn_main_menu};
mod ui;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.insert_state(GameState::MainMenu)
.add_systems(Startup, spawn_camera)
.add_systems(OnEnter(GameState::MainMenu), spawn_main_menu)
.add_systems(OnExit(GameState::MainMenu), despawn_main_menu)
.add_observer(ui::button_hover_start)
.add_observer(ui::button_hover_stop)
// TODO: System to operate buttons & other UI widgets
// .add_systems(Update, )
.add_systems(
Update,
(
@@ -20,7 +31,8 @@ fn main() {
handle_tasks,
send_info,
recv_info,
),
)
.run_if(in_state(GameState::ConnectionDemo)),
)
.add_message::<WebSocketConnectionMessage>()
.insert_resource(ChatTimer {
@@ -29,6 +41,29 @@ fn main() {
.run();
}
/// Main game state indicator
#[derive(Debug, Hash, PartialEq, Eq, Clone, States)]
enum GameState {
MainMenu,
Playing,
ConnectionDemo, // TODO: Remove this state.
}
/// Utility to despawn entities with a given component. Useful for scene
/// changes.
///
/// I'm using this primarily with marker components to delete UI elements when
/// they are no longer needed. E.g.: Removing the main menu after starting.
pub fn despawn<T: Component>(mut commands: Commands, targets: Query<Entity, With<T>>) {
targets
.iter()
.for_each(|entt| commands.entity(entt).despawn());
}
fn spawn_camera(mut commands: Commands) {
commands.spawn(Camera2d);
}
/// Initialize the scene
fn setup(
mut commands: Commands,