Spawn colliders for blocked tiles.

This commit is contained in:
Nolan Darilek 2021-06-02 15:50:38 -05:00
parent 5a6a3efb35
commit 6c3db77e9e

View File

@ -2,6 +2,7 @@ use std::{collections::HashMap, error::Error, fmt::Debug, hash::Hash, marker::Ph
use bevy::prelude::*;
use bevy_input_actionmap::InputMap;
use bevy_rapier2d::prelude::*;
use bevy_tts::Tts;
use derive_more::{Deref, DerefMut};
@ -68,6 +69,36 @@ pub struct Collision {
pub index: usize,
}
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(1., 1.),
..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>>,
@ -504,6 +535,7 @@ where
.register_type::<Sprinting>()
.add_event::<Collision>()
.insert_resource(PreviousBlocksMotionIndex::default())
.add_system(add_map_colliders.system())
.add_system_to_stage(
CoreStage::PostUpdate,
blocks_motion_indexing