Compare commits

..
4 Commits
Author SHA1 Message Date
zeefaad 8d496eee73 state modifs 2026-09-18 22:39:29 +02:00
zeefaad 26448cd0a0 main menu 2026-09-18 22:39:13 +02:00
zeefaad 62c06161e7 game states 2026-09-18 22:38:58 +02:00
zeefaad 9ab3431c63 bugs 2026-09-18 22:25:27 +02:00
11 changed files with 145 additions and 98 deletions
+3
View File
@@ -26,5 +26,8 @@
## Ideas ## Ideas
- Camera shake / non-following patterns - Camera shake / non-following patterns
## Bugs
- Camera adapts to screen...
## Assets ## Assets
- Make assets for actual better looking - Make assets for actual better looking
+7 -2
View File
@@ -1,5 +1,6 @@
use bevy::prelude::*; use bevy::prelude::*;
use crate::game_state::GameState;
use crate::player::Player; use crate::player::Player;
use crate::player::move_player; use crate::player::move_player;
@@ -25,7 +26,11 @@ pub struct CameraPlugin;
impl Plugin for CameraPlugin { impl Plugin for CameraPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Startup, spawn_main_camera) app.add_systems(Startup, spawn_main_camera).add_systems(
.add_systems(Update, follow_player.after(move_player)); Update,
follow_player
.run_if(in_state(GameState::InGame))
.after(move_player),
);
} }
} }
+7 -2
View File
@@ -1,6 +1,7 @@
use bevy::prelude::*; use bevy::prelude::*;
use crate::{ use crate::{
game_state::GameState,
obstacle::Obstacle, obstacle::Obstacle,
player::{Player, move_player}, player::{Player, move_player},
}; };
@@ -52,7 +53,11 @@ pub struct CollisionPlugin;
impl Plugin for CollisionPlugin { impl Plugin for CollisionPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.init_resource::<HasCollided>() app.init_resource::<HasCollided>().add_systems(
.add_systems(Update, check_collision_with_player.after(move_player)); Update,
check_collision_with_player
.run_if(in_state(GameState::InGame))
.after(move_player),
);
} }
} }
+15 -7
View File
@@ -1,6 +1,6 @@
use bevy::prelude::*; use bevy::prelude::*;
use crate::{camera::MainCamera, player::Player}; use crate::{camera::MainCamera, game_state::GameState, player::Player};
#[derive(Component)] #[derive(Component)]
pub struct DebugCamera; pub struct DebugCamera;
@@ -103,11 +103,19 @@ pub struct DebugPlugin;
impl Plugin for DebugPlugin { impl Plugin for DebugPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Startup, spawn_debug_camera) app.add_systems(
.add_systems(Startup, setup_debug_camera.after(spawn_debug_camera)) Startup,
.add_systems(Startup, spawn_debug_frame) (
.add_systems(Update, draw_view_frame) spawn_debug_camera,
.add_systems(Update, follow_player_debug) setup_debug_camera.after(spawn_debug_camera),
.add_systems(Update, toggle_debug_camera); spawn_debug_frame,
)
.run_if(in_state(GameState::InGame)),
)
.add_systems(
Update,
(draw_view_frame, follow_player_debug, toggle_debug_camera)
.run_if(in_state(GameState::InGame)),
);
} }
} }
+18
View File
@@ -0,0 +1,18 @@
use bevy::prelude::*;
#[derive(States, Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum GameState {
#[default]
MainMenu,
InGame,
SettingsMenu,
GameOver,
}
pub struct GameStatePlugin;
impl Plugin for GameStatePlugin {
fn build(&self, app: &mut App) {
app.init_state::<GameState>();
}
}
+6 -2
View File
@@ -4,7 +4,8 @@ use crate::bounds::BoundsPlugin;
use crate::camera::CameraPlugin; use crate::camera::CameraPlugin;
use crate::collision::CollisionPlugin; use crate::collision::CollisionPlugin;
use crate::debug::DebugPlugin; use crate::debug::DebugPlugin;
use crate::obstacle::ObstaclePlugin; use crate::game_state::GameStatePlugin;
use crate::main_menu::MainMenuPlugin;
use crate::player::PlayerPlugin; use crate::player::PlayerPlugin;
use crate::score::ScorePlugin; use crate::score::ScorePlugin;
use crate::world::WorldPlugin; use crate::world::WorldPlugin;
@@ -14,6 +15,8 @@ mod bounds;
mod camera; mod camera;
mod collision; mod collision;
mod debug; mod debug;
mod game_state;
mod main_menu;
mod obstacle; mod obstacle;
mod player; mod player;
mod score; mod score;
@@ -33,10 +36,11 @@ fn main() {
.add_plugins(PlayerPlugin) // Player .add_plugins(PlayerPlugin) // Player
.add_plugins(CameraPlugin) // Camera .add_plugins(CameraPlugin) // Camera
.add_plugins(CollisionPlugin) // Collisions .add_plugins(CollisionPlugin) // Collisions
.add_plugins(ObstaclePlugin) // Obstacles
.add_plugins(BoundsPlugin) // Bound player .add_plugins(BoundsPlugin) // Bound player
.add_plugins(ScorePlugin) // Score .add_plugins(ScorePlugin) // Score
.add_plugins(WorldPlugin) // Chunk logic .add_plugins(WorldPlugin) // Chunk logic
.add_plugins(DebugPlugin) // Debug .add_plugins(DebugPlugin) // Debug
.add_plugins(GameStatePlugin) // Game states
.add_plugins(MainMenuPlugin) // Main Menu
.run(); .run();
} }
+60
View File
@@ -0,0 +1,60 @@
use bevy::prelude::*;
use crate::game_state::GameState;
// To delete menu's components
#[derive(Component)]
pub struct MainMenu;
fn main_menu(mut commands: Commands) {
commands.spawn((
MainMenu,
Node {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
flex_direction: FlexDirection::Column,
row_gap: Val::Px(30.0),
..default()
},
children![
(
Text::new("DOWN INTO THE VOID"),
TextFont {
font_size: FontSize::Px(120.0),
..default()
},
),
(
Text::new("Press Enter to fall into the void"),
TextFont {
font_size: FontSize::Px(40.0),
..default()
}
)
],
));
}
fn cleanup_menu(mut commands: Commands, menu: Query<Entity, With<MainMenu>>) {
for entity in menu {
commands.entity(entity).despawn();
}
}
fn menu_input(input: Res<ButtonInput<KeyCode>>, mut next_state: ResMut<NextState<GameState>>) {
if input.just_pressed(KeyCode::Enter) {
next_state.set(GameState::InGame);
}
}
pub struct MainMenuPlugin;
impl Plugin for MainMenuPlugin {
fn build(&self, app: &mut App) {
app.add_systems(OnEnter(GameState::MainMenu), main_menu)
.add_systems(Update, menu_input.run_if(in_state(GameState::MainMenu)))
.add_systems(OnExit(GameState::MainMenu), cleanup_menu);
}
}
-66
View File
@@ -8,69 +8,3 @@ use rand::SeedableRng;
#[derive(Component)] #[derive(Component)]
pub struct Obstacle; pub struct Obstacle;
#[derive(Resource, Default)]
pub struct RegenerateObstacles(pub bool);
fn spawn_obstacle(commands: &mut Commands, position: Vec2, size: Vec2) {
commands.spawn((
Obstacle,
Collider { size },
Transform::from_xyz(position.x, position.y, 1.0),
Sprite::from_color(Color::srgb(1.0, 0.5, 0.1), size),
));
}
// Temporary fonction for display random obstacles
fn spawn_random_obstacles(commands: &mut Commands) {
let mut rng = rand::rng();
let min_x = -960.0;
let max_x = 960.0;
let mut y = -1000.0;
for _ in 0..100 {
let obstacle_count = rng.random_range(1..=3);
for _ in 0..obstacle_count {
let width = rng.random_range(100.0..=400.0);
let height = rng.random_range(20.0..=200.0);
let x = rng.random_range((min_x + width / 2.0)..=(max_x - width / 2.0));
spawn_obstacle(commands, Vec2::new(x, y), Vec2::new(width, height));
}
y -= rng.random_range(80.0..=160.0);
}
}
fn regenerate_obstacles(
mut commands: Commands,
obstacles: Query<Entity, With<Obstacle>>,
mut regenerate_obstacles: ResMut<RegenerateObstacles>,
) {
if regenerate_obstacles.0 {
for obstacle in obstacles {
commands.entity(obstacle).despawn();
}
spawn_random_obstacles(&mut commands);
regenerate_obstacles.0 = false;
}
}
fn spawn_obstacles(mut commands: Commands) {
spawn_random_obstacles(&mut commands);
}
pub struct ObstaclePlugin;
impl Plugin for ObstaclePlugin {
fn build(&self, app: &mut App) {
app
// .add_systems(Startup, spawn_obstacles)
.init_resource::<RegenerateObstacles>()
.add_systems(Update, regenerate_obstacles.after(reset_player));
}
}
+11 -7
View File
@@ -2,13 +2,13 @@ use bevy::prelude::*;
use crate::collision::check_collision_with_player; use crate::collision::check_collision_with_player;
use crate::collision::{Collider, HasCollided}; use crate::collision::{Collider, HasCollided};
use crate::obstacle::RegenerateObstacles; use crate::game_state::GameState;
const FALL_SPEED: f32 = 600.0; // Falling speed const FALL_SPEED: f32 = 600.0; // Falling speed
const DIVE_SPEED: f32 = 1000.0; // Falling speed sped up const DIVE_SPEED: f32 = 1000.0; // Falling speed sped up
const HORIZONTAL_SPEED: f32 = 350.0; // Lateral movement speed const HORIZONTAL_SPEED: f32 = 500.0; // Lateral movement speed
pub const START_POSITION: Vec3 = Vec3::new(0.0, 0.0, 2.0); // Start position pub const START_POSITION: Vec3 = Vec3::new(0.0, 0.0, 2.0); // Start position
const RATE: f32 = 13.0; // Inertia rate // const RATE: f32 = 13.0; // Inertia rate
// The player // The player
#[derive(Component)] #[derive(Component)]
@@ -67,7 +67,7 @@ pub(crate) fn move_player(
0.0 0.0
}; };
let facteur = 1.0 - (-RATE * time.delta_secs()).exp(); // let facteur = 1.0 - (-RATE * time.delta_secs()).exp();
let target = Vec2::new( let target = Vec2::new(
direction * HORIZONTAL_SPEED, direction * HORIZONTAL_SPEED,
-if input.pressed(KeyCode::KeyS) { -if input.pressed(KeyCode::KeyS) {
@@ -77,7 +77,8 @@ pub(crate) fn move_player(
}, },
); );
velocity.0 = velocity.0.lerp(target, facteur); // velocity.0 = velocity.0.lerp(target, facteur);
velocity.0 = target;
// Position update (no smoothness in velocity) // Position update (no smoothness in velocity)
transform.translation += velocity.0.extend(0.0) * time.delta_secs(); transform.translation += velocity.0.extend(0.0) * time.delta_secs();
@@ -89,7 +90,7 @@ pub fn reset_player(
input: Res<ButtonInput<KeyCode>>, input: Res<ButtonInput<KeyCode>>,
mut has_collided: ResMut<HasCollided>, mut has_collided: ResMut<HasCollided>,
mut fall_started: ResMut<FallStarted>, mut fall_started: ResMut<FallStarted>,
mut regenerate_obstacles: ResMut<RegenerateObstacles>, // mut regenerate_obstacles: ResMut<RegenerateObstacles>,
) { ) {
let (mut transform, mut velocity, mut sprite) = player.into_inner(); let (mut transform, mut velocity, mut sprite) = player.into_inner();
@@ -112,12 +113,15 @@ impl Plugin for PlayerPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Startup, spawn_player) app.add_systems(Startup, spawn_player)
.init_resource::<FallStarted>() .init_resource::<FallStarted>()
.add_systems(Update, move_player)
.add_systems( .add_systems(
Update, Update,
(
move_player,
reset_player reset_player
.after(move_player) .after(move_player)
.after(check_collision_with_player), .after(check_collision_with_player),
)
.run_if(in_state(GameState::InGame)),
); );
} }
} }
+9 -3
View File
@@ -1,6 +1,9 @@
use bevy::prelude::*; use bevy::prelude::*;
use crate::player::{Player, START_POSITION, move_player}; use crate::{
game_state::GameState,
player::{Player, START_POSITION, move_player},
};
#[derive(Resource, Default)] #[derive(Resource, Default)]
pub struct Depth(pub f32); pub struct Depth(pub f32);
@@ -49,7 +52,10 @@ impl Plugin for ScorePlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Startup, spawn_depth_text) app.add_systems(Startup, spawn_depth_text)
.init_resource::<Depth>() .init_resource::<Depth>()
.add_systems(Update, update_depth.after(move_player)) .add_systems(
.add_systems(Update, update_depth_text); Update,
(update_depth_text, update_depth.after(move_player))
.run_if(in_state(GameState::InGame)),
);
} }
} }
+6 -6
View File
@@ -1,10 +1,10 @@
use bevy::prelude::*; use bevy::prelude::*;
use crate::{camera::MainCamera, player::Player}; use crate::{camera::MainCamera, game_state::GameState, player::Player};
pub const CHUNK_HEIGHT: f32 = 200.0; // Height a cunk pub const CHUNK_HEIGHT: f32 = 600.0; // Height a cunk
const MARGIN_BELLOW: f32 = 500.0; // Height bellow the camera view it'll be gen const MARGIN_BELLOW: f32 = 400.0; // Height bellow the camera view it'll be gen
const MARGIN_ABOVE: f32 = 500.0; // Height above the camera view it'll be gen const MARGIN_ABOVE: f32 = 400.0; // Height above the camera view it'll be gen
#[derive(Component)] #[derive(Component)]
pub struct Chunk { pub struct Chunk {
@@ -55,13 +55,13 @@ fn visible_chunk_range(player_y: f32, camera_y: f32) -> (i64, i64) {
fn spawn_chunk(commands: &mut Commands, id: i64, run_seed: u64) { fn spawn_chunk(commands: &mut Commands, id: i64, run_seed: u64) {
crate::background::spawn_background_chunk(commands, id); crate::background::spawn_background_chunk(commands, id);
// TODO: obstacles // crate::obstacle::spawn_obstacle_chunk(commands, id, run_seed);
} }
pub struct WorldPlugin; pub struct WorldPlugin;
impl Plugin for WorldPlugin { impl Plugin for WorldPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.insert_resource(RunSeed(rand::random::<u64>())) app.insert_resource(RunSeed(rand::random::<u64>()))
.add_systems(Update, stream_world); .add_systems(Update, stream_world.run_if(in_state(GameState::InGame)));
} }
} }