fix broken maps
This commit is contained in:
@@ -76,7 +76,7 @@ fn spawn_level(
|
|||||||
// Transform::from_xyz(-500.0, 3.0, -500.0),
|
// Transform::from_xyz(-500.0, 3.0, -500.0),
|
||||||
// ));
|
// ));
|
||||||
// let map = GameMap::test();
|
// let map = GameMap::test();
|
||||||
let map = GameMap::new(10);
|
let map = GameMap::new((0, 0), 5);
|
||||||
map.print_map();
|
map.print_map();
|
||||||
|
|
||||||
for ((x, z), node) in map.nodes.into_iter() {
|
for ((x, z), node) in map.nodes.into_iter() {
|
||||||
@@ -207,11 +207,9 @@ fn spawn_level(
|
|||||||
}
|
}
|
||||||
let (x,z) = map.end_node;
|
let (x,z) = map.end_node;
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
Mesh3d(meshes.add(Cuboid::new(3.0, 3.0, 3.0))),
|
Mesh3d(meshes.add(Cuboid::new(1.0, 3.0, 1.0))),
|
||||||
MeshMaterial3d(materials.add(Color::srgb_u8(255, 0, 0))),
|
MeshMaterial3d(materials.add(Color::srgb_u8(255, 0, 0))),
|
||||||
Transform::from_xyz(2.0*x as f32, 0.5, -2.0*z as f32),
|
Transform::from_xyz(2.0*x as f32, 0.5, -2.0*z as f32),
|
||||||
RigidBody::Fixed,
|
|
||||||
Collider::cuboid(0.5, 0.5, 0.5),
|
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -248,15 +246,15 @@ struct GameMap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl GameMap {
|
impl GameMap {
|
||||||
fn new(grid_size: i32) -> Self {
|
fn new(initial_point: (i32, i32), grid_size: i32) -> Self {
|
||||||
let mut nodes = HashMap::new();
|
let mut nodes = HashMap::new();
|
||||||
let mut start_node = MapNode::new();
|
let mut start_node = MapNode::new();
|
||||||
start_node.north = Side::Connection;
|
start_node.north = Side::Connection;
|
||||||
start_node.south = Side::Connection;
|
start_node.south = Side::Connection;
|
||||||
start_node.east = Side::Connection;
|
start_node.east = Side::Connection;
|
||||||
start_node.west = Side::Connection;
|
start_node.west = Side::Connection;
|
||||||
nodes.insert((0, 0), start_node);
|
nodes.insert(initial_point, start_node);
|
||||||
let mut m = GameMap { nodes, grid_size, end_node: (0, 0) };
|
let mut m = GameMap { nodes, grid_size, end_node: initial_point };
|
||||||
m.generate_map();
|
m.generate_map();
|
||||||
m
|
m
|
||||||
}
|
}
|
||||||
@@ -336,23 +334,23 @@ impl GameMap {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ensure_connection(&self, node: &mut MapNode) {
|
fn ensure_connection(&self, node: &mut MapNode, (x, y): (i32, i32)) {
|
||||||
let am_connections = (node.north == Side::Connection) as u8 +
|
let am_connections = (node.north == Side::Connection) as u8 +
|
||||||
(node.south == Side::Connection) as u8 +
|
(node.south == Side::Connection) as u8 +
|
||||||
(node.east == Side::Connection) as u8 +
|
(node.east == Side::Connection) as u8 +
|
||||||
(node.west == Side::Connection) as u8;
|
(node.west == Side::Connection) as u8;
|
||||||
|
|
||||||
if am_connections <= 2 {
|
if am_connections <= 2 {
|
||||||
if node.north != Side::Connection {
|
if node.north != Side::Connection && !self.nodes.contains_key(&(x, y + 1)) {
|
||||||
node.north = Side::Connection
|
node.north = Side::Connection
|
||||||
}
|
}
|
||||||
else if node.south != Side::Connection {
|
else if node.south != Side::Connection && !self.nodes.contains_key(&(x, y - 1)) {
|
||||||
node.south = Side::Connection
|
node.south = Side::Connection
|
||||||
}
|
}
|
||||||
else if node.east != Side::Connection {
|
else if node.east != Side::Connection && !self.nodes.contains_key(&(x + 1, y)) {
|
||||||
node.east = Side::Connection
|
node.east = Side::Connection
|
||||||
}
|
}
|
||||||
else if node.west != Side::Connection {
|
else if node.west != Side::Connection && !self.nodes.contains_key(&(x - 1, y)) {
|
||||||
node.west = Side::Connection
|
node.west = Side::Connection
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -384,7 +382,7 @@ impl GameMap {
|
|||||||
new_node.west = west.map_or_else(|| self.choose_side(current_idx), |w| w.east);
|
new_node.west = west.map_or_else(|| self.choose_side(current_idx), |w| w.east);
|
||||||
|
|
||||||
if !(current_idx.0 > self.grid_size || current_idx.1 > self.grid_size || current_idx.0 < 0 || current_idx.1 < 0) {
|
if !(current_idx.0 > self.grid_size || current_idx.1 > self.grid_size || current_idx.0 < 0 || current_idx.1 < 0) {
|
||||||
self.ensure_connection(&mut new_node);
|
self.ensure_connection(&mut new_node, current_idx);
|
||||||
}
|
}
|
||||||
if new_node.north == Side::Connection && !self.nodes.contains_key(&(x, y + 1)) {
|
if new_node.north == Side::Connection && !self.nodes.contains_key(&(x, y + 1)) {
|
||||||
nodes_to_process.push((x, y + 1));
|
nodes_to_process.push((x, y + 1));
|
||||||
@@ -401,6 +399,14 @@ impl GameMap {
|
|||||||
|
|
||||||
self.nodes.insert(current_idx, new_node);
|
self.nodes.insert(current_idx, new_node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let end_node = self.nodes.get_mut(&self.end_node).unwrap();
|
||||||
|
if self.end_node.1 >= self.end_node.0 {
|
||||||
|
end_node.north = Side::Connection;
|
||||||
|
}
|
||||||
|
else if self.end_node.0 >= self.end_node.1 {
|
||||||
|
end_node.east = Side::Connection;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_map(&self) {
|
fn print_map(&self) {
|
||||||
|
|||||||
Reference in New Issue
Block a user