From 6c3db77e9ee9e1d858e05b13eefaa44bc4d1ae74 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Wed, 2 Jun 2021 15:50:38 -0500 Subject: [PATCH] Spawn colliders for blocked tiles. --- src/navigation.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/navigation.rs b/src/navigation.rs index 959cd75..b6e0aec 100644 --- a/src/navigation.rs +++ b/src/navigation.rs @@ -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>) { + 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( mut commands: Commands, config: Res>, @@ -504,6 +535,7 @@ where .register_type::() .add_event::() .insert_resource(PreviousBlocksMotionIndex::default()) + .add_system(add_map_colliders.system()) .add_system_to_stage( CoreStage::PostUpdate, blocks_motion_indexing