initial map generation

This commit is contained in:
AmadeusWM
2025-04-06 16:58:05 +02:00
parent f538c597f7
commit 8cce001672
14 changed files with 395 additions and 73 deletions

49
Cargo.lock generated
View File

@@ -149,6 +149,7 @@ dependencies = [
"bevy_egui", "bevy_egui",
"bevy_kira_audio", "bevy_kira_audio",
"bevy_rapier3d", "bevy_rapier3d",
"rand 0.9.0",
] ]
[[package]] [[package]]
@@ -978,7 +979,7 @@ dependencies = [
"derive_more", "derive_more",
"glam", "glam",
"itertools 0.13.0", "itertools 0.13.0",
"rand", "rand 0.8.5",
"rand_distr", "rand_distr",
"serde", "serde",
"smallvec", "smallvec",
@@ -2543,7 +2544,7 @@ checksum = "8babf46d4c1c9d92deac9f7be466f76dfc4482b6452fc5024b5e8daf6ffeb3ee"
dependencies = [ dependencies = [
"bytemuck", "bytemuck",
"mint", "mint",
"rand", "rand 0.8.5",
"serde", "serde",
] ]
@@ -4263,8 +4264,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [ dependencies = [
"libc", "libc",
"rand_chacha", "rand_chacha 0.3.1",
"rand_core", "rand_core 0.6.4",
]
[[package]]
name = "rand"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3779b94aeb87e8bd4e834cee3650289ee9e0d5677f976ecdb6d219e5f4f6cd94"
dependencies = [
"rand_chacha 0.9.0",
"rand_core 0.9.3",
"zerocopy 0.8.24",
] ]
[[package]] [[package]]
@@ -4274,7 +4286,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [ dependencies = [
"ppv-lite86", "ppv-lite86",
"rand_core", "rand_core 0.6.4",
]
[[package]]
name = "rand_chacha"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
dependencies = [
"ppv-lite86",
"rand_core 0.9.3",
] ]
[[package]] [[package]]
@@ -4286,6 +4308,15 @@ dependencies = [
"getrandom 0.2.15", "getrandom 0.2.15",
] ]
[[package]]
name = "rand_core"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38"
dependencies = [
"getrandom 0.3.2",
]
[[package]] [[package]]
name = "rand_distr" name = "rand_distr"
version = "0.4.3" version = "0.4.3"
@@ -4293,7 +4324,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32cb0b9bc82b0a0876c2dd994a7e7a2683d3e7390ca40e6886785ef0c7e3ee31" checksum = "32cb0b9bc82b0a0876c2dd994a7e7a2683d3e7390ca40e6886785ef0c7e3ee31"
dependencies = [ dependencies = [
"num-traits", "num-traits",
"rand", "rand 0.8.5",
] ]
[[package]] [[package]]
@@ -4358,8 +4389,8 @@ dependencies = [
"once_cell", "once_cell",
"paste", "paste",
"profiling", "profiling",
"rand", "rand 0.8.5",
"rand_chacha", "rand_chacha 0.3.1",
"simd_helpers", "simd_helpers",
"system-deps", "system-deps",
"thiserror 1.0.69", "thiserror 1.0.69",
@@ -5427,7 +5458,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3758f5e68192bb96cc8f9b7e2c2cfdabb435499a28499a42f8f984092adad4b" checksum = "b3758f5e68192bb96cc8f9b7e2c2cfdabb435499a28499a42f8f984092adad4b"
dependencies = [ dependencies = [
"getrandom 0.2.15", "getrandom 0.2.15",
"rand", "rand 0.8.5",
"serde", "serde",
] ]

View File

@@ -9,3 +9,4 @@ bevy_asset_loader = { version ="0.22.0", features = ["standard_dynamic_assets"]
bevy_egui = "0.33.0" bevy_egui = "0.33.0"
bevy_kira_audio = "0.22.0" bevy_kira_audio = "0.22.0"
bevy_rapier3d = "0.29.0" bevy_rapier3d = "0.29.0"
rand = "0.9.0"

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View File

@@ -1,5 +1,8 @@
use std::collections::HashMap;
use bevy::prelude::*; use bevy::prelude::*;
use bevy_rapier3d::prelude::{Collider, RigidBody}; use bevy_rapier3d::prelude::{Collider, RigidBody};
use rand::Rng;
use crate::{asset_loading::GltfAssets, GameState}; use crate::{asset_loading::GltfAssets, GameState};
@@ -17,22 +20,25 @@ fn spawn_level(
let shapes = mesh_names.map(|mesh_name| { let shapes = mesh_names.map(|mesh_name| {
let collider: Vec<(Collider, Transform)> = match mesh_name { let collider: Vec<(Collider, Transform)> = match mesh_name {
"corner_inside" => vec![ "corner_inside" => vec![
(Collider::cuboid(1.0, 0.1, 1.0), Transform::from_xyz(0.0, 0.0, 0.0)) (Collider::cuboid(0.2, 1.0, 0.2), Transform::from_xyz(1.0, 1.0, 1.0).with_rotation(Quat::from_rotation_y(45.0_f32.to_radians())))
], ],
"corner_outside" => vec![ "corner_outside" => vec![
(Collider::cuboid(1.0, 0.1, 1.0), Transform::from_xyz(0.0, 0.0, 0.0)) (Collider::cuboid(1.0, 1.0, 0.1), Transform::from_xyz(0.0, 1.0, -1.0)),
(Collider::cuboid(1.0, 1.0, 0.1), Transform::from_xyz(-1.0, 1.0, 0.0)
.with_rotation(Quat::from_rotation_y(90.0_f32.to_radians()))),
], ],
"wall" => vec![ "wall" => vec![
(Collider::cuboid(1.0, 1.0, 0.1), Transform::from_xyz(0.0, 0.5, -1.0)) (Collider::cuboid(1.0, 1.0, 0.1), Transform::from_xyz(0.0, 1.0, -1.0))
], ],
"door" => vec![ "door" => vec![
(Collider::cuboid(1.0, 0.1, 1.0), Transform::from_xyz(0.0, 0.0, 0.0)) (Collider::cuboid(1.0, 1.0, 0.1), Transform::from_xyz(0.0, 1.0, -1.0))
], ],
"round_door" => vec![ "round_door" => vec![
(Collider::cuboid(1.0, 0.1, 1.0), Transform::from_xyz(0.0, 0.0, 0.0)) (Collider::cuboid(1.0, 1.0, 0.1), Transform::from_xyz(0.0, 1.0, -1.0))
], ],
"round_hole" => vec![ "round_hole" => vec![
(Collider::cuboid(1.0, 0.1, 1.0), Transform::from_xyz(0.0, 0.0, 0.0)) (Collider::cuboid(0.2, 1.0, 0.2), Transform::from_xyz(1.0, 1.0, -1.0).with_rotation(Quat::from_rotation_y(45.0_f32.to_radians()))),
(Collider::cuboid(0.2, 1.0, 0.2), Transform::from_xyz(-1.0, 1.0, -1.0).with_rotation(Quat::from_rotation_y(45.0_f32.to_radians())))
], ],
_ => vec![ _ => vec![
(Collider::cuboid(1.0, 0.1, 1.0), Transform::from_xyz(0.0, 0.0, 0.0)) (Collider::cuboid(1.0, 0.1, 1.0), Transform::from_xyz(0.0, 0.0, 0.0))
@@ -52,35 +58,8 @@ fn spawn_level(
door, door,
round_door, round_door,
round_hole round_hole
] = shapes; ] = shapes.clone();
for i in 0..30 {
commands.spawn((
wall.0.clone(),
Transform::from_xyz(i as f32 * 2.0, 0.0, 0.0),
)).with_children(|parent| {
for ele in wall.1.clone() {
parent.spawn((
RigidBody::Fixed,
ele.0,
ele.1,
));
}
});
commands.spawn((
wall.0.clone(),
Transform::from_xyz(i as f32 * 2.0, 0.0, 0.0)
.with_rotation(Quat::from_rotation_y(std::f32::consts::PI)),
)).with_children(|parent| {
for ele in wall.1.clone() {
parent.spawn((
RigidBody::Fixed,
ele.0,
ele.1,
));
}
});
}
// huge floor // huge floor
commands.spawn(( commands.spawn((
@@ -88,9 +67,323 @@ fn spawn_level(
Collider::cuboid(1000.0, 0.1, 1000.0), Collider::cuboid(1000.0, 0.1, 1000.0),
Transform::from_xyz(-500.0, 0.0, -500.0), Transform::from_xyz(-500.0, 0.0, -500.0),
)); ));
commands.spawn(( // huge roof
RigidBody::Fixed, // commands.spawn((
Collider::cuboid(1000.0, 0.1, 1000.0), // RigidBody::Fixed,
Transform::from_xyz(-500.0, 3.0, -500.0), // Collider::cuboid(1000.0, 0.1, 1000.0),
)); // Transform::from_xyz(-500.0, 3.0, -500.0),
// ));
// let map = GameMap::test();
let map = GameMap::new(100);
map.print_map();
for ((x, z), node) in map.nodes.into_iter() {
let values = {
// corners are handled later, for now just a wall to test
let node = node.clone();
let pos = Transform::from_xyz(2.0*x as f32, 0.0, -2.0*z as f32);
use Side::{Connection, Closed};
match (node.north, node.east, node.south, node.west) {
// hallway horizontal
(Closed, Connection, Closed, Connection) => {
vec![
(wall.clone(), pos),
(wall.clone(), pos.with_rotation(Quat::from_rotation_y(180.0_f32.to_radians())))
]
},
// hallway vertical
(Connection, Closed, Connection, Closed) => {
vec![
(wall.clone(), pos.with_rotation(Quat::from_rotation_y(90.0_f32.to_radians()))),
(wall.clone(), pos.with_rotation(Quat::from_rotation_y(270.0_f32.to_radians())))
]
}
// dead ends
(Connection, Closed, Closed, Closed) => {
vec![
(wall.clone(), pos.with_rotation(Quat::from_rotation_y(90.0_f32.to_radians()))),
(round_door.clone(), pos.with_rotation(Quat::from_rotation_y(180.0_f32.to_radians()))),
(wall.clone(), pos.with_rotation(Quat::from_rotation_y(270.0_f32.to_radians())))
]
}
(Closed, Closed, Connection, Closed) => {
vec![
(wall.clone(), pos.with_rotation(Quat::from_rotation_y(90.0_f32.to_radians()))),
(round_door.clone(), pos.with_rotation(Quat::from_rotation_y(0.0_f32.to_radians()))),
(wall.clone(), pos.with_rotation(Quat::from_rotation_y(270.0_f32.to_radians())))
]
}
(Closed, Connection, Closed, Closed) => {
vec![
(wall.clone(), pos),
(round_door.clone(), pos.with_rotation(Quat::from_rotation_y(90.0_f32.to_radians()))),
(wall.clone(), pos.with_rotation(Quat::from_rotation_y(180.0_f32.to_radians())))
]
},
(Closed, Closed, Closed, Connection) => {
vec![
(wall.clone(), pos),
(round_door.clone(), pos.with_rotation(Quat::from_rotation_y(270.0_f32.to_radians()))),
(wall.clone(), pos.with_rotation(Quat::from_rotation_y(180.0_f32.to_radians())))
]
},
// T hallways
(Closed, Connection, Connection, Connection) => {
vec![
(wall.clone(), pos),
(round_hole.clone(), pos.with_rotation(Quat::from_rotation_y(180.0_f32.to_radians())))
]
},
(Connection, Closed, Connection, Connection) => {
vec![
(round_hole.clone(), pos.with_rotation(Quat::from_rotation_y(90.0_f32.to_radians()))),
(wall.clone(), pos.with_rotation(Quat::from_rotation_y(270.0_f32.to_radians())))
]
},
(Connection, Connection, Closed, Connection) => {
vec![
(round_hole.clone(), pos),
(wall.clone(), pos.with_rotation(Quat::from_rotation_y(180.0_f32.to_radians())))
]
},
(Connection, Connection, Connection, Closed) => {
vec![
(wall.clone(), pos.with_rotation(Quat::from_rotation_y(90.0_f32.to_radians()))),
(round_hole.clone(), pos.with_rotation(Quat::from_rotation_y(270.0_f32.to_radians())))
]
},
// fourway
(Connection, Connection, Connection, Connection) => {
vec![
(corner_inside.clone(), pos.with_rotation(Quat::from_rotation_y(0.0_f32.to_radians()))),
(corner_inside.clone(), pos.with_rotation(Quat::from_rotation_y(90.0_f32.to_radians()))),
(corner_inside.clone(), pos.with_rotation(Quat::from_rotation_y(180.0_f32.to_radians()))),
(corner_inside.clone(), pos.with_rotation(Quat::from_rotation_y(270.0_f32.to_radians())))
]
},
// corners
(Connection, Connection, Closed, Closed) => {
vec![
(corner_outside.clone(), pos.with_rotation(Quat::from_rotation_y(90.0_f32.to_radians()))),
(corner_inside.clone(), pos.with_rotation(Quat::from_rotation_y(90.0_f32.to_radians()))),
]
},
(Closed, Connection, Connection, Closed) => {
vec![
(corner_outside.clone(), pos.with_rotation(Quat::from_rotation_y(0.0_f32.to_radians()))),
(corner_inside.clone(), pos.with_rotation(Quat::from_rotation_y(0.0_f32.to_radians()))),
]
},
(Closed, Closed, Connection, Connection) => {
vec![
(corner_outside.clone(), pos.with_rotation(Quat::from_rotation_y(270.0_f32.to_radians()))),
(corner_inside.clone(), pos.with_rotation(Quat::from_rotation_y(270.0_f32.to_radians()))),
]
},
(Connection, Closed, Closed, Connection) => {
vec![
(corner_outside.clone(), pos.with_rotation(Quat::from_rotation_y(180.0_f32.to_radians()))),
(corner_inside.clone(), pos.with_rotation(Quat::from_rotation_y(180.0_f32.to_radians()))),
]
}
_ => vec![]
}
};
for ((scene_root, colliders), pos) in values {
commands.spawn((
RigidBody::Fixed,
scene_root,
pos,
)).with_children(|parent| {
for (collider, transform) in colliders {
parent.spawn((collider, transform));
}
});
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Side {
Empty,
Connection,
Closed,
}
#[derive(Clone, Debug)]
struct MapNode {
north: Side,
south: Side,
east: Side,
west: Side,
collapsed: bool,
}
impl MapNode {
fn new() -> Self {
MapNode {
north: Side::Empty,
south: Side::Empty,
east: Side::Empty,
west: Side::Empty,
collapsed: false,
}
}
}
struct GameMap {
nodes: HashMap<(i32, i32), MapNode>,
max_nodes: i32
}
impl GameMap {
fn new(max_nodes: i32) -> Self {
let mut nodes = HashMap::new();
let mut start_node = MapNode::new();
start_node.north = Side::Connection;
start_node.south = Side::Connection;
start_node.east = Side::Connection;
start_node.west = Side::Connection;
nodes.insert((0, 0), start_node);
let mut m = GameMap { nodes, max_nodes };
m.generate_map();
m
}
fn test() -> Self {
let mut nodes = HashMap::new();
let mut wall_node0 = MapNode::new();
wall_node0.north = Side::Connection;
wall_node0.south = Side::Closed;
wall_node0.east = Side::Closed;
wall_node0.west = Side::Closed;
nodes.insert((0, -1), wall_node0);
let mut wall_node1 = MapNode::new();
wall_node1.north = Side::Connection;
wall_node1.south = Side::Connection;
wall_node1.east = Side::Connection;
wall_node1.west = Side::Closed;
nodes.insert((0, 0), wall_node1);
let mut wall_node2 = MapNode::new();
wall_node2.north = Side::Connection;
wall_node2.south = Side::Connection;
wall_node2.east = Side::Closed;
wall_node2.west = Side::Connection;
nodes.insert((0, 1), wall_node2);
let mut corner_node = MapNode::new();
corner_node.south = Side::Connection;
corner_node.east = Side::Connection;
corner_node.north = Side::Closed;
corner_node.west = Side::Closed;
nodes.insert((0, 2), corner_node);
let mut wall_node3 = MapNode::new();
wall_node3.west = Side::Connection;
wall_node3.east = Side::Connection;
wall_node3.north = Side::Closed;
wall_node3.south = Side::Connection;
nodes.insert((1, 2), wall_node3);
let mut wall_node4 = MapNode::new();
wall_node4.west = Side::Connection;
wall_node4.east = Side::Connection;
wall_node4.north = Side::Connection;
wall_node4.south = Side::Closed;
nodes.insert((2, 2), wall_node4);
let mut wall_node5 = MapNode::new();
wall_node5.west = Side::Connection;
wall_node5.east = Side::Closed;
wall_node5.north = Side::Closed;
wall_node5.south = Side::Connection;
nodes.insert((3, 2), wall_node5);
GameMap { nodes, max_nodes: 7 }
}
fn generate_map(&mut self) {
self.create_node(&(0, 1)); // North
// self.create_node(&(0, -1)); // South
// self.create_node(&(1, 0)); // East
// self.create_node(&(-1, 0)); // West
}
fn choose_side(&self) -> Side {
if self.nodes.len() > self.max_nodes as usize {
Side::Closed
} else {
if rand::rng().random_bool(0.5) { Side::Connection } else { Side::Closed }
}
}
fn create_node(&mut self, idx: &(i32, i32)) {
let mut nodes_to_process = vec![*idx];
let mut nodes_created = 0;
while let Some(current_idx) = nodes_to_process.pop() {
if self.nodes.contains_key(&current_idx) {
continue;
}
let (x, y) = current_idx;
let mut new_node = MapNode::new();
let north = self.nodes.get(&(x, y + 1));
let south = self.nodes.get(&(x, y - 1));
let east = self.nodes.get(&(x + 1, y));
let west = self.nodes.get(&(x - 1, y));
new_node.north = north.map_or_else(|| self.choose_side(), |n| n.south);
new_node.south = south.map_or_else(|| self.choose_side(), |s| s.north);
new_node.east = east.map_or_else(|| self.choose_side(), |e| e.west);
new_node.west = west.map_or_else(|| self.choose_side(), |w| w.east);
if new_node.north == Side::Connection && !self.nodes.contains_key(&(x, y + 1)) {
nodes_to_process.push((x, y + 1));
}
if new_node.south == Side::Connection && !self.nodes.contains_key(&(x, y - 1)) {
nodes_to_process.push((x, y - 1));
}
if new_node.east == Side::Connection && !self.nodes.contains_key(&(x + 1, y)) {
nodes_to_process.push((x + 1, y));
}
if new_node.west == Side::Connection && !self.nodes.contains_key(&(x - 1, y)) {
nodes_to_process.push((x - 1, y));
}
self.nodes.insert(current_idx, new_node);
nodes_created += 1;
}
}
fn print_map(&self) {
let mut min_x = 0;
let mut max_x = 0;
let mut min_y = 0;
let mut max_y = 0;
for &(x, y) in self.nodes.keys() {
min_x = min_x.min(x);
max_x = max_x.max(x);
min_y = min_y.min(y);
max_y = max_y.max(y);
}
for y in (min_y..=max_y).rev() {
for x in min_x..=max_x {
match self.nodes.get(&(x, y)) {
Some(node) => print!("[+]"),
None => print!(" "),
}
}
println!();
}
}
} }

View File

@@ -138,8 +138,6 @@ pub fn init_player(
}, },
RenderLayers::layer(STATIC_LAYER), RenderLayers::layer(STATIC_LAYER),
)); ));
// pitslamp sprite
let window = window.single(); let window = window.single();
let transform = flashlight_base_transform(window.width(), window.height()); let transform = flashlight_base_transform(window.width(), window.height());
parent.spawn(( parent.spawn((
@@ -184,8 +182,8 @@ fn on_resize_system(
fn flashlight_base_transform(window_width: f32, window_height: f32) -> BaseTransform { fn flashlight_base_transform(window_width: f32, window_height: f32) -> BaseTransform {
let window_size = Vec2::new(window_width, window_height); let window_size = Vec2::new(window_width, window_height);
let sprite_size = Vec2::new(101.0, 101.0); let sprite_size = Vec2::new(404.0, 404.0);
let scale = window_width / 600.0; let scale = window_width / 2400.0;
let world_size = sprite_size * scale; let world_size = sprite_size * scale;
let xoffset = window_size.x / 4.0 - 40.0; let xoffset = window_size.x / 4.0 - 40.0;
let yoffset = 15.0; let yoffset = 15.0;
@@ -369,7 +367,6 @@ pub fn apply_head_bob(
} }
} }
} }
pub fn handle_flashlight( pub fn handle_flashlight(
player_query: Query<&PlayerAction, With<Player>>, player_query: Query<&PlayerAction, With<Player>>,
mut flashlight_query: Query<&mut SpotLight, With<Flashlight>>, mut flashlight_query: Query<&mut SpotLight, With<Flashlight>>,