grid logic
This commit is contained in:
@@ -1 +1,2 @@
|
||||
/target
|
||||
/docs
|
||||
|
||||
@@ -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);
|
||||
+5
-10
@@ -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<Player>>,
|
||||
camera: Single<&Camera, With<MainCamera>>,
|
||||
) {
|
||||
let viewport = camera
|
||||
.logical_viewport_size()
|
||||
.unwrap_or(Vec2::new(1920.0, 1080.0));
|
||||
fn constrain_player(mut player: Single<(&Collider, &mut Transform), With<Player>>) {
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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),
|
||||
));
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
+2
-1
@@ -8,8 +8,9 @@ pub enum GameState {
|
||||
#[default]
|
||||
MainMenu,
|
||||
InGame,
|
||||
SettingsMenu,
|
||||
// SettingsMenu,
|
||||
GameOver,
|
||||
// EscMenu
|
||||
}
|
||||
|
||||
pub struct GameStatePlugin;
|
||||
|
||||
+8
-9
@@ -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<MainCamera>>,
|
||||
run_seed: Res<RunSeed>,
|
||||
) {
|
||||
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<Entity, With<Chunk>>) {
|
||||
|
||||
+16
-14
@@ -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),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user