Compare commits
3
Commits
bf9ef34bf8
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
afdea8d093 | ||
|
|
f042ee50a3 | ||
|
|
effd08e05d |
@@ -1,2 +1 @@
|
|||||||
/target
|
/target
|
||||||
/docs
|
|
||||||
|
|||||||
Generated
+10
@@ -2446,6 +2446,7 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"bevy",
|
"bevy",
|
||||||
"bevy_egui",
|
"bevy_egui",
|
||||||
|
"fastnoise-lite",
|
||||||
"rand",
|
"rand",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -2636,6 +2637,15 @@ dependencies = [
|
|||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fastnoise-lite"
|
||||||
|
version = "1.1.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3e5f3c3cc9081e5d0e18bcd50e80cd33cba47fc22f88a9da9c33ecd1c87ea5c0"
|
||||||
|
dependencies = [
|
||||||
|
"num-traits",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fastrand"
|
name = "fastrand"
|
||||||
version = "2.5.0"
|
version = "2.5.0"
|
||||||
|
|||||||
@@ -6,4 +6,5 @@ edition = "2024"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
bevy = "0.19.1"
|
bevy = "0.19.1"
|
||||||
bevy_egui = "0.42.0"
|
bevy_egui = "0.42.0"
|
||||||
|
fastnoise-lite = "1.1.1"
|
||||||
rand = "0.10.2"
|
rand = "0.10.2"
|
||||||
|
|||||||
@@ -30,9 +30,3 @@ pub const CAMERAE_OFFSET: f32 = 280.0;
|
|||||||
pub const HORIZONTAL_ACCEL: f32 = 2600.0;
|
pub const HORIZONTAL_ACCEL: f32 = 2600.0;
|
||||||
pub const HORIZONTAL_FRICTION: f32 = 1800.0;
|
pub const HORIZONTAL_FRICTION: f32 = 1800.0;
|
||||||
pub const VERTICAL_RATE: f32 = 4000.0;
|
pub const VERTICAL_RATE: f32 = 4000.0;
|
||||||
|
|
||||||
// Validator
|
|
||||||
pub const INERTIA_MARGIN: f32 = 4.0;
|
|
||||||
pub const DESIGN_LATERAL_SPEED_RATIO: f32 = 0.6;
|
|
||||||
pub const DESIGN_SLOPE_COLS_PER_ROW: f32 =
|
|
||||||
HORIZONTAL_SPEED * DESIGN_LATERAL_SPEED_RATIO / FALL_SPEED;
|
|
||||||
|
|||||||
+32
-2
@@ -8,12 +8,13 @@ use crate::{
|
|||||||
config::{SCREEN_HEIGHT, SCREEN_WIDTH},
|
config::{SCREEN_HEIGHT, SCREEN_WIDTH},
|
||||||
gameplay::{
|
gameplay::{
|
||||||
camera::MainCamera,
|
camera::MainCamera,
|
||||||
|
collision::ColliderDebug,
|
||||||
player::{FallStarted, MoveSettings, Player, Velocity, spawn_player},
|
player::{FallStarted, MoveSettings, Player, Velocity, spawn_player},
|
||||||
score::Depth,
|
score::Depth,
|
||||||
},
|
},
|
||||||
screen::DisplayImage,
|
screen::DisplayImage,
|
||||||
states::GameState,
|
states::GameState,
|
||||||
world::{Chunk, RunSeed},
|
world::{Chunk, RunSeed, terrain::TerrainSettings},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
@@ -183,6 +184,8 @@ fn debug_panel(
|
|||||||
mut fall_started: ResMut<FallStarted>,
|
mut fall_started: ResMut<FallStarted>,
|
||||||
mut depth: ResMut<Depth>,
|
mut depth: ResMut<Depth>,
|
||||||
display_image: Single<&mut Visibility, With<DisplayImage>>,
|
display_image: Single<&mut Visibility, With<DisplayImage>>,
|
||||||
|
mut terrain_settings: ResMut<TerrainSettings>,
|
||||||
|
mut collider_debug: ResMut<ColliderDebug>,
|
||||||
) {
|
) {
|
||||||
egui::Window::new("Debug").show(contexts.ctx_mut().expect("gui context missing"), |ui| {
|
egui::Window::new("Debug").show(contexts.ctx_mut().expect("gui context missing"), |ui| {
|
||||||
// Fps
|
// Fps
|
||||||
@@ -245,7 +248,34 @@ fn debug_panel(
|
|||||||
|
|
||||||
spawn_player(commands);
|
spawn_player(commands);
|
||||||
}
|
}
|
||||||
// TODO: Add colidbox On/Off button
|
|
||||||
|
ui.separator();
|
||||||
|
|
||||||
|
ui.label("Terrain");
|
||||||
|
if ui.button("Reset terrain seed").clicked() {
|
||||||
|
run_seed.0 = rand::random::<u64>();
|
||||||
|
}
|
||||||
|
let mut settings = *terrain_settings;
|
||||||
|
let changed = ui
|
||||||
|
.add(egui::Slider::new(&mut settings.threshold, -1.0..=1.0).text("Threshold"))
|
||||||
|
.changed()
|
||||||
|
| ui.add(egui::Slider::new(&mut settings.frequency, 0.0..=0.3).text("Frequency"))
|
||||||
|
.changed()
|
||||||
|
| ui.add(egui::Slider::new(&mut settings.octaves, 1..=10).text("Octaves"))
|
||||||
|
.changed();
|
||||||
|
|
||||||
|
if changed {
|
||||||
|
*terrain_settings = settings; // ne marque changed qu'au vrai changement
|
||||||
|
}
|
||||||
|
// ui.add(egui::Slider::new(&mut terrain_settings.threshold, -1.0..=1.0).text("Treshold"));
|
||||||
|
// ui.add(egui::Slider::new(&mut terrain_settings.frequency, 0.0..=0.3).text("Frequency"));
|
||||||
|
// ui.add(egui::Slider::new(&mut terrain_settings.octaves, 1..=10).text("Octaves"));
|
||||||
|
//
|
||||||
|
ui.separator();
|
||||||
|
let state = if collider_debug.0 { "ON" } else { "OFF" };
|
||||||
|
if ui.button(format!("Kill blocks: {state}")).clicked() {
|
||||||
|
collider_debug.0 = !collider_debug.0;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,16 @@ pub struct Collider {
|
|||||||
pub size: Vec2,
|
pub size: Vec2,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Collider debug
|
||||||
|
#[derive(Resource)]
|
||||||
|
pub struct ColliderDebug(pub bool);
|
||||||
|
|
||||||
|
impl Default for ColliderDebug {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Check collision with Player
|
// Check collision with Player
|
||||||
pub fn check_collision_with_player(
|
pub fn check_collision_with_player(
|
||||||
player: Single<(&Transform, &Collider), With<Player>>,
|
player: Single<(&Transform, &Collider), With<Player>>,
|
||||||
@@ -46,11 +56,13 @@ pub struct CollisionPlugin;
|
|||||||
|
|
||||||
impl Plugin for CollisionPlugin {
|
impl Plugin for CollisionPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_systems(
|
app.init_resource::<ColliderDebug>() // Debug
|
||||||
Update,
|
.add_systems(
|
||||||
check_collision_with_player
|
Update,
|
||||||
.run_if(in_state(GameState::InGame))
|
check_collision_with_player
|
||||||
.after(move_player),
|
.run_if(in_state(GameState::InGame))
|
||||||
);
|
.run_if(|collision: Res<ColliderDebug>| collision.0) // Debug
|
||||||
|
.after(move_player),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ use crate::states::GameStatePlugin;
|
|||||||
use crate::states::game_over::GameOverPlugin;
|
use crate::states::game_over::GameOverPlugin;
|
||||||
use crate::states::main_menu::MainMenuPlugin;
|
use crate::states::main_menu::MainMenuPlugin;
|
||||||
use crate::world::WorldPlugin;
|
use crate::world::WorldPlugin;
|
||||||
|
use crate::world::terrain::TerrainPlugin;
|
||||||
|
|
||||||
mod config;
|
mod config;
|
||||||
mod debug;
|
mod debug;
|
||||||
@@ -58,5 +59,6 @@ fn main() {
|
|||||||
})
|
})
|
||||||
.add_plugins(EguiPlugin::default()) // Debug UI
|
.add_plugins(EguiPlugin::default()) // Debug UI
|
||||||
.add_plugins(DebugPlugin) // Debug
|
.add_plugins(DebugPlugin) // Debug
|
||||||
|
.add_plugins(TerrainPlugin) // Terrain debug
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|||||||
+36
-7
@@ -4,14 +4,13 @@ use crate::{
|
|||||||
config::{CAMERAE_OFFSET, CHUNK_HEIGHT, MARGIN_ABOVE, MARGIN_BELLOW, SCREEN_HEIGHT},
|
config::{CAMERAE_OFFSET, CHUNK_HEIGHT, MARGIN_ABOVE, MARGIN_BELLOW, SCREEN_HEIGHT},
|
||||||
gameplay::player::Player,
|
gameplay::player::Player,
|
||||||
states::GameState,
|
states::GameState,
|
||||||
|
world::terrain::TerrainSettings,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub mod background;
|
pub mod background;
|
||||||
pub mod doors;
|
|
||||||
pub mod generator;
|
|
||||||
pub mod grid;
|
pub mod grid;
|
||||||
pub mod obstacle;
|
pub mod obstacle;
|
||||||
pub mod validator;
|
pub mod terrain;
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Chunk {
|
pub struct Chunk {
|
||||||
@@ -26,6 +25,7 @@ fn stream_world(
|
|||||||
entities: Query<(Entity, &Chunk)>,
|
entities: Query<(Entity, &Chunk)>,
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
run_seed: Res<RunSeed>,
|
run_seed: Res<RunSeed>,
|
||||||
|
terrain_settings: Res<TerrainSettings>, // Debug
|
||||||
) {
|
) {
|
||||||
let (min_id, max_id) = visible_chunk_range(player.translation.y);
|
let (min_id, max_id) = visible_chunk_range(player.translation.y);
|
||||||
|
|
||||||
@@ -40,7 +40,12 @@ fn stream_world(
|
|||||||
|
|
||||||
for id in min_id..=max_id {
|
for id in min_id..=max_id {
|
||||||
if !existing_ids.contains(&id) {
|
if !existing_ids.contains(&id) {
|
||||||
spawn_chunk(&mut commands, id, run_seed.0);
|
spawn_chunk(
|
||||||
|
&mut commands,
|
||||||
|
id,
|
||||||
|
run_seed.0,
|
||||||
|
*terrain_settings, // Debug
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -55,9 +60,28 @@ fn visible_chunk_range(player_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,
|
||||||
|
terrain_settings: TerrainSettings, // Debug
|
||||||
|
) {
|
||||||
crate::world::background::spawn_background_chunk(commands, id);
|
crate::world::background::spawn_background_chunk(commands, id);
|
||||||
crate::world::obstacle::spawn_obstacle_chunk(commands, id, run_seed);
|
crate::world::obstacle::spawn_obstacle_chunk(commands, id, run_seed, terrain_settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Debug
|
||||||
|
fn rebuild_terrain_on_settings_change(
|
||||||
|
mut commands: Commands,
|
||||||
|
terrain_settings: Res<TerrainSettings>,
|
||||||
|
run_seed: Res<RunSeed>, // Debug
|
||||||
|
entities: Query<Entity, With<Chunk>>,
|
||||||
|
) {
|
||||||
|
if terrain_settings.is_changed() || run_seed.is_changed() {
|
||||||
|
for entity in entities {
|
||||||
|
commands.entity(entity).despawn();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cleanup_chunk(mut commands: Commands, entities: Query<Entity, With<Chunk>>) {
|
fn cleanup_chunk(mut commands: Commands, entities: Query<Entity, With<Chunk>>) {
|
||||||
@@ -71,7 +95,12 @@ 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.run_if(in_state(GameState::InGame)))
|
.add_systems(
|
||||||
|
Update,
|
||||||
|
(rebuild_terrain_on_settings_change, stream_world)
|
||||||
|
.chain()
|
||||||
|
.run_if(in_state(GameState::InGame)),
|
||||||
|
)
|
||||||
.add_systems(OnExit(GameState::InGame), cleanup_chunk);
|
.add_systems(OnExit(GameState::InGame), cleanup_chunk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,49 +0,0 @@
|
|||||||
use rand::{RngExt, SeedableRng, rngs::StdRng};
|
|
||||||
|
|
||||||
use crate::config::TERRAIN_COLS;
|
|
||||||
|
|
||||||
const SALT_DOOR: u64 = 1;
|
|
||||||
|
|
||||||
const DOOR_HALF_WIDTH: i32 = 4;
|
|
||||||
const DOOR_MIN_WIDTH: i32 = 2;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
||||||
pub struct Door {
|
|
||||||
pub min_col: i32,
|
|
||||||
pub max_col: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Door {
|
|
||||||
pub fn width(&self) -> i32 {
|
|
||||||
self.max_col - self.min_col + 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn door(seed: u64, k: i64) -> Option<Door> {
|
|
||||||
if k < 0 {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Chunk 0
|
|
||||||
if k == 0 {
|
|
||||||
let center = TERRAIN_COLS / 2;
|
|
||||||
return Some(Door {
|
|
||||||
min_col: center - DOOR_HALF_WIDTH,
|
|
||||||
max_col: center + DOOR_HALF_WIDTH - 1,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut rng = StdRng::seed_from_u64(seed ^ (k as u64).wrapping_mul(SALT_DOOR));
|
|
||||||
|
|
||||||
// Random width
|
|
||||||
let width = DOOR_MIN_WIDTH + rng.random_range(0..(2 * DOOR_HALF_WIDTH - DOOR_MIN_WIDTH + 1));
|
|
||||||
|
|
||||||
// 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 {
|
|
||||||
min_col,
|
|
||||||
max_col: min_col + width - 1,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
+35
-3
@@ -1,13 +1,45 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
// const OBSTACLE_COLOR: Color = Color::srgb(0.1, 0.7, 0.3);
|
use crate::{
|
||||||
const _DEBUG_OBSTACLE_COLOR: Color = Color::srgba(0.0, 1.0, 0.0, 1.0);
|
config::{CHUNK_ROWS, TERRAIN_COLS},
|
||||||
|
gameplay::collision::Collider,
|
||||||
|
world::{
|
||||||
|
Chunk,
|
||||||
|
grid::{GridRect, grid_to_world},
|
||||||
|
terrain::{Terrain, TerrainSettings},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const OBSTACLE_COLOR: Color = Color::srgb(0.1, 0.7, 0.3);
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Obstacle;
|
pub struct Obstacle;
|
||||||
|
|
||||||
pub fn spawn_obstacle_chunk(commands: &mut Commands, id: i64, run_seed: u64) {
|
pub fn spawn_obstacle_chunk(
|
||||||
|
commands: &mut Commands,
|
||||||
|
id: i64,
|
||||||
|
run_seed: u64,
|
||||||
|
terrain_settings: TerrainSettings, // Debug
|
||||||
|
) {
|
||||||
|
// No blocks in chunk 0
|
||||||
if id >= -1 {
|
if id >= -1 {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let terrain = Terrain::new(run_seed, terrain_settings);
|
||||||
|
for row in 0..CHUNK_ROWS {
|
||||||
|
let global_row = id * CHUNK_ROWS as i64 + row as i64;
|
||||||
|
for col in 0..TERRAIN_COLS {
|
||||||
|
if terrain.solid_at(col as i64, global_row, terrain_settings) {
|
||||||
|
let (center, size) = grid_to_world(id, GridRect::cell(col, row));
|
||||||
|
commands.spawn((
|
||||||
|
Chunk { id },
|
||||||
|
Obstacle,
|
||||||
|
Collider { size },
|
||||||
|
Sprite::from_color(OBSTACLE_COLOR, size),
|
||||||
|
Transform::from_xyz(center.x, center.y, 1.0),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
use bevy::{app::Plugin, ecs::resource::Resource};
|
||||||
|
use fastnoise_lite::{FastNoiseLite, FractalType, NoiseType};
|
||||||
|
|
||||||
|
const THRESHOLD: f32 = 0.0;
|
||||||
|
const FREQUENCY: f32 = 0.1;
|
||||||
|
const OCTAVES: i32 = 3;
|
||||||
|
|
||||||
|
pub struct Terrain {
|
||||||
|
noise: FastNoiseLite,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Resource, Clone, Copy)]
|
||||||
|
pub struct TerrainSettings {
|
||||||
|
pub threshold: f32,
|
||||||
|
pub frequency: f32,
|
||||||
|
pub octaves: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for TerrainSettings {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
threshold: THRESHOLD,
|
||||||
|
frequency: FREQUENCY,
|
||||||
|
octaves: OCTAVES,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Terrain {
|
||||||
|
pub fn new(seed: u64, terrain_settings: TerrainSettings) -> Self {
|
||||||
|
let mut noise = FastNoiseLite::with_seed(seed as i32);
|
||||||
|
noise.set_noise_type(Some(NoiseType::OpenSimplex2));
|
||||||
|
noise.set_frequency(Some(terrain_settings.frequency));
|
||||||
|
noise.set_fractal_type(Some(FractalType::FBm));
|
||||||
|
noise.set_fractal_octaves(Some(terrain_settings.octaves));
|
||||||
|
Self { noise }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn solid_at(&self, col: i64, row: i64, terrain_settings: TerrainSettings) -> bool {
|
||||||
|
self.noise.get_noise_2d(col as f32, row as f32) > terrain_settings.threshold
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct TerrainPlugin;
|
||||||
|
|
||||||
|
impl Plugin for TerrainPlugin {
|
||||||
|
fn build(&self, app: &mut bevy::app::App) {
|
||||||
|
app.init_resource::<TerrainSettings>();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,104 +0,0 @@
|
|||||||
use crate::config::{
|
|
||||||
CELL_SIZE, CHUNK_ROWS, DESIGN_SLOPE_COLS_PER_ROW, HALF_TERRAIN_WIDTH, INERTIA_MARGIN,
|
|
||||||
PLAYER_HITBOX, TERRAIN_COLS,
|
|
||||||
};
|
|
||||||
use crate::world::{doors::Door, grid::GridRect};
|
|
||||||
|
|
||||||
const INFLATE_X: f32 = PLAYER_HITBOX.x / 2.0 + INERTIA_MARGIN;
|
|
||||||
const INFLATE_Y: f32 = PLAYER_HITBOX.y / 2.0 + INERTIA_MARGIN;
|
|
||||||
|
|
||||||
const EXPAND_PER_ROW: f32 = DESIGN_SLOPE_COLS_PER_ROW * CELL_SIZE;
|
|
||||||
|
|
||||||
fn door_px(door: Door) -> (f32, f32) {
|
|
||||||
let offset = TERRAIN_COLS as f32 / 2.0;
|
|
||||||
(
|
|
||||||
(door.min_col as f32 - offset) * CELL_SIZE,
|
|
||||||
(door.max_col as f32 + 1.0 - offset) * CELL_SIZE,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn free_intervals(rects: &[GridRect], row: i32) -> Vec<(f32, f32)> {
|
|
||||||
let row_y0 = row as f32 * CELL_SIZE;
|
|
||||||
let row_y1 = row_y0 + CELL_SIZE;
|
|
||||||
let mut blocked: Vec<(f32, f32)> = Vec::new();
|
|
||||||
for r in rects {
|
|
||||||
let y0 = r.row as f32 * CELL_SIZE - INFLATE_Y;
|
|
||||||
let y1 = (r.row + r.height) as f32 * CELL_SIZE + INFLATE_Y;
|
|
||||||
if y1 <= row_y0 || y0 >= row_y1 {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let offset = TERRAIN_COLS as f32 / 2.0;
|
|
||||||
let x0 = (r.col as f32 - offset) * CELL_SIZE - INFLATE_X;
|
|
||||||
let x1 = ((r.col + r.width) as f32 - offset) * CELL_SIZE + INFLATE_X;
|
|
||||||
blocked.push((x0, x1));
|
|
||||||
}
|
|
||||||
blocked = merge(blocked);
|
|
||||||
|
|
||||||
let mut free: Vec<(f32, f32)> = vec![(
|
|
||||||
-HALF_TERRAIN_WIDTH + INFLATE_X,
|
|
||||||
HALF_TERRAIN_WIDTH - INFLATE_X,
|
|
||||||
)];
|
|
||||||
for (a, b) in &blocked {
|
|
||||||
free = subtract_all(&free, *a, *b);
|
|
||||||
}
|
|
||||||
free
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn validate(rects: &[GridRect], entry: Door, exit: Door) -> bool {
|
|
||||||
let mut safe: Vec<(f32, f32)> = vec![door_px(exit)];
|
|
||||||
|
|
||||||
for row in (0..CHUNK_ROWS).rev() {
|
|
||||||
let free = free_intervals(rects, row);
|
|
||||||
|
|
||||||
let mut next: Vec<(f32, f32)> = Vec::new();
|
|
||||||
for f in &free {
|
|
||||||
for (a, b) in &safe {
|
|
||||||
if let Some((i0, i1)) = intersect((a - EXPAND_PER_ROW, b + EXPAND_PER_ROW), *f) {
|
|
||||||
next.push((i0, i1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
safe = merge(next);
|
|
||||||
if safe.is_empty() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let e = door_px(entry);
|
|
||||||
safe.iter().any(|(a, b)| *a <= e.0 && e.1 <= *b)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn intersect((a0, a1): (f32, f32), (b0, b1): (f32, f32)) -> Option<(f32, f32)> {
|
|
||||||
let lo = a0.max(b0);
|
|
||||||
let hi = a1.min(b1);
|
|
||||||
(lo < hi).then_some((lo, hi))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn merge(mut list: Vec<(f32, f32)>) -> Vec<(f32, f32)> {
|
|
||||||
list.sort_by(|a, b| a.0.total_cmp(&b.0));
|
|
||||||
let mut out: Vec<(f32, f32)> = Vec::new();
|
|
||||||
for (a, b) in list {
|
|
||||||
match out.last_mut() {
|
|
||||||
Some(last) if a <= last.1 => last.1 = last.1.max(b),
|
|
||||||
_ => out.push((a, b)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
out
|
|
||||||
}
|
|
||||||
|
|
||||||
fn subtract_all(free: &[(f32, f32)], a: f32, b: f32) -> Vec<(f32, f32)> {
|
|
||||||
let mut out = Vec::new();
|
|
||||||
for (x0, x1) in free {
|
|
||||||
if b <= *x0 || a >= *x1 {
|
|
||||||
out.push((*x0, *x1));
|
|
||||||
} else {
|
|
||||||
if a > *x0 {
|
|
||||||
out.push((*x0, a));
|
|
||||||
}
|
|
||||||
if b < *x1 {
|
|
||||||
out.push((b, *x1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
out
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user