Skip to content

Commit

Permalink
feat: Menu (#63)
Browse files Browse the repository at this point in the history
* Added menu
  • Loading branch information
Zac8668 authored Jan 24, 2024
1 parent f722c19 commit 17853e6
Show file tree
Hide file tree
Showing 14 changed files with 229 additions and 20 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ ron = "0.8.1"
bevy-async-task = "1.3.1"

egui = "0.24"
bevy_egui = "0.24"
puffin = "0.18"
puffin_egui = "0.24"

Expand Down
Binary file added assets/images/menu_background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion src/actors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ impl Plugin for ActorsPlugin {
fill_actors.before(chunk_manager_update),
unfill_actors.after(chunk_manager_update),
update_actors.after(unfill_actors),
),
)
.run_if(in_state(GameState::Game)),
);
}
}
7 changes: 6 additions & 1 deletion src/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ fn animate_sprite(
pub struct AnimationPlugin;
impl Plugin for AnimationPlugin {
fn build(&self, app: &mut App) {
app.add_systems(FixedUpdate, animate_sprite.after(update_player));
app.add_systems(
FixedUpdate,
animate_sprite
.after(update_player)
.run_if(in_state(GameState::Game)),
);
}
}
9 changes: 6 additions & 3 deletions src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,11 @@ pub struct Zoom(pub f32);
pub struct CameraPlugin;
impl Plugin for CameraPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, (update_camera, on_resize_system))
.insert_resource(Zoom(0.23))
.insert_resource(TrackingCamera::default());
app.add_systems(
Update,
(update_camera, on_resize_system).run_if(in_state(GameState::Game)),
)
.insert_resource(Zoom(0.23))
.insert_resource(TrackingCamera::default());
}
}
14 changes: 10 additions & 4 deletions src/chunk_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,10 +593,16 @@ fn clear_render_rect(mut dirty_rects: ResMut<DirtyRects>) {
pub struct ChunkManagerPlugin;
impl Plugin for ChunkManagerPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, manager_setup)
.add_systems(FixedUpdate, chunk_manager_update)
.add_systems(Update, update_manager_pos)
.add_systems(PreUpdate, clear_render_rect)
app.add_systems(OnEnter(GameState::Game), manager_setup)
.add_systems(
FixedUpdate,
chunk_manager_update.run_if(in_state(GameState::Game)),
)
.add_systems(Update, update_manager_pos.run_if(in_state(GameState::Game)))
.add_systems(
PreUpdate,
clear_render_rect.run_if(in_state(GameState::Game)),
)
.init_resource::<ChunkManager>()
.init_resource::<DirtyRects>();

Expand Down
7 changes: 7 additions & 0 deletions src/consts.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use bevy::prelude::Color;

// Chunk Length consts
// Chunk length MUST be divisible by 4
pub const CHUNK_LENGHT: usize = 64;
Expand Down Expand Up @@ -49,3 +51,8 @@ pub const _CAMERA_SPEED: f32 = 10.;
pub const PLAYER_LAYER: f32 = 1.;
pub const PARTICLE_LAYER: f32 = 10.;
pub const AUTOMATA_LAYER: f32 = 100.;

//Buttons
pub const NORMAL_BUTTON: Color = Color::rgb(0.15, 0.15, 0.15);
pub const HOVERED_BUTTON: Color = Color::rgb(0.25, 0.25, 0.25);
pub const PRESSED_BUTTON: Color = Color::rgb(0.35, 0.75, 0.35);
5 changes: 3 additions & 2 deletions src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,10 @@ impl Plugin for DebugPlugin {
render_actors.after(update_actors),
prev_mpos.after(brush),
//_camera
),
)
.run_if(in_state(GameState::Game)),
)
.add_systems(PreUpdate, delete_image)
.add_systems(PreUpdate, delete_image.run_if(in_state(GameState::Game)))
.add_plugins(WorldInspectorPlugin::new())
//Frame on console
.add_plugins((LogDiagnosticsPlugin::default(), FrameTimeDiagnosticsPlugin))
Expand Down
32 changes: 29 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::type_complexity)]

use bevy::prelude::*;

mod actors;
Expand All @@ -12,20 +14,23 @@ mod debug;
mod geom_tools;
mod manager_api;
mod materials;
mod menu;
mod particles;
mod player;
mod puffin_plugin;
mod prelude {
pub use crate::GameState;
pub use crate::{
actors::*, animation::*, atom::*, camera::*, chunk::*, chunk_group::*, chunk_manager::*,
consts::*, debug::*, geom_tools::*, manager_api::*, materials::*, particles::*, player::*,
puffin_plugin::*,
consts::*, debug::*, geom_tools::*, manager_api::*, materials::*, menu::*, particles::*,
player::*, puffin_plugin::*,
};
pub use bevy::input::mouse::MouseScrollUnit;
pub use bevy::input::mouse::MouseWheel;
pub use bevy::math::{ivec2, uvec2, vec2, vec3};
pub use bevy::prelude::*;
pub use bevy::tasks::*;
pub use bevy::window::PrimaryWindow;
pub use bevy_async_task::*;

pub use serde::{Deserialize, Serialize};
Expand All @@ -39,6 +44,7 @@ mod prelude {
pub use std::sync::{Arc, RwLock};

pub use crate::materials::Material;
pub use bevy_egui::EguiContext;
}

use prelude::*;
Expand All @@ -48,7 +54,8 @@ fn main() {

let mut app = App::new();

app.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest()))
app.add_state::<GameState>()
.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest()))
//local plugins
.add_plugins((
ChunkManagerPlugin,
Expand All @@ -58,6 +65,7 @@ fn main() {
ParticlesPlugin,
MaterialsPlugin,
CameraPlugin,
MenuPlugin,
))
.add_systems(Startup, setup);

Expand All @@ -82,3 +90,21 @@ fn setup(mut commands: Commands, mut time: ResMut<Time<Fixed>>) {

commands.spawn(camera);
}

#[derive(Clone, Copy, Eq, PartialEq, Debug, Hash, States)]
pub enum GameState {
Menu,
Game,
}

impl Default for GameState {
fn default() -> Self {
let args: Vec<_> = env::args().collect();

if args.contains(&"-g".to_string()) || args.contains(&"--game".to_string()) {
GameState::Game
} else {
GameState::Menu
}
}
}
158 changes: 158 additions & 0 deletions src/menu.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
use crate::prelude::*;
use bevy::app::AppExit;

#[derive(Component)]
pub struct MenuUI;

#[derive(Component)]
enum ButtonType {
Start,
Quit,
}

#[derive(Component)]
pub struct Background(pub Handle<Image>);

fn spawn_menu_buttons(mut commands: Commands, asset_server: Res<AssetServer>) {
let background = asset_server.load("images/menu_background.png");

commands.spawn((
SpriteBundle {
texture: background.clone(),
..Default::default()
},
Background(background),
MenuUI,
));

let button_style: Style = Style {
width: Val::Px(150.0),
height: Val::Px(65.0),
border: UiRect::all(Val::Px(5.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
};

let ui_style = Style {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
flex_direction: FlexDirection::Column,
row_gap: Val::Px(15.),
..default()
};

let text_style = TextStyle {
font_size: 40.0,
color: Color::rgb(0.9, 0.9, 0.9),
..Default::default()
};

commands
.spawn(NodeBundle {
style: ui_style,
..default()
})
.insert(MenuUI)
.with_children(|parent| {
//Start
parent
.spawn(ButtonBundle {
style: button_style.clone(),
border_color: BorderColor(Color::BLACK),
background_color: NORMAL_BUTTON.into(),
..default()
})
.insert(ButtonType::Start)
.with_children(|parent| {
parent.spawn(TextBundle::from_section("Start", text_style.clone()));
});

//Quit
parent
.spawn(ButtonBundle {
style: button_style,
border_color: BorderColor(Color::BLACK),
background_color: NORMAL_BUTTON.into(),
..default()
})
.insert(ButtonType::Quit)
.with_children(|parent| {
parent.spawn(TextBundle::from_section("Quit", text_style));
});
});
}

fn background_system(
mut background: Query<(&mut Transform, &Background)>,
images: Res<Assets<Image>>,
window: Query<&Window>,
) {
let (mut transform, handle) = background.single_mut();
let Some(image) = images.get(handle.0.clone()) else {
return;
};
let window = window.single();

let scale =
(window.width() / image.width() as f32).max(window.height() / image.height() as f32);

transform.scale.x = scale * 0.23;
transform.scale.y = scale * 0.23;
}

fn button_system(
mut interaction_query: Query<
(
&Interaction,
&mut BackgroundColor,
&mut BorderColor,
&ButtonType,
),
(Changed<Interaction>, With<Button>),
>,
mut next_state: ResMut<NextState<GameState>>,
mut exit: EventWriter<AppExit>,
) {
for (interaction, mut color, mut border_color, button_type) in &mut interaction_query {
match *interaction {
Interaction::Pressed => {
*color = PRESSED_BUTTON.into();
border_color.0 = Color::RED;

match *button_type {
ButtonType::Start => next_state.set(GameState::Game),
ButtonType::Quit => exit.send(AppExit),
}
}
Interaction::Hovered => {
*color = HOVERED_BUTTON.into();
border_color.0 = Color::WHITE;
}
Interaction::None => {
*color = NORMAL_BUTTON.into();
border_color.0 = Color::BLACK;
}
}
}
}

pub fn cleanup_menu(mut commands: Commands, menu_ui: Query<Entity, With<MenuUI>>) {
for ent in menu_ui.iter() {
commands.entity(ent).despawn_recursive()
}
}

pub struct MenuPlugin;
impl Plugin for MenuPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
Update,
(button_system, background_system).run_if(in_state(GameState::Menu)),
)
.add_systems(OnEnter(GameState::Menu), spawn_menu_buttons)
.add_systems(OnExit(GameState::Menu), cleanup_menu);
}
}
3 changes: 2 additions & 1 deletion src/particles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ impl Plugin for ParticlesPlugin {
(
hydrate_particles.after(update_player),
update_particles.before(chunk_manager_update),
),
)
.run_if(in_state(GameState::Game)),
);
}
}
7 changes: 4 additions & 3 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,11 +418,12 @@ impl Plugin for PlayerPlugin {
update_player_sprite.after(update_actors),
tool_system.before(chunk_manager_update),
clear_input.after(update_player).after(tool_system),
),
)
.run_if(in_state(GameState::Game)),
)
.add_systems(PreUpdate, get_input)
.add_systems(PreUpdate, get_input.run_if(in_state(GameState::Game)))
.init_resource::<SavingTask>()
.init_resource::<Inputs>()
.add_systems(PostStartup, player_setup.after(manager_setup));
.add_systems(OnEnter(GameState::Game), player_setup.after(manager_setup));
}
}
2 changes: 0 additions & 2 deletions src/puffin_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::prelude::*;
use bevy::window::PrimaryWindow;
use bevy_inspector_egui::bevy_egui::EguiContext;

fn setup() {
puffin::set_scopes_on(true);
Expand Down

0 comments on commit 17853e6

Please sign in to comment.