diff --git a/Cargo.lock b/Cargo.lock index f283fbd..3b3fb24 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -227,7 +227,7 @@ dependencies = [ "ndk-glue", "notify", "parking_lot", - "rand", + "rand 0.7.3", "ron", "serde", "thiserror", @@ -300,7 +300,7 @@ dependencies = [ "fixedbitset 0.3.1", "lazy_static", "parking_lot", - "rand", + "rand 0.7.3", "serde", "thiserror", ] @@ -2148,7 +2148,7 @@ dependencies = [ "num-complex", "num-rational", "num-traits", - "rand", + "rand 0.7.3", "rand_distr", "simba", "typenum", @@ -2572,9 +2572,21 @@ checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" dependencies = [ "getrandom 0.1.15", "libc", - "rand_chacha", - "rand_core", - "rand_hc", + "rand_chacha 0.2.2", + "rand_core 0.5.1", + "rand_hc 0.2.0", +] + +[[package]] +name = "rand" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +dependencies = [ + "libc", + "rand_chacha 0.3.0", + "rand_core 0.6.1", + "rand_hc 0.3.0", ] [[package]] @@ -2584,7 +2596,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" dependencies = [ "ppv-lite86", - "rand_core", + "rand_core 0.5.1", +] + +[[package]] +name = "rand_chacha" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +dependencies = [ + "ppv-lite86", + "rand_core 0.6.1", ] [[package]] @@ -2596,6 +2618,15 @@ dependencies = [ "getrandom 0.1.15", ] +[[package]] +name = "rand_core" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c026d7df8b298d90ccbbc5190bd04d85e159eaf5576caeacf8741da93ccbd2e5" +dependencies = [ + "getrandom 0.2.0", +] + [[package]] name = "rand_distr" version = "0.3.0" @@ -2603,7 +2634,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c9e9532ada3929fb8b2e9dbe28d1e06c9b2cc65813f074fcb6bd5fbefeff9d56" dependencies = [ "num-traits", - "rand", + "rand 0.7.3", ] [[package]] @@ -2612,7 +2643,16 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" dependencies = [ - "rand_core", + "rand_core 0.5.1", +] + +[[package]] +name = "rand_hc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +dependencies = [ + "rand_core 0.6.1", ] [[package]] @@ -2743,7 +2783,7 @@ dependencies = [ "bevy", "bevy_rapier2d", "bevy_webgl2", - "rand", + "rand 0.8.3", "winit", ] @@ -3271,7 +3311,7 @@ version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9fde2f6a4bea1d6e007c4ad38c6839fa71cbb63b6dbf5b595aa38dc9b1093c11" dependencies = [ - "rand", + "rand 0.7.3", "serde", ] diff --git a/Cargo.toml b/Cargo.toml index b9d3e4c..18f1429 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ web = [ ] [dependencies] -rand = "*" +rand = "0.8" anyhow = "1" bevy = {version="0.4", default-features=false} bevy_webgl2 = {version="0.4", optional=true} diff --git a/src/bomb.rs b/src/bomb.rs index 786233e..6eeb089 100644 --- a/src/bomb.rs +++ b/src/bomb.rs @@ -1,8 +1,4 @@ use bevy::prelude::*; -use bevy_rapier2d::{ - physics::{ColliderHandleComponent, RigidBodyHandleComponent}, - rapier::{dynamics::RigidBodyBuilder, geometry::ColliderBuilder}, -}; use crate::{ assets::{ @@ -14,8 +10,8 @@ use crate::{ Wall, FIRE_ANIMATE_TIME, }, entitys::{ - create_bomb, create_bomb_number_buff, create_center_fire, create_collider, create_ember, - create_portal, create_power_buff, create_speed_buff, create_static_rigid_body, + create_bomb, create_bomb_number_buff, create_center_fire, create_ember, create_portal, + create_power_buff, create_speed_buff, }, events::GameEvents, resources::Map, @@ -36,7 +32,6 @@ impl BombSystems for SystemStage { .add_system(animate_bomb.system()) .add_system(animate_fire.system()) .add_system(ember_trigger.system()) - .add_system(for_wall_add_collision_detection.system()) } } @@ -58,30 +53,7 @@ impl BombBunble { } } } -fn for_wall_add_collision_detection( - commands: &mut Commands, - query: Query< - (Entity, &Transform), - ( - With, - Without, - Without, - Without, - Without, - ), - >, -) { - for (entity, transform) in query.iter() { - let translation = transform.translation; - commands.insert( - entity, - ( - create_static_rigid_body(translation.x, translation.y), - create_collider(entity), - ), - ); - } -} + fn space_to_set_bomb( commands: &mut Commands, bomb_texture_atlas: Res, diff --git a/src/components.rs b/src/components.rs index 7e7c19a..e254d78 100644 --- a/src/components.rs +++ b/src/components.rs @@ -83,7 +83,11 @@ impl Ember { Ember(Timer::from_seconds(EMBER_START_TIME, false), power) } } - +use bevy_rapier2d::na::Vector2; +use rand::{ + distributions::{Distribution, Standard}, + Rng, +}; #[derive(Debug, PartialEq, Copy, Clone)] pub enum Direction { Left = 0, @@ -91,6 +95,28 @@ pub enum Direction { Right = 2, Down = 3, } +impl Distribution for Standard { + fn sample(&self, rng: &mut R) -> Direction { + match rng.gen_range(0..=3) { + // rand 0.8 + 0 => Direction::Left, + 1 => Direction::Up, + 2 => Direction::Right, + _ => Direction::Down, + } + } +} +impl Direction { + pub fn into_dir(&self) -> Vector2 { + match self { + Direction::Up => Vector2::new(-1.0, 0.0), + Direction::Left => Vector2::new(0.0, 1.0), + Direction::Down => Vector2::new(1.0, 0.0), + Direction::Right => Vector2::new(0.0, -1.0), + } + } +} + pub const NEXT_PLAYER_SHEET: u32 = 14; pub struct AnimateIndexs { diff --git a/src/creatures.rs b/src/creatures.rs index 7513123..fc93cee 100644 --- a/src/creatures.rs +++ b/src/creatures.rs @@ -1,25 +1,14 @@ use crate::{ - components::{AnimateIndexs, Animation, Destructible, Direction, Player, Stop, Velocity}, - entitys::{create_creature_collider, create_dyn_rigid_body}, + components::{AnimateIndexs, Animation, Destructible, Direction, Stop, Velocity}, errors::querr_error_handler, - events::*, ui::DrawBlinkTimer, - utils::vecs_xy_intersect, }; +use bevy::ecs::{Query, ResMut, SystemStage, With}; use bevy::{ecs::QueryError, prelude::*}; -use bevy::{ - ecs::{Query, ResMut, SystemStage, With}, - sprite::ColorMaterial, -}; use bevy_rapier2d::{ - na::Vector2, - physics::{ColliderHandleComponent, RigidBodyHandleComponent}, - rapier::{ - dynamics::{RigidBodyBuilder, RigidBodySet}, - geometry::ColliderBuilder, - }, + na::Vector2, physics::RigidBodyHandleComponent, rapier::dynamics::RigidBodySet, }; -use rand::{seq::SliceRandom, thread_rng}; +use rand::{thread_rng, Rng}; #[derive(Bundle)] pub struct CreatureBundle { @@ -42,30 +31,37 @@ impl Default for CreatureBundle { } } -pub struct CreatureMaterial(pub Handle); - pub struct Creature; -// could be done with a crate -const DIRECTIONS: [Direction; 4] = [ - Direction::Up, - Direction::Down, - Direction::Left, - Direction::Right, -]; -const TURN_PROBABILITY: f32 = 0.02; +const TURN_PROBABILITY: i32 = 4; +pub trait CreatureSystems { + fn creature_systems(&mut self) -> &mut Self; +} +impl CreatureSystems for SystemStage { + fn creature_systems(&mut self) -> &mut Self { + self.add_system( + creature_movement + .system() + .chain(querr_error_handler.system()), + ) + .add_system(despawn_player.system()) + .add_system(animate_creature.system()) + } +} + fn creature_movement( mut query: Query<(Entity, &Velocity, &mut Direction), (With, Without)>, mut rigid_body_handle_query: Query<&mut RigidBodyHandleComponent>, mut rigid_body_set: ResMut, ) -> Result<(), QueryError> { for (entity, velocity, mut direction) in query.iter_mut() { - let mut rng = thread_rng(); let rigid_body_handle = rigid_body_handle_query.get_component_mut::(entity)?; - if rand::random::() < TURN_PROBABILITY { + let mut rng = thread_rng(); + let n = rng.gen_range(0..=100); + if n < TURN_PROBABILITY { // only change ocassionally - *direction = *DIRECTIONS.choose(&mut rng).unwrap() + *direction = rand::random(); } let linvel = match *direction { Direction::Left => Vector2::new(-velocity.0, 0.0), @@ -82,67 +78,9 @@ fn creature_movement( } Ok(()) } -fn for_creature_add_collision_detection( - commands: &mut Commands, - query: Query< - (Entity, &Transform), - ( - With, - Without, - Without, - Without, - Without, - ), - >, -) { - for (entity, transform) in query.iter() { - let translation = transform.translation; - commands.insert( - entity, - ( - create_dyn_rigid_body(translation.x, translation.y), - create_creature_collider(entity), - ), - ); - } -} - -pub trait CreatureSystems { - fn creature_systems(&mut self) -> &mut Self; -} -impl CreatureSystems for SystemStage { - fn creature_systems(&mut self) -> &mut Self { - self.add_system(creature_player_collision.system()) - .add_system(for_creature_add_collision_detection.system()) - .add_system( - creature_movement - .system() - .chain(querr_error_handler.system()), - ) - .add_system(despawn_player.system()) - .add_system(animate_creature.system()) - } -} - -fn creature_player_collision( - commands: &mut Commands, - mut player_query: Query<(Entity, &mut Transform), (With, Without)>, - mut creature_query: Query<&mut Transform, With>, - mut game_over_events: ResMut>, -) { - for (entity, player_transform) in player_query.iter_mut() { - let player_pos = &player_transform.translation.truncate(); - for creature_transform in creature_query.iter_mut() { - if vecs_xy_intersect(&creature_transform.translation.truncate(), player_pos) { - commands.insert(entity, StopAndFlashing::default()); - game_over_events.send(GameEvents::GameOver); - } - } - } -} #[derive(Bundle)] -struct StopAndFlashing(Stop, DrawBlinkTimer, Timer); +pub struct StopAndFlashing(Stop, DrawBlinkTimer, Timer); impl Default for StopAndFlashing { fn default() -> Self { Self( diff --git a/src/entitys.rs b/src/entitys.rs index f987636..21ebaa0 100644 --- a/src/entitys.rs +++ b/src/entitys.rs @@ -1,8 +1,4 @@ use bevy::prelude::*; -use bevy_rapier2d::rapier::{ - dynamics::RigidBodyBuilder, - geometry::{ColliderBuilder, InteractionGroups}, -}; use crate::{ bomb::{BombBunble, FireBundle}, @@ -10,7 +6,7 @@ use crate::{ constants::{FLOOR_LAYER, OBJECT_LAYER, PLAYER_LAYER}, creatures::CreatureBundle, player::PlayerBundle, - utils::{HALF_TILE_WIDTH, SCALE, TILE_WIDTH}, + utils::{SCALE, TILE_WIDTH}, }; #[inline(always)] @@ -45,78 +41,7 @@ fn create_transform_vec3(translation: Vec3) -> Transform { ..Default::default() } } -#[inline(always)] -pub fn create_static_rigid_body(translation_x: f32, translation_y: f32) -> RigidBodyBuilder { - RigidBodyBuilder::new_static() - .translation(translation_x, translation_y) - .lock_rotations() - .lock_translations() -} -#[inline(always)] -pub fn create_dyn_rigid_body(translation_x: f32, translation_y: f32) -> RigidBodyBuilder { - RigidBodyBuilder::new_dynamic() - .translation(translation_x, translation_y) - .lock_rotations() -} -#[inline(always)] -pub fn create_collider(entity: Entity) -> ColliderBuilder { - ColliderBuilder::cuboid(HALF_TILE_WIDTH, HALF_TILE_WIDTH) - .friction(0.0) - .restitution(0.0) - .user_data(entity.to_bits() as u128) -} -const CREATURE_GROUPS: u16 = 0b0010; -const PLAYER_GROUPS: u16 = 0b0001; -const WALL_GROUPS: u16 = 0b0100; - -#[inline(always)] -pub fn create_creature_collider(entity: Entity) -> ColliderBuilder { - //ColliderBuilder::cuboid(TILE_WIDTH / 2.0, TILE_WIDTH / 2.0) - ColliderBuilder::ball(HALF_TILE_WIDTH) - .friction(0.0) - .restitution(0.0) - .user_data(entity.to_bits() as u128) - .solver_groups(InteractionGroups::new(CREATURE_GROUPS, WALL_GROUPS)) -} -#[inline(always)] -pub fn create_fire_collider(entity: Entity) -> ColliderBuilder { - ColliderBuilder::cuboid(HALF_TILE_WIDTH, HALF_TILE_WIDTH) - .friction(0.0) - .restitution(0.0) - .user_data(entity.to_bits() as u128) - .solver_groups(InteractionGroups::none()) -} -#[inline(always)] -pub fn create_ball_collider(entity: Entity) -> ColliderBuilder { - ColliderBuilder::ball(HALF_TILE_WIDTH) - .friction(0.0) - .restitution(0.0) - .sensor(true) - .user_data(entity.to_bits() as u128) -} -#[inline(always)] -pub fn create_player_collider(entity: Entity) -> ColliderBuilder { - //ColliderBuilder::cuboid(TILE_WIDTH / 2.0, TILE_WIDTH / 2.0) - ColliderBuilder::ball(HALF_TILE_WIDTH) - .friction(0.0) - .restitution(0.0) - .user_data(entity.to_bits() as u128) - //.sensor(true) - .solver_groups(InteractionGroups::new(PLAYER_GROUPS, WALL_GROUPS)) -} -#[inline(always)] -pub fn create_sensor_collider(entity: Entity) -> ColliderBuilder { - ColliderBuilder::cuboid(HALF_TILE_WIDTH, HALF_TILE_WIDTH) - .sensor(true) - .user_data(entity.to_bits() as u128) -} -#[inline(always)] -pub fn create_ball_sensor_collider(entity: Entity) -> ColliderBuilder { - ColliderBuilder::ball(HALF_TILE_WIDTH) - .sensor(true) - .user_data(entity.to_bits() as u128) -} #[inline(always)] fn create_wall( commands: &mut Commands, diff --git a/src/main.rs b/src/main.rs index 20c9973..ccdcb58 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,6 +23,7 @@ pub mod creatures; pub mod entitys; pub mod errors; pub mod events; +pub mod physics; pub mod player; pub mod portal; pub mod resources; diff --git a/src/physics.rs b/src/physics.rs new file mode 100644 index 0000000..3cf7ca5 --- /dev/null +++ b/src/physics.rs @@ -0,0 +1,329 @@ +use anyhow::{anyhow, Result}; +use bevy::prelude::*; +use bevy_rapier2d::{ + na::Vector2, + physics::{ColliderHandleComponent, EventQueue, RigidBodyHandleComponent}, + rapier::{ + dynamics::{RigidBodyBuilder, RigidBodySet}, + geometry::{ColliderBuilder, ColliderSet, InteractionGroups, Ray}, + ncollide::{math::Point, narrow_phase::ContactEvent::Started}, + pipeline::QueryPipeline, + }, +}; +use rand::{Rng, thread_rng}; + +use crate::{ + components::{Direction, *}, + creatures::{Creature, StopAndFlashing}, + errors::error_handler, + events::GameEvents, + utils::HALF_TILE_WIDTH, +}; + +const CREATURE_GROUPS: u16 = 0b0010; +const PLAYER_GROUPS: u16 = 0b0001; +const WALL_GROUPS: u16 = 0b0100; +const WAY_GROUPS: u16 = 0b1000; +const NONE_GROUPS: u16 = 0b0000; + +pub trait PhysicsSystems { + fn physics_systems(&mut self) -> &mut Self; +} +impl PhysicsSystems for SystemStage { + fn physics_systems(&mut self) -> &mut Self { + self.add_system(for_player_add_collision_detection.system()) + .add_system(for_wall_add_collision_detection.system()) + .add_system(for_way_add_collision_detection.system()) + .add_system(for_creature_add_collision_detection.system()) + .add_system(handle_contact_events.system().chain(error_handler.system())) + } +} +fn for_player_add_collision_detection( + commands: &mut Commands, + query: Query< + (Entity, &Transform), + ( + With, + Without, + Without, + Without, + Without, + ), + >, +) { + for (entity, transform) in query.iter() { + let translation = transform.translation; + commands.insert( + entity, + ( + create_dyn_rigid_body(translation.x, translation.y), + create_player_collider(entity), + ), + ); + } +} +fn for_wall_add_collision_detection( + commands: &mut Commands, + query: Query< + (Entity, &Transform), + ( + With, + Without, + Without, + Without, + Without, + ), + >, +) { + for (entity, transform) in query.iter() { + let translation = transform.translation; + commands.insert( + entity, + ( + create_static_rigid_body(translation.x, translation.y), + create_collider(entity), + ), + ); + } +} +fn for_way_add_collision_detection( + commands: &mut Commands, + query: Query< + (Entity, &Transform), + ( + With, + Without, + Without, + Without, + Without, + ), + >, +) { + for (entity, transform) in query.iter() { + let translation = transform.translation; + commands.insert( + entity, + ( + create_static_rigid_body(translation.x, translation.y), + create_way_collider(entity), + ), + ); + } +} +fn for_creature_add_collision_detection( + commands: &mut Commands, + query: Query< + (Entity, &Transform), + ( + With, + Without, + Without, + Without, + Without, + ), + >, +) { + for (entity, transform) in query.iter() { + let translation = transform.translation; + commands.insert( + entity, + ( + create_dyn_rigid_body(translation.x, translation.y), + create_creature_collider(entity), + ), + ); + } +} + +#[inline(always)] +pub fn get_velocity_vec(direction: &Direction, velocity: f32) -> Vector2 { + match direction { + Direction::Left => Vector2::new(-velocity, 0.0), + Direction::Up => Vector2::new(0.0, velocity), + Direction::Right => Vector2::new(velocity, 0.0), + Direction::Down => Vector2::new(0.0, -velocity), + } +} +fn handle_contact_events( + commands: &mut Commands, + events: Res, + collider_set: Res, + //query_pipeline: Res, + mut game_over_events: ResMut>, + //mut rigid_body_handle_query: Query<&mut RigidBodyHandleComponent>, + //mut rigid_body_set: ResMut, + mut query_set: QuerySet<( + Query< + (Option<&Player>, Option<&Direction>), + ( + Or<(With, With, With)>, + Without, + ), + >, + Query<&mut Direction>, + )>, +) -> Result<()> { + while let Ok(contact_event) = events.contact_events.pop() { + match contact_event { + Started(idxl, idxr) => { + let entity_left = Entity::from_bits( + collider_set + .get(idxl) + .ok_or(anyhow!("get error!"))? + .user_data as u64, + ); + let entity_right = Entity::from_bits( + collider_set + .get(idxr) + .ok_or(anyhow!("get error!"))? + .user_data as u64, + ); + match query_set.q0().get(entity_left) { + Ok((Some(_), Some(_))) => { + // player + match query_set.q0().get(entity_right) { + Ok((None, Some(_))) => { + // creature + commands.insert(entity_left, StopAndFlashing::default()); + game_over_events.send(GameEvents::GameOver); + } + Ok(_) => {} + Err(err) => { + error!("{:?}", err); + } + } + } + Ok((None, Some(_))) => { + // creature + match query_set.q0().get(entity_right) { + Ok((None, None)) => { + //wall + //TODO:Make creatures smarter + if let Ok(mut direction) = query_set.q1_mut().get_mut(entity_left) { + let mut rng = thread_rng(); + let n = rng.gen_range(0..=100); + if n <= 50 { + // only change ocassionally + *direction = rand::random(); + } + } + } + Ok((Some(_), Some(_))) => { + commands.insert(entity_left, StopAndFlashing::default()); + game_over_events.send(GameEvents::GameOver); + } + Ok(_) => {} + Err(err) => { + error!("{:?}", err); + } + } + } + Ok((None, None)) => { + // wall + match query_set.q0().get(entity_right) { + Ok((None, Some(_))) => { + //TODO:Make creatures smarter + if let Ok(mut direction) = query_set.q1_mut().get_mut(entity_right) + { + let mut rng = thread_rng(); + let n = rng.gen_range(0..=100); + if n <= 50 { + // only change ocassionally + *direction = rand::random(); + } + } + } + Ok(_) => {} + Err(err) => { + error!("{:?}", err); + } + } + } + Ok(_) => {} + Err(err) => { + error!("error:{:?}", err); + } + } + } + _ => {} + } + } + Ok(()) +} +#[inline(always)] +pub fn create_static_rigid_body(translation_x: f32, translation_y: f32) -> RigidBodyBuilder { + RigidBodyBuilder::new_static() + .translation(translation_x, translation_y) + .lock_rotations() + .lock_translations() +} +#[inline(always)] +pub fn create_dyn_rigid_body(translation_x: f32, translation_y: f32) -> RigidBodyBuilder { + RigidBodyBuilder::new_dynamic() + .translation(translation_x, translation_y) + .lock_rotations() +} +#[inline(always)] +pub fn create_collider(entity: Entity) -> ColliderBuilder { + ColliderBuilder::cuboid(HALF_TILE_WIDTH, HALF_TILE_WIDTH) + .friction(0.0) + .restitution(0.0) + .user_data(entity.to_bits() as u128) +} + +#[inline(always)] +pub fn create_creature_collider(entity: Entity) -> ColliderBuilder { + //ColliderBuilder::cuboid(HALF_TILE_WIDTH, HALF_TILE_WIDTH) + + ColliderBuilder::ball(HALF_TILE_WIDTH) + .friction(0.0) + .restitution(0.0) + .user_data(entity.to_bits() as u128) + .solver_groups(InteractionGroups::new(CREATURE_GROUPS, WALL_GROUPS)) +} +#[inline(always)] +pub fn create_way_collider(entity: Entity) -> ColliderBuilder { + ColliderBuilder::cuboid(HALF_TILE_WIDTH, HALF_TILE_WIDTH) + .user_data(entity.to_bits() as u128) + .solver_groups(InteractionGroups::new(WAY_GROUPS, NONE_GROUPS)) + .collision_groups(InteractionGroups::new(WAY_GROUPS, NONE_GROUPS)) +} +#[inline(always)] +pub fn create_fire_collider(entity: Entity) -> ColliderBuilder { + ColliderBuilder::cuboid(HALF_TILE_WIDTH, HALF_TILE_WIDTH) + .friction(0.0) + .restitution(0.0) + .user_data(entity.to_bits() as u128) + .solver_groups(InteractionGroups::none()) +} +#[inline(always)] +pub fn create_ball_collider(entity: Entity) -> ColliderBuilder { + ColliderBuilder::ball(HALF_TILE_WIDTH) + .friction(0.0) + .restitution(0.0) + .sensor(true) + .user_data(entity.to_bits() as u128) +} +#[inline(always)] +pub fn create_player_collider(entity: Entity) -> ColliderBuilder { + //ColliderBuilder::cuboid(HALF_TILE_WIDTH, HALF_TILE_WIDTH) + ColliderBuilder::ball(HALF_TILE_WIDTH) + .friction(0.0) + .restitution(0.0) + .user_data(entity.to_bits() as u128) + //.sensor(true) + .solver_groups(InteractionGroups::new(PLAYER_GROUPS, WALL_GROUPS)) +} + +#[inline(always)] +pub fn create_sensor_collider(entity: Entity) -> ColliderBuilder { + ColliderBuilder::cuboid(HALF_TILE_WIDTH, HALF_TILE_WIDTH) + .sensor(true) + .user_data(entity.to_bits() as u128) +} +#[inline(always)] +pub fn create_ball_sensor_collider(entity: Entity) -> ColliderBuilder { + ColliderBuilder::ball(HALF_TILE_WIDTH) + .sensor(true) + .user_data(entity.to_bits() as u128) +} diff --git a/src/player.rs b/src/player.rs index c435a1f..be91a00 100644 --- a/src/player.rs +++ b/src/player.rs @@ -3,19 +3,13 @@ use crate::{ AnimateIndexs, Animation, BombNumber, BombPower, Destructible, Direction, Player, Stop, Velocity, }, - entitys::{create_dyn_rigid_body, create_player_collider}, errors::querr_error_handler, }; use anyhow::Result; use bevy::{ecs::QueryError, prelude::*}; use bevy_rapier2d::{ - na::Vector2, - physics::{ColliderHandleComponent, RigidBodyHandleComponent}, - rapier::{ - dynamics::{RigidBodyBuilder, RigidBodySet}, - geometry::ColliderBuilder, - }, + na::Vector2, physics::RigidBodyHandleComponent, rapier::dynamics::RigidBodySet, }; pub trait PlayerSystems { @@ -27,7 +21,6 @@ impl PlayerSystems for SystemStage { // movement .add_system(movement.system().chain(querr_error_handler.system())) .add_system(stop_player.system()) - .add_system(for_player_add_collision_detection.system()) // animate .add_system(animate_player.system()) .add_system(velocity_to_animation.system()) @@ -57,30 +50,6 @@ impl Default for PlayerBundle { } } } -fn for_player_add_collision_detection( - commands: &mut Commands, - query: Query< - (Entity, &Transform), - ( - With, - Without, - Without, - Without, - Without, - ), - >, -) { - for (entity, transform) in query.iter() { - let translation = transform.translation; - commands.insert( - entity, - ( - create_dyn_rigid_body(translation.x, translation.y), - create_player_collider(entity), - ), - ); - } -} fn movement( keyboard_input: Res>, diff --git a/src/resources.rs b/src/resources.rs index 6a7d544..acc80d2 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -1,7 +1,5 @@ use std::ops::{Deref, DerefMut}; -use bevy_rapier2d::rapier::geometry::ColliderSet; - use crate::utils::TILE_WIDTH; pub struct Map { @@ -35,14 +33,14 @@ impl Map { pub fn init(&mut self) { self.value = vec![ vec![1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 1], - vec![1, 0, 2, 2, 0, 4, 0, 0, 2, 0, 0, 7, 1], - vec![1, 0, 9, 0, 9, 4, 9, 0, 9, 7, 1, 0, 1], - vec![1, 0, 0, 4, 0, 0, 0, 0, 0, 7, 1, 0, 1], + vec![1, 0, 2, 2, 0, 4, 7, 0, 2, 0, 0, 7, 1], + vec![1, 0, 9, 0, 9, 4, 9, 0, 9, 0, 9, 0, 1], + vec![1, 0, 0, 4, 0, 0, 0, 0, 0, 2, 0, 0, 1], + vec![1, 0, 9, 0, 9, 0, 9, 0, 9, 0, 9, 0, 1], + vec![1, 0, 0, 0, 0, 0, 6, 0, 0, 0, 5, 8, 1], + vec![1, 0, 9, 0, 9, 0, 9, 0, 9, 0, 9, 9, 1], + vec![1, 0, 0, 6, 0, 6, 0, 0, 0, 2, 2, 0, 1], vec![1, 0, 9, 0, 9, 0, 9, 0, 9, 7, 9, 0, 1], - vec![1, 0, 0, 0, 0, 0, 6, 0, 0, 7, 5, 8, 1], - vec![1, 0, 9, 0, 9, 0, 9, 0, 9, 7, 9, 9, 1], - vec![1, 0, 0, 6, 0, 6, 0, 0, 0, 7, 2, 0, 1], - vec![1, 0, 9, 0, 9, 0, 9, 0, 9, 7, 4, 1, 1], vec![1, 3, 8, 0, 0, 0, 0, 0, 0, 0, 2, 0, 1], vec![9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9], ]; @@ -135,4 +133,3 @@ impl DerefMut for Map { &mut self.value } } -pub struct WallColider(ColliderSet); diff --git a/src/state.rs b/src/state.rs index e433e86..cd7e307 100644 --- a/src/state.rs +++ b/src/state.rs @@ -6,6 +6,7 @@ use crate::{ buff::BuffSystems, components::{Animation, Bomb, Fire, InGame, Player, Stop}, creatures::{Creature, CreatureSystems}, + physics::PhysicsSystems, player::PlayerSystems, portal::PortalSystems, setup_map::setup_map, @@ -40,6 +41,7 @@ impl Plugin for AppStatePluge { //.on_state_enter(AppState::Game, spawn_game_ui.system()) .update_stage(AppState::Game, |stage: &mut SystemStage| { stage + .physics_systems() .player_systems() .bomb_systems() .buff_systems()