From 56d90253bd4d9f16229602111628d69ab74cd1f3 Mon Sep 17 00:00:00 2001 From: zefad Date: Sat, 19 Sep 2026 16:32:04 +0200 Subject: [PATCH] grid logic --- .gitignore | 1 + src/config.rs | 21 +++++++++++++++++++++ src/gameplay/bounds.rs | 15 +++++---------- src/gameplay/player.rs | 15 +++++++-------- src/gameplay/score.rs | 3 ++- src/main.rs | 1 + src/states.rs | 3 ++- src/world.rs | 17 ++++++++--------- src/world/background.rs | 30 ++++++++++++++++-------------- src/world/grid.rs | 41 +++++++++++++++++++++++++++++++++++++++++ src/world/obstacle.rs | 6 ------ src/world/rng.rs | 0 12 files changed, 104 insertions(+), 49 deletions(-) create mode 100644 src/config.rs create mode 100644 src/world/grid.rs create mode 100644 src/world/rng.rs diff --git a/.gitignore b/.gitignore index ea8c4bf..fc6e9cc 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +/docs diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..2bf01f2 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,21 @@ +use bevy::prelude::*; + +// Grid +pub const CELL_SIZE: f32 = 32.0; +pub const TERRAIN_COLS: i32 = 60; +pub const CHUNK_ROWS: i32 = 32; + +pub const TERRAIN_WIDTH: f32 = CELL_SIZE * TERRAIN_COLS as f32; +pub const HALF_TERRAIN_WIDTH: f32 = TERRAIN_WIDTH / 2.0; +pub const CHUNK_HEIGHT: f32 = CELL_SIZE * CHUNK_ROWS as f32; + +// Streaming +pub const MARGIN_ABOVE: f32 = 400.0; +pub const MARGIN_BELLOW: f32 = 400.0; + +// Player +pub const PLAYER_HITBOX: Vec2 = Vec2::new(20.0, 40.0); +pub const FALL_SPEED: f32 = 600.0; +pub const DIVE_SPEED: f32 = 1000.0; +pub const HORIZONTAL_SPEED: f32 = 500.0; +pub const START_POSITION: Vec3 = Vec3::new(0.0, 0.0, 2.0); diff --git a/src/gameplay/bounds.rs b/src/gameplay/bounds.rs index 5c17565..751d37c 100644 --- a/src/gameplay/bounds.rs +++ b/src/gameplay/bounds.rs @@ -1,21 +1,16 @@ use bevy::prelude::*; use crate::{ - gameplay::camera::MainCamera, gameplay::collision::Collider, gameplay::player::Player, + config::HALF_TERRAIN_WIDTH, + gameplay::{collision::Collider, player::Player}, states::GameState, }; -fn constrain_player( - mut player: Single<(&Collider, &mut Transform), With>, - camera: Single<&Camera, With>, -) { - let viewport = camera - .logical_viewport_size() - .unwrap_or(Vec2::new(1920.0, 1080.0)); +fn constrain_player(mut player: Single<(&Collider, &mut Transform), With>) { let half_width = player.0.size.x / 2.0; - let border = viewport.x / 2.0 - half_width; + let border = HALF_TERRAIN_WIDTH - half_width; - // Restrict player position + // Restrict player position to the terrain widht player.1.translation.x = player.1.translation.x.clamp(-border, border); } diff --git a/src/gameplay/player.rs b/src/gameplay/player.rs index 4d55620..6c4e5a0 100644 --- a/src/gameplay/player.rs +++ b/src/gameplay/player.rs @@ -1,15 +1,14 @@ use bevy::prelude::*; +use crate::config::DIVE_SPEED; +use crate::config::FALL_SPEED; +use crate::config::HORIZONTAL_SPEED; +use crate::config::PLAYER_HITBOX; +use crate::config::START_POSITION; use crate::gameplay::collision::Collider; use crate::gameplay::collision::check_collision_with_player; use crate::states::GameState; -const FALL_SPEED: f32 = 600.0; // Falling speed -const DIVE_SPEED: f32 = 1000.0; // Falling speed sped up -const HORIZONTAL_SPEED: f32 = 500.0; // Lateral movement speed -pub const START_POSITION: Vec3 = Vec3::new(0.0, 0.0, 2.0); // Start position -// const RATE: f32 = 13.0; // Inertia rate - // The player #[derive(Component)] pub struct Player; @@ -27,10 +26,10 @@ fn spawn_player(mut commands: Commands) { commands.spawn(( Player, Collider { - size: Vec2::new(20.0, 40.0), + size: PLAYER_HITBOX, }, Velocity(Vec2::ZERO), - Sprite::from_color(Color::srgb(1.0, 1.0, 1.0), Vec2::new(20.0, 40.0)), + Sprite::from_color(Color::srgb(1.0, 1.0, 1.0), PLAYER_HITBOX), Transform::from_xyz(START_POSITION.x, START_POSITION.y, START_POSITION.z), )); } diff --git a/src/gameplay/score.rs b/src/gameplay/score.rs index 81b952d..fcdac09 100644 --- a/src/gameplay/score.rs +++ b/src/gameplay/score.rs @@ -1,7 +1,8 @@ use bevy::prelude::*; use crate::{ - gameplay::player::{Player, START_POSITION, move_player}, + config::START_POSITION, + gameplay::player::{Player, move_player}, states::GameState, }; diff --git a/src/main.rs b/src/main.rs index 77ff1ff..29842d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,7 @@ use crate::states::game_over::GameOverPlugin; use crate::states::main_menu::MainMenuPlugin; use crate::world::WorldPlugin; +mod config; mod debug; mod gameplay; mod states; diff --git a/src/states.rs b/src/states.rs index 6b8ef09..ae492f4 100644 --- a/src/states.rs +++ b/src/states.rs @@ -8,8 +8,9 @@ pub enum GameState { #[default] MainMenu, InGame, - SettingsMenu, + // SettingsMenu, GameOver, + // EscMenu } pub struct GameStatePlugin; diff --git a/src/world.rs b/src/world.rs index 4951598..22111f6 100644 --- a/src/world.rs +++ b/src/world.rs @@ -1,14 +1,15 @@ use bevy::prelude::*; -use crate::{gameplay::camera::MainCamera, gameplay::player::Player, states::GameState}; +use crate::{ + config::{CHUNK_HEIGHT, MARGIN_ABOVE, MARGIN_BELLOW}, + gameplay::{camera::MainCamera, player::Player}, + states::GameState, +}; pub mod background; +pub mod grid; pub mod obstacle; -pub const CHUNK_HEIGHT: f32 = 600.0; // Height a cunk -const MARGIN_BELLOW: f32 = 400.0; // Height bellow the camera view it'll be gen -const MARGIN_ABOVE: f32 = 400.0; // Height above the camera view it'll be gen - #[derive(Component)] pub struct Chunk { pub id: i64, @@ -22,7 +23,6 @@ fn stream_world( entities: Query<(Entity, &Chunk)>, mut commands: Commands, camera: Single<&Camera, With>, - run_seed: Res, ) { let (min_id, max_id) = visible_chunk_range( player.translation.y, @@ -43,7 +43,7 @@ fn stream_world( for id in min_id..=max_id { if !existing_ids.contains(&id) { - spawn_chunk(&mut commands, id, run_seed.0); + spawn_chunk(&mut commands, id); } } } @@ -56,9 +56,8 @@ 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) { crate::world::background::spawn_background_chunk(commands, id); - // crate::obstacle::spawn_obstacle_chunk(commands, id, run_seed); } fn cleanup_chunk(mut commands: Commands, entities: Query>) { diff --git a/src/world/background.rs b/src/world/background.rs index e84a5a7..e0dfbad 100644 --- a/src/world/background.rs +++ b/src/world/background.rs @@ -1,33 +1,35 @@ use bevy::prelude::*; -use crate::world::Chunk; +use crate::{ + config::{CHUNK_ROWS, TERRAIN_COLS}, + world::{ + Chunk, + grid::{GridRect, grid_to_world}, + }, +}; // Test checkboard background const COLOR_A: Color = Color::srgb(0.0, 0.0, 0.0); const COLOR_B: Color = Color::srgb(0.6, 0.2, 0.8); -const ROWS_PER_CHUNK: i64 = 2; -const CELL_SIZE: f32 = crate::world::CHUNK_HEIGHT / ROWS_PER_CHUNK as f32; -const BOARD_WIDTH: f32 = 1920.0; -const COLS: i64 = (BOARD_WIDTH / CELL_SIZE).ceil() as i64; pub fn spawn_background_chunk(commands: &mut Commands, id: i64) { - for row in (id * ROWS_PER_CHUNK)..(id * ROWS_PER_CHUNK + ROWS_PER_CHUNK) { - for col in 0..COLS { - let color = if (row + col) % 2 == 0 { + for row in 0..CHUNK_ROWS { + for col in 0..TERRAIN_COLS { + let global_row = id * CHUNK_ROWS as i64 + row as i64; + + let color = if (global_row + col as i64) % 2 == 0 { COLOR_A } else { COLOR_B }; + let (center, size) = grid_to_world(id, GridRect::cell(col, row)); + commands.spawn(( Chunk { id }, - Sprite::from_color(color, Vec2::new(CELL_SIZE, CELL_SIZE)), - Transform::from_xyz( - (col as f32 + 0.5 - COLS as f32 / 2.0) * CELL_SIZE, - (row as f32 + 0.5) * CELL_SIZE, - 0.0, - ), + Sprite::from_color(color, size), + Transform::from_xyz(center.x, center.y, 0.0), )); } } diff --git a/src/world/grid.rs b/src/world/grid.rs new file mode 100644 index 0000000..8bef468 --- /dev/null +++ b/src/world/grid.rs @@ -0,0 +1,41 @@ +use bevy::prelude::*; + +use crate::config::{CELL_SIZE, CHUNK_ROWS, TERRAIN_COLS}; + +// Cell rectangle +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct GridRect { + pub col: i32, + pub row: i32, + pub width: i32, + pub height: i32, +} + +impl GridRect { + // One cell rectangle + pub fn cell(col: i32, row: i32) -> Self { + Self { + col, + row, + width: 1, + height: 1, + } + } +} + +// Conversion grid to world +pub fn grid_to_world(chunk_id: i64, rect: GridRect) -> (Vec2, Vec2) { + let global_row = chunk_id * CHUNK_ROWS as i64 + rect.row as i64; + + let center = Vec2::new( + (rect.col as f32 + rect.width as f32 / 2.0 - TERRAIN_COLS as f32 / 2.0) * CELL_SIZE, + (global_row as f32 + rect.height as f32 / 2.0) * CELL_SIZE, + ); + + let size = Vec2::new( + rect.width as f32 * CELL_SIZE, + rect.height as f32 * CELL_SIZE, + ); + + (center, size) +} diff --git a/src/world/obstacle.rs b/src/world/obstacle.rs index f4dfba4..06dd060 100644 --- a/src/world/obstacle.rs +++ b/src/world/obstacle.rs @@ -1,10 +1,4 @@ -use crate::gameplay::collision::Collider; use bevy::prelude::*; -use rand::RngExt; -use rand::SeedableRng; - -//TODO: add chunk logic to obstacles -// TODO: add seed logic to obstacles #[derive(Component)] pub struct Obstacle; diff --git a/src/world/rng.rs b/src/world/rng.rs new file mode 100644 index 0000000..e69de29