Move collider add code into MapPlugin.

This commit is contained in:
Nolan Darilek 2021-06-04 07:39:38 -05:00
parent 72c647385a
commit f01b0856f4
2 changed files with 33 additions and 32 deletions

View File

@ -1,6 +1,7 @@
use std::collections::{HashMap, HashSet};
use bevy::prelude::*;
use bevy_rapier2d::prelude::*;
use derive_more::{Deref, DerefMut};
use mapgen::{geometry::Rect as MRect, Map as MapgenMap, MapFilter, TileType};
use maze_generator::{prelude::*, recursive_backtracking::RbGenerator};
@ -203,6 +204,37 @@ impl MapFilter for GridBuilder {
}
}
fn add_map_colliders(mut commands: Commands, maps: Query<(Entity, &Map), Added<Map>>) {
for (map_entity, map) in maps.iter() {
let rigid_body_entity = commands
.entity(map_entity)
.insert_bundle(RigidBodyBundle {
body_type: RigidBodyType::Static,
..Default::default()
})
.id();
for x in 0..map.width() {
for y in 0..map.height() {
let tile = map.base.at(x, y);
if tile.blocks_motion() {
let collider = ColliderBundle {
shape: ColliderShape::cuboid(0.5, 0.5),
..Default::default()
};
let collider_parent = ColliderParent {
handle: rigid_body_entity.handle(),
pos_wrt_parent: Vec2::new(x as f32 + 0.5, y as f32 + 0.5).into(),
};
commands
.spawn()
.insert_bundle(collider)
.insert(collider_parent);
}
}
}
}
}
fn portal_spawner(
mut commands: Commands,
map: Query<(Entity, &Map), Added<Map>>,
@ -372,6 +404,7 @@ impl Plugin for MapPlugin {
const SPAWN_PORTALS: &str = "SPAWN_PORTALS";
app.register_type::<Portal>()
.insert_resource(PreviousIndex::default())
.add_system(add_map_colliders.system())
.add_system(entity_indexing.system().label(UPDATE_ENTITY_INDEX_LABEL))
.add_system(
portal_spawner

View File

@ -42,37 +42,6 @@ pub struct Speed(pub f32);
#[reflect(Component)]
pub struct Sprinting;
fn add_map_colliders(mut commands: Commands, maps: Query<(Entity, &Map), Added<Map>>) {
for (map_entity, map) in maps.iter() {
let rigid_body_entity = commands
.entity(map_entity)
.insert_bundle(RigidBodyBundle {
body_type: RigidBodyType::Static,
..Default::default()
})
.id();
for x in 0..map.width() {
for y in 0..map.height() {
let tile = map.base.at(x, y);
if tile.blocks_motion() {
let collider = ColliderBundle {
shape: ColliderShape::cuboid(0.5, 0.5),
..Default::default()
};
let collider_parent = ColliderParent {
handle: rigid_body_entity.handle(),
pos_wrt_parent: Vec2::new(x as f32 + 0.5, y as f32 + 0.5).into(),
};
commands
.spawn()
.insert_bundle(collider)
.insert(collider_parent);
}
}
}
}
}
fn movement_controls<S, A: 'static>(
mut commands: Commands,
config: Res<NavigationConfig<S, A>>,
@ -288,7 +257,6 @@ where
app.register_type::<MaxSpeed>()
.register_type::<RotationSpeed>()
.register_type::<Sprinting>()
.add_system(add_map_colliders.system())
.add_system(speak_direction.system().chain(error_handler.system()));
if config.movement_states.is_empty() {
} else {