Skip to content

Commit

Permalink
feat: AppState: MainMenu and InGame
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamtowards committed Jan 30, 2024
1 parent 62b2272 commit c930277
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 113 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ version = "0.1.0"
edition = "2021"


[features]
default = []
web = [] # disables some native crates


[dev-dependencies]
bevy = { version = "0.12", features = ["dynamic_linking", "file_watcher", "trace_tracy"] }
# cargo run --features bevy/trace_tracy


[dependencies]
bevy = { version = "0.12" }

Expand All @@ -29,8 +35,8 @@ thread_local = "1.1"
once_cell = "1.19"
futures-lite = "2.0.1" # async poll task

native-dialog = "0.7"
sysinfo = "0.29"
native-dialog = "0.7"
memory-stats = "1.1.0"
log = "0.4.20"
env_logger = "0.11"
Expand All @@ -52,4 +58,5 @@ opt-level = 3

# further improve performances in a noticeable way, even for a release build (though the build itself will take longer to complete):
[profile.release]
codegen-units = 1
codegen-units = 1

2 changes: 1 addition & 1 deletion src/character_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl Plugin for CharacterControllerPlugin {
fn build(&self, app: &mut App) {
app.register_type::<CharacterController>();

app.add_systems(Update, input_move);
app.add_systems(Update, input_move.run_if(in_state(crate::game::AppState::InGame)));

app.add_systems(PostUpdate, sync_camera.in_set(PhysicsSet::Sync));
}
Expand Down
111 changes: 77 additions & 34 deletions src/editor/mod.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,51 @@
use bevy::{
diagnostic::{
DiagnosticsStore, EntityCountDiagnosticsPlugin, FrameTimeDiagnosticsPlugin,
},
prelude::*,
render::{renderer::RenderAdapterInfo, view::VisibleEntities},
}, prelude::*, render::{renderer::RenderAdapterInfo, view::VisibleEntities}, window::{CursorGrabMode, PrimaryWindow, WindowMode}
};

use bevy_editor_pls::editor::EditorEvent;
use bevy_egui::{
egui::{style::HandleShape, FontData, FontDefinitions, FontFamily, Rounding},
EguiContexts, EguiPlugin, EguiSettings,
};

use crate::voxel::HitResult;
use crate::{character_controller::CharacterController, voxel::HitResult};
use crate::game::AppState;



pub struct EditorPlugin;

impl Plugin for EditorPlugin {
fn build(&self, app: &mut App) {
//app.add_plugins(EguiPlugin);
//app.add_systems(Update, ui_example_system);

// Editor
use bevy_editor_pls::prelude::*;
app.add_plugins(
EditorPlugin::default(), // .in_new_window(Window {
// title: "Editor".into(),
// ..default()
// })
);

app.add_plugins((
FrameTimeDiagnosticsPlugin,
EntityCountDiagnosticsPlugin,
//SystemInformationDiagnosticsPlugin
));

// Setup Controls
app.insert_resource(res_editor_controls());
app.add_systems(Startup, setup_editor_camera_controls);

app.add_plugins(EguiPlugin);

// // Editor
// use bevy_editor_pls::prelude::*;
// app.add_plugins(
// EditorPlugin::default(), // .in_new_window(Window {
// // title: "Editor".into(),
// // ..default()
// // })
// );

// // Setup Controls
// app.insert_resource(res_editor_controls());
// app.add_systems(Startup, setup_editor_camera_controls);
// app.add_systems(Update, handle_inputs);

// app.add_plugins((
// FrameTimeDiagnosticsPlugin,
// EntityCountDiagnosticsPlugin,
// //SystemInformationDiagnosticsPlugin
// ));



// DebugText
app.add_systems(Startup, setup_debug_text);
app.add_systems(Update, update_debug_text);
app.add_systems(OnEnter(AppState::InGame), setup_debug_text);
app.add_systems(Update, update_debug_text.run_if(in_state(AppState::InGame)));

// Setup Egui Style
app.add_systems(Startup, setup_egui_style);
Expand All @@ -50,6 +54,42 @@ impl Plugin for EditorPlugin {
}
}

fn handle_inputs(
mut editor_events: EventReader<bevy_editor_pls::editor::EditorEvent>,
mut window_query: Query<&mut Window, With<PrimaryWindow>>,
mut controller_query: Query<&mut CharacterController>,
key: Res<Input<KeyCode>>,
// mouse_input: Res<Input<MouseButton>>,
) {
let mut window = window_query.single_mut();

// Toggle MouseGrab
for event in editor_events.read() {
if let EditorEvent::Toggle { now_active } = *event {
let playing = !now_active;
window.cursor.grab_mode = if playing {
CursorGrabMode::Locked
} else {
CursorGrabMode::None
};
window.cursor.visible = !playing;
for mut controller in &mut controller_query {
controller.enable_input = playing;
}
}
}

// Toggle Fullscreen
if key.just_pressed(KeyCode::F11)
|| (key.pressed(KeyCode::AltLeft) && key.just_pressed(KeyCode::Return))
{
window.mode = if window.mode != WindowMode::Fullscreen {
WindowMode::Fullscreen
} else {
WindowMode::Windowed
};
}
}
fn res_editor_controls() -> bevy_editor_pls::controls::EditorControls {
use bevy_editor_pls::controls::*;
let mut editor_controls = EditorControls::default_bindings();
Expand Down Expand Up @@ -251,12 +291,15 @@ fn update_debug_text(
let mut mem_usage_phys = 0.;
let mut mem_usage_virtual = 0.;

if let Some(usage) = memory_stats::memory_stats() {
// println!("Current physical memory usage: {}", byte_unit::Byte::from_bytes(usage.physical_mem as u128).get_appropriate_unit(false).to_string());
// println!("Current virtual memory usage: {}", byte_unit::Byte::from_bytes(usage.virtual_mem as u128).get_appropriate_unit(false).to_string());

mem_usage_phys = usage.physical_mem as f64 * BYTES_TO_MIB;
mem_usage_virtual = usage.virtual_mem as f64 * BYTES_TO_MIB;
#[cfg(not(feature = "web"))]
{
if let Some(usage) = memory_stats::memory_stats() {
// println!("Current physical memory usage: {}", byte_unit::Byte::from_bytes(usage.physical_mem as u128).get_appropriate_unit(false).to_string());
// println!("Current virtual memory usage: {}", byte_unit::Byte::from_bytes(usage.virtual_mem as u128).get_appropriate_unit(false).to_string());

mem_usage_phys = usage.physical_mem as f64 * BYTES_TO_MIB;
mem_usage_virtual = usage.virtual_mem as f64 * BYTES_TO_MIB;
}
}

let gpu_name = &render_adapter_info.0.name;
Expand Down
74 changes: 28 additions & 46 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ use crate::{character_controller::{

use crate::voxel::VoxelPlugin;




pub struct GamePlugin;

impl Plugin for GamePlugin {
Expand Down Expand Up @@ -50,33 +53,48 @@ impl Plugin for GamePlugin {
// ChunkSystem
app.add_plugins(VoxelPlugin);

app.add_systems(Startup, startup);
app.add_systems(Update, tick_world);

app.add_systems(Update, handle_inputs);

app.add_systems(PostUpdate, gizmo_sys.after(PhysicsSet::Sync));
app.add_systems(OnEnter(AppState::InGame), startup);
app.add_systems(Update, tick_world.run_if(in_state(AppState::InGame)));

app.add_systems(PostUpdate, gizmo_sys.after(PhysicsSet::Sync).run_if(in_state(AppState::InGame)));

// Network Client
app.add_plugins(NetworkClientPlugin);

app.add_systems(Update, crate::ui::ui_main_menu);


app.add_state::<AppState>();

app.add_systems(Update, crate::ui::ui_main_menu.run_if(in_state(AppState::MainMenu)));

}
}



#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)]
pub enum AppState {
#[default]
MainMenu,
InGame,
}








fn startup(
assets: Res<AssetServer>,
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut editor_events: EventWriter<bevy_editor_pls::editor::EditorEvent>,
// mut editor_events: EventWriter<bevy_editor_pls::editor::EditorEvent>,
) {
// Grab mouse at startup.
editor_events.send(bevy_editor_pls::editor::EditorEvent::Toggle { now_active: false });
// // Grab mouse at startup.
// editor_events.send(bevy_editor_pls::editor::EditorEvent::Toggle { now_active: false });

// Logical Player
commands.spawn((
Expand Down Expand Up @@ -254,42 +272,6 @@ fn gizmo_sys(
}


fn handle_inputs(
mut editor_events: EventReader<bevy_editor_pls::editor::EditorEvent>,
mut window_query: Query<&mut Window, With<PrimaryWindow>>,
mut controller_query: Query<&mut CharacterController>,
key: Res<Input<KeyCode>>,
// mouse_input: Res<Input<MouseButton>>,
) {
let mut window = window_query.single_mut();

// Toggle MouseGrab
for event in editor_events.read() {
if let EditorEvent::Toggle { now_active } = *event {
let playing = !now_active;
window.cursor.grab_mode = if playing {
CursorGrabMode::Locked
} else {
CursorGrabMode::None
};
window.cursor.visible = !playing;
for mut controller in &mut controller_query {
controller.enable_input = playing;
}
}
}

// Toggle Fullscreen
if key.just_pressed(KeyCode::F11)
|| (key.pressed(KeyCode::AltLeft) && key.just_pressed(KeyCode::Return))
{
window.mode = if window.mode != WindowMode::Fullscreen {
WindowMode::Fullscreen
} else {
WindowMode::Windowed
};
}
}

#[derive(Resource, Default, Reflect)]
#[reflect(Resource)]
Expand Down
27 changes: 17 additions & 10 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@

use bevy::prelude::*;

use bevy_egui::{EguiContexts, egui::{self, Widget}};
use bevy_egui::{EguiContexts, egui::{self, Widget, Ui}};

use crate::game::WorldInfo;
use crate::game::{AppState, WorldInfo};


pub fn ui_main_menu(
mut ctx: EguiContexts,
mut worldinfo: ResMut<WorldInfo>,
mut next_state: ResMut<NextState<AppState>>,
) {
egui::TopBottomPanel::top("top_panel2").show(ctx.ctx_mut(), |ui| {

Expand Down Expand Up @@ -60,16 +61,22 @@ pub fn ui_main_menu(

egui::Window::new("Main Menu").show(ctx.ctx_mut(), |ui| {

ui.label("Ethertia");
ui.vertical_centered(|ui| {

if ui.button("Play").clicked() {

}
if ui.button("Settings").clicked() {
ui.heading("ethertia");

}
if ui.button("Terminate").clicked() {
if ui.add_sized([200., 20.], egui::Button::new("Play")).clicked() {
next_state.set(AppState::InGame);
}
if ui.add_sized([200., 20.], egui::Button::new("Settings")).clicked() {

}
if ui.add_sized([200., 20.], egui::Button::new("Terminate")).clicked() {

}
});

ui.label("Copyright Ethertia. Do not distribute!");

}
});
}
Loading

0 comments on commit c930277

Please sign in to comment.