diff --git a/src/debug.rs b/src/debug.rs index a438e01..3680d1c 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -1,7 +1,5 @@ use bevy::{ - asset::uuid::timestamp::context, diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin}, - math::curve::cores::ChunkedUnevenCoreError, prelude::*, }; use bevy_egui::{EguiContexts, EguiPrimaryContextPass, egui}; @@ -24,9 +22,6 @@ pub struct Debug; #[derive(Component)] pub struct DebugCamera; -#[derive(Component)] -struct ViewDebugText; - fn spawn_debug_camera(mut commands: Commands) { commands.spawn(( Debug, @@ -129,92 +124,6 @@ fn draw_view_frame( } } -#[derive(Component)] -struct SeedDebugText; - -#[derive(Component)] -struct FpsDebugText; - -// Debug information on screen -fn spawn_debug_texts(mut commands: Commands) { - commands.spawn(( - Debug, - Node { - position_type: PositionType::Absolute, - right: Val::Px(0.0), - top: Val::Px(0.0), - width: Val::Percent(17.0), - height: Val::Percent(10.0), - flex_direction: FlexDirection::Column, - row_gap: Val::Px(5.0), - ..default() - }, - // Black background - BackgroundColor(Color::srgba(0.0, 1.0, 0.0, 0.1)), - // Texts - children![ - // Seed display - ( - Text::new(""), - TextFont { - font_size: FontSize::Px(20.0), - ..default() - }, - TextColor(Color::srgb(1.0, 1.0, 0.0)), - SeedDebugText, - ), - ( - Text::new(""), - TextFont { - font_size: FontSize::Px(20.0), - ..default() - }, - TextColor(Color::srgb(1.0, 1.0, 0.0)), - FpsDebugText, - ), - ( - Text::new(""), - TextFont { - font_size: FontSize::Px(20.0), - ..default() - }, - TextColor(Color::srgb(1.0, 1.0, 0.0)), - ViewDebugText, - ) - ], - )); -} - -// Update depth score texts -fn update_seed_debug_text( - run_seed: Res, - mut run_seed_debug_text: Single<&mut Text, With>, -) { - run_seed_debug_text.0 = format!("Seed: {}", run_seed.0); -} - -fn update_fps_debug_text( - store: Res, - mut fps_debug_text: Single<&mut Text, With>, -) { - if let Some(fps) = store.get(&FrameTimeDiagnosticsPlugin::FPS) { - if let Some(value) = fps.smoothed() { - fps_debug_text.0 = format!("FPS: {:.0}", value); - } - } -} - -fn cleanup_debug(mut commands: Commands, entities: Query>) { - for entity in entities { - commands.entity(entity).despawn(); - } -} - -// Show the world rect the game camera renders -fn update_view_debug_text(mut text: Single<&mut Text, With>) { - text.0 = format!("View: {SCREEN_WIDTH:.0}x{SCREEN_HEIGHT:.0}"); -} - // Real time tuning movement & inertia sliders fn move_tuning_panel(mut contexts: EguiContexts, mut settings: ResMut) { egui::Window::new("Movement / Inertia ").show( @@ -268,12 +177,12 @@ fn debug_panel( mut run_seed: ResMut, player: Single<(Entity, &Transform, &Velocity), With>, chunk: Query>, - mut main_camera: Single<&mut Camera, (With, Without)>, - mut debug_camera: Single<&mut Camera, (With, Without)>, + main_camera: Single<&mut Camera, (With, Without)>, + debug_camera: Single<&mut Camera, (With, Without)>, mut commands: Commands, mut fall_started: ResMut, mut depth: ResMut, - mut display_image: Single<&mut Visibility, With>, + display_image: Single<&mut Visibility, With>, ) { egui::Window::new("Debug").show(contexts.ctx_mut().expect("gui context missing"), |ui| { // Fps @@ -340,6 +249,12 @@ fn debug_panel( }); } +fn cleanup_debug(mut commands: Commands, entities: Query>) { + for entity in entities { + commands.entity(entity).despawn(); + } +} + pub struct DebugPlugin; impl Plugin for DebugPlugin { @@ -350,19 +265,11 @@ impl Plugin for DebugPlugin { spawn_debug_camera, setup_debug_camera.after(spawn_debug_camera), spawn_debug_frame, - // spawn_debug_texts, ), ) .add_systems( Update, - ( - draw_view_frame, - follow_player_debug, - // update_seed_debug_text, - // update_fps_debug_text, - // update_view_debug_text, - ) - .run_if(in_state(GameState::InGame)), + (draw_view_frame, follow_player_debug).run_if(in_state(GameState::InGame)), ) // Egui debug .add_systems( diff --git a/src/gameplay/collision.rs b/src/gameplay/collision.rs index 693645a..5a39c45 100644 --- a/src/gameplay/collision.rs +++ b/src/gameplay/collision.rs @@ -14,11 +14,11 @@ pub struct Collider { // Check collision with Player pub fn check_collision_with_player( - player: Single<(&Transform, &Collider, &mut Sprite), With>, + player: Single<(&Transform, &Collider), With>, obstacle: Query<(&Transform, &Collider), With>, mut next_state: ResMut>, ) { - let (player_transform, player_collider, mut _player_sprite) = player.into_inner(); + let (player_transform, player_collider) = player.into_inner(); // Check whether or not each obstacles overlap the player for (collider_transform, collider_collider) in obstacle { diff --git a/src/gameplay/player.rs b/src/gameplay/player.rs index 118c6b9..b19bbae 100644 --- a/src/gameplay/player.rs +++ b/src/gameplay/player.rs @@ -9,7 +9,6 @@ use crate::config::PLAYER_HITBOX; use crate::config::START_POSITION; use crate::config::VERTICAL_RATE; use crate::gameplay::collision::Collider; -use crate::gameplay::collision::check_collision_with_player; use crate::states::GameState; // The player @@ -126,27 +125,6 @@ pub(crate) fn move_player( transform.translation += velocity.0.extend(0.0) * dt; } -// Debug -pub fn debug_game_over( - // player: Single<(&mut Transform, &mut Velocity, &mut Sprite), With>, - input: Res>, - mut fall_started: ResMut, - mut next_state: ResMut>, // For debug gameover state -) { - // let (mut transform, mut velocity, mut sprite) = player.into_inner(); - - // R key for reset or player has collider - if input.just_pressed(KeyCode::KeyG) { - // transform.translation = Vec3::new(START_POSITION.x, START_POSITION.y, START_POSITION.z); - // velocity.0 = Vec2::ZERO; - // sprite.color = Color::WHITE; - - fall_started.0 = false; - next_state.set(GameState::GameOver); - // TODO: uncomment when obstacle chunk logic is implemented - } -} - // To freeze player at the begining of the Ingame fn start_run(mut fall_started: ResMut) { fall_started.0 = false; @@ -163,16 +141,7 @@ impl Plugin for PlayerPlugin { app.add_systems(OnEnter(GameState::InGame), (spawn_player, start_run)) .init_resource::() .init_resource::() - .add_systems( - Update, - ( - move_player, - debug_game_over - .after(move_player) - .after(check_collision_with_player), - ) - .run_if(in_state(GameState::InGame)), - ) + .add_systems(Update, (move_player,).run_if(in_state(GameState::InGame))) .add_systems(OnExit(GameState::InGame), cleanup_player); } } diff --git a/src/main.rs b/src/main.rs index aee91e2..8c11bf2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,3 @@ -use std::time::UNIX_EPOCH; - use bevy::{prelude::*, window::PresentMode}; use bevy_egui::{EguiGlobalSettings, EguiPlugin}; diff --git a/src/states.rs b/src/states.rs index ae492f4..43bfc70 100644 --- a/src/states.rs +++ b/src/states.rs @@ -8,9 +8,9 @@ pub enum GameState { #[default] MainMenu, InGame, - // SettingsMenu, GameOver, - // EscMenu + _SettingsMenu, + _EscMenu, } pub struct GameStatePlugin; diff --git a/src/world.rs b/src/world.rs index f84b483..7ffdedc 100644 --- a/src/world.rs +++ b/src/world.rs @@ -1,10 +1,8 @@ use bevy::prelude::*; use crate::{ - config::{ - CAMERAE_OFFSET, CHUNK_HEIGHT, MARGIN_ABOVE, MARGIN_BELLOW, SCREEN_HEIGHT, SCREEN_WIDTH, - }, - gameplay::{camera::MainCamera, player::Player}, + config::{CAMERAE_OFFSET, CHUNK_HEIGHT, MARGIN_ABOVE, MARGIN_BELLOW, SCREEN_HEIGHT}, + gameplay::player::Player, states::GameState, }; @@ -13,7 +11,6 @@ pub mod doors; pub mod generator; pub mod grid; pub mod obstacle; -pub mod rng; pub mod validator; #[derive(Component)] diff --git a/src/world/doors.rs b/src/world/doors.rs index 4575cc5..6cf0b59 100644 --- a/src/world/doors.rs +++ b/src/world/doors.rs @@ -1,4 +1,6 @@ -use crate::{config::TERRAIN_COLS, world::rng::SplitMix64, world::rng::derive}; +use rand::{RngExt, SeedableRng, rngs::StdRng}; + +use crate::config::TERRAIN_COLS; const SALT_DOOR: u64 = 1; @@ -31,15 +33,13 @@ pub fn door(seed: u64, k: i64) -> Option { }); } - let mut rng = SplitMix64::new(derive(seed, k, SALT_DOOR)); + let mut rng = StdRng::seed_from_u64(seed ^ (k as u64).wrapping_mul(SALT_DOOR)); // Random width - let width = DOOR_MIN_WIDTH - + (rng.next_u64() % (2 * DOOR_HALF_WIDTH as u64 - DOOR_MIN_WIDTH as u64 + 1)) as i32; + let width = DOOR_MIN_WIDTH + rng.random_range(0..(2 * DOOR_HALF_WIDTH - DOOR_MIN_WIDTH + 1)); - // DOor stay in terrain - let center = - DOOR_HALF_WIDTH + (rng.next_u64() % (TERRAIN_COLS - 2 * DOOR_HALF_WIDTH) as u64) as i32; + // Door stays in terrain + let center = DOOR_HALF_WIDTH + rng.random_range(0..(TERRAIN_COLS - 2 * DOOR_HALF_WIDTH)); let min_col = (center - width / 2).clamp(1, TERRAIN_COLS - 1 - width); Some(Door { diff --git a/src/world/obstacle.rs b/src/world/obstacle.rs index 8c2108d..5a066db 100644 --- a/src/world/obstacle.rs +++ b/src/world/obstacle.rs @@ -1,15 +1,7 @@ use bevy::prelude::*; -use crate::{ - gameplay::collision::Collider, - world::{ - Chunk, - grid::{GridRect, grid_to_world}, - }, -}; - // const OBSTACLE_COLOR: Color = Color::srgb(0.1, 0.7, 0.3); -const DEBUG_OBSTACLE_COLOR: Color = Color::srgba(0.0, 1.0, 0.0, 1.0); +const _DEBUG_OBSTACLE_COLOR: Color = Color::srgba(0.0, 1.0, 0.0, 1.0); #[derive(Component)] pub struct Obstacle; @@ -18,23 +10,4 @@ pub fn spawn_obstacle_chunk(commands: &mut Commands, id: i64, run_seed: u64) { if id >= -1 { return; } - - let depth = -id; - - // let data = generate_chunk(run_seed, depth); - - // for rect in data.rects { - // spawn_rect(commands, id, rect); - // } -} - -fn spawn_rect(commands: &mut Commands, id: i64, rect: GridRect) { - let (center, size) = grid_to_world(id, rect); - commands.spawn(( - Chunk { id }, - Obstacle, - // Collider { size }, - Sprite::from_color(DEBUG_OBSTACLE_COLOR, size), - Transform::from_xyz(center.x, center.y, 0.5), - )); } diff --git a/src/world/rng.rs b/src/world/rng.rs deleted file mode 100644 index efabfb3..0000000 --- a/src/world/rng.rs +++ /dev/null @@ -1,32 +0,0 @@ -// Pseudo random generator SplitMix64 - -pub const GENERATOR_VERSION: u32 = 1; - -const MIX_GAMMA: u64 = 0x9E37_79B9_7F4A_7C15; -const MIX_1: u64 = 0xBF58_476D_1CE4_E5B9; -const MIX_2: u64 = 0x94D0_49BB_1331_11EB; - -fn splitmix64_mix(mut z: u64) -> u64 { - z = (z ^ (z >> 30)).wrapping_mul(MIX_1); - z = (z ^ (z >> 27)).wrapping_mul(MIX_2); - z ^ (z >> 31) -} - -pub struct SplitMix64 { - state: u64, -} - -impl SplitMix64 { - pub fn new(seed: u64) -> Self { - Self { state: seed } - } - - pub fn next_u64(&mut self) -> u64 { - self.state = self.state.wrapping_add(MIX_GAMMA); - splitmix64_mix(self.state) - } -} - -pub fn derive(seed: u64, index: i64, salt: u64) -> u64 { - splitmix64_mix(seed ^ (index as u64).wrapping_mul(MIX_GAMMA) ^ salt.rotate_left(32)) -}