Skip to content

Commit

Permalink
Feature score 01 (#22)
Browse files Browse the repository at this point in the history
* feat: add score

* fix: repair the opening of the menu

menu is buged and starting and unopening crashes the game

* feat: add score

* fix: repair the opening of the menu

menu is buged and starting and unopening crashes the game

* alocate camera spawning

* broken scene switch

* fix: only spawn player if he does not exsist yet

* fix scene switch problem and code cleanup

---------

Co-authored-by: Space Nerd <71553327+SpaceNerde@users.noreply.github.com>
Co-authored-by: Space Nerd <spacenerddev@gmail.com>
  • Loading branch information
3 people authored Dec 10, 2023
1 parent 8345bf3 commit 8f6fdf1
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 32 deletions.
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod player;
mod spawner;
mod ui_handler;
mod voxel_painting;
mod scene_handler;

use crate::actions::ActionsPlugin;
use crate::audio::InternalAudioPlugin;
Expand All @@ -25,6 +26,7 @@ use crate::menu::MenuPlugin;
use crate::player::PlayerPlugin;
use crate::ui_handler::UiHandlerPlugin;
use crate::voxel_painting::paint_voxel_system;
use crate::scene_handler::SceneSwitchPlugin;
use bevy::app::App;
#[cfg(debug_assertions)]
use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin};
Expand All @@ -51,6 +53,7 @@ impl Plugin for GamePlugin {
app.add_state::<GameState>()
.add_plugins((
LoadingPlugin,
SceneSwitchPlugin,
MenuPlugin,
ActionsPlugin,
InternalAudioPlugin,
Expand Down
10 changes: 0 additions & 10 deletions src/map_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,6 @@ pub fn map_setup(mut commands: Commands) {

// --- Just scene setup below ---

// camera
commands.spawn((
Camera3dBundle {
transform: Transform::from_xyz(-200.0, 180.0, -200.0),
..default()
},
// This tells bevy_voxel_world tos use this cameras transform to calculate spawning area
VoxelWorldCamera,
));

// Sun
let cascade_shadow_config = CascadeShadowConfigBuilder { ..default() }.build();
commands.spawn(DirectionalLightBundle {
Expand Down
22 changes: 3 additions & 19 deletions src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::core_components::ChangeState;
use crate::loading::TextureAssets;
use crate::GameState;
use bevy::prelude::*;
use bevy_voxel_world::prelude::*;

pub struct MenuPlugin;

/// This plugin is responsible for the game menu (containing only one button...)
Expand All @@ -21,9 +21,6 @@ struct ButtonColors {
hovered: Color,
}

#[derive(Component)]
struct MenuCamera;

impl Default for ButtonColors {
fn default() -> Self {
ButtonColors {
Expand All @@ -39,14 +36,8 @@ struct Menu;
fn setup_menu(
mut commands: Commands,
textures: Res<TextureAssets>,
mut game_camera_query: Query<&mut Camera, With<VoxelWorldCamera>>,
) {
// disable Game Camera
let mut camera = game_camera_query.single_mut();
camera.is_active = false;

info!("menu");
commands.spawn((Camera2dBundle::default(), MenuCamera));
commands
.spawn((
NodeBundle {
Expand Down Expand Up @@ -202,21 +193,14 @@ fn click_play_button(
),
(Changed<Interaction>, With<Button>),
>,
mut menu_camera_query: Query<&mut Camera, With<MenuCamera>>,
mut game_camera_query: Query<&mut Camera, (With<VoxelWorldCamera>, Without<MenuCamera>)>,
mut commands: Commands,
) {
for (interaction, mut color, button_colors, change_state, open_link) in &mut interaction_query {
match *interaction {
Interaction::Pressed => {
// disable menu camera to avoid multiple cameras at once
let mut menu_camera = menu_camera_query.single_mut();
menu_camera.is_active = false;

// enable Game Camera
let mut game_camera = game_camera_query.single_mut();
game_camera.is_active = true;
if let Some(state) = change_state {
next_state.set(state.0.clone());
commands.spawn(ChangeState(GameState::Playing));
} else if let Some(link) = open_link {
if let Err(error) = webbrowser::open(link.0) {
warn!("Failed to open link {error:?}");
Expand Down
38 changes: 36 additions & 2 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::core_components::*;
use crate::voxel_painting::get_surface_air_voxel;
use crate::GameState;
use bevy::prelude::*;
use bevy::window::CursorGrabMode;
use bevy_voxel_world::prelude::*;

pub struct PlayerPlugin;
Expand All @@ -17,14 +18,19 @@ impl Plugin for PlayerPlugin {
app.add_systems(OnEnter(GameState::Playing), spawn_player)
.add_systems(Update, move_player.run_if(in_state(GameState::Playing)))
.add_systems(Update, player_click.run_if(in_state(GameState::Playing)))
.add_systems(Update, open_menu);
.add_systems(Update, open_menu)
.add_systems(OnExit(GameState::Playing), cleanup);
}
}

fn spawn_player(
mut commands: Commands,
mut cam_transform: Query<&mut Transform, (With<VoxelWorldCamera>, Without<Player>)>,
query: Query<&Player>
) {
for _ in query.iter(){
return;
}
commands
.spawn((
SpriteBundle {
Expand Down Expand Up @@ -95,9 +101,37 @@ fn player_click(
}
}

#[derive(Component)]
struct OpenLink(&'static str);

// FIXME: doesn't seem to do anything
fn open_menu(mut commands: Commands, actions: Res<Actions>) {
fn open_menu(
mut commands: Commands,
actions: Res<Actions>,
querry: Query<(Option<&ChangeState>, Option<&OpenLink>)>,
mut next_state: ResMut<NextState<GameState>>,
) {
for (change_state, open_link) in &querry {
if actions.open_menu {
if let Some(state) = change_state {
next_state.set(state.0.clone());
} else if let Some(link) = open_link {
if let Err(error) = webbrowser::open(link.0) {
warn!("Failed to open link {error:?}");
}
}
}
}
if actions.open_menu {
commands.spawn(ChangeState(GameState::Menu));
}
}

fn cleanup(
mut windows: Query<&mut Window>
) {
let mut window = windows.single_mut();

window.cursor.visible = true;
window.cursor.grab_mode = CursorGrabMode::None;
}
58 changes: 58 additions & 0 deletions src/scene_handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use crate::GameState;
use bevy::prelude::*;
use bevy_voxel_world::prelude::*;

pub struct SceneSwitchPlugin;

/// This Plugin configurats the cameras so that only one is active at a time
impl Plugin for SceneSwitchPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, setup_cameras)
.add_systems(OnEnter(GameState::Playing), enter_playing)
.add_systems(OnEnter(GameState::Menu), enter_menu);
}
}

#[derive(Component)]
pub struct MenuCamera;

fn setup_cameras(
mut commands: Commands,
){
// Spawn Cameras

// Menu Camera
commands.spawn((Camera2dBundle::default(), MenuCamera));

// Game Camera
commands.spawn((
Camera3dBundle {
transform: Transform::from_xyz(-200.0, 180.0, -200.0),
..default()
},
// This tells bevy_voxel_world tos use this cameras transform to calculate spawning area
VoxelWorldCamera,
));
}

fn enter_playing(
mut menu_camera_query: Query<&mut Camera, With<MenuCamera>>,
mut game_camera_query: Query<&mut Camera, (With<VoxelWorldCamera>, Without<MenuCamera>)>,
){
let mut menu_camera = menu_camera_query.single_mut();
menu_camera.is_active = false;

let mut game_camera = game_camera_query.single_mut();
game_camera.is_active = true;
}

fn enter_menu(
mut menu_camera_query: Query<&mut Camera, With<MenuCamera>>,
mut game_camera_query: Query<&mut Camera, (With<VoxelWorldCamera>, Without<MenuCamera>)>,
){
let mut menu_camera = menu_camera_query.single_mut();
menu_camera.is_active = true;

let mut game_camera = game_camera_query.single_mut();
game_camera.is_active = false;
}
9 changes: 8 additions & 1 deletion src/ui_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct HudData {
time_since_update: f32,
entities: u32,
resource_count: u32,
score: u32,
}

#[derive(Component)]
Expand All @@ -41,11 +42,17 @@ fn render_ui(
if hud_data.time_since_update > 1.0 {
hud_data.time_since_update = 0.0;
hud_data.entities = entities.len();
hud_data.score = inventory_query
.iter()
.map(|inv| inv.resources.values().sum::<u32>())
.sum();
hud_data.resource_count = inventory_query
.iter()
.map(|inv| inv.resources.values().sum::<u32>())
.sum();
}


hud_query.for_each(|hud| commands.entity(hud).despawn_recursive());
// render the score, resources and the entities
commands
Expand All @@ -65,7 +72,7 @@ fn render_ui(
))
.with_children(|children| {
children.spawn(TextBundle::from_section(
"Score: 100",
format!("Score: {}", hud_data.score),
TextStyle {
font_size: 40.0,
color: Color::rgb(0.9, 0.9, 0.9),
Expand Down

0 comments on commit 8f6fdf1

Please sign in to comment.