add projectile::colide()

This commit is contained in:
Astro 2022-01-23 19:12:06 +01:00
parent 1ebc94bd9d
commit 7ab0f4612b
4 changed files with 56 additions and 10 deletions

View File

@ -11,6 +11,7 @@ mod map;
mod player;
mod off_map;
mod enemy;
mod projectile;
#[derive(PhysicsLayer)]
pub enum Layer {
@ -38,6 +39,7 @@ fn main() {
.add_system(player::input.after("input"))
.add_system(off_map::check)
.add_system(enemy::walk)
.add_system(projectile::collide)
.add_system(exit_on_escape)
// .add_system(log_collisions)
.run();

View File

@ -8,6 +8,7 @@ use crate::{
enemy::Enemy,
player::Player,
off_map::CanFallOffMap,
projectile::EGG_RADIUS,
Layer,
};
@ -101,7 +102,7 @@ pub fn setup(
bridge_material: materials.add(Color::rgb(0.5, 0.4, 0.1).into()),
egg_material: materials.add(Color::rgb(1.0, 1.0, 0.9).into()),
egg_mesh: meshes.add(Mesh::from(shape::Icosphere {
radius: crate::player::EGG_RADIUS,
radius: EGG_RADIUS,
subdivisions: 8,
})),
last_build: None,
@ -326,7 +327,7 @@ fn add_island(
})
.insert(PhysicMaterial {
restitution: 1.0,
density: 500.0,
density: 300.0,
friction: 0.0,
})
.insert(RotationConstraints::lock())
@ -335,7 +336,7 @@ fn add_island(
.insert(CanFallOffMap)
.insert(Enemy {
rotation: 0.0,
bounds: (translation.x - half_extends.x + 1.0..=translation.x + half_extends.x - 1.0, translation.z - half_extends.z + 1.0..=translation.z + half_extends.z - 1.0),
bounds: (translation.x - half_extends.x + 0.5..=translation.x + half_extends.x - 0.5, translation.z - half_extends.z + 0.5..=translation.z + half_extends.z - 0.5),
patrol_target: None,
})
.with_children(|children| {

View File

@ -6,11 +6,10 @@ use crate::{
input::{InputState, Key, Source as InputSource},
map::GroundContact,
off_map::CanFallOffMap,
projectile::{EGG_RADIUS, Projectile},
Layer,
};
pub const EGG_RADIUS: f32 = 0.15;
#[derive(Component)]
pub struct Player {
input_source: InputSource,
@ -116,7 +115,7 @@ pub fn input(
1.2
} else if x != 0.0 || z != 0.0 {
// walk bobbing
0.2
0.3
} else {
0.0
};
@ -145,7 +144,7 @@ pub fn input(
&& input.keys.contains(&(player.input_source.clone(), Key::Shoot))
{
let direction = Quat::from_rotation_y(player.rotation) * Vec3::Z;
let mut transform = Transform::from_translation(transform.translation + 0.5 * direction);
let mut transform = Transform::from_translation(transform.translation + 1.0 * direction);
transform.scale += 0.3 * direction;
commands.spawn()
.insert(RigidBody::Dynamic)
@ -157,11 +156,12 @@ pub fn input(
radius: EGG_RADIUS,
})
.insert(PhysicMaterial {
restitution: 0.0,
density: 20_000.0,
restitution: 1.0,
density: 10_000.0,
friction: 1.0,
})
.insert(Velocity::from_linear(20.0 * direction))
.insert(Velocity::from_linear(30.0 * direction))
.insert(Projectile)
.insert(CanFallOffMap)
.insert(GlobalTransform::default())
.insert_bundle(PbrBundle {

43
src/projectile.rs Normal file
View File

@ -0,0 +1,43 @@
use std::collections::HashMap;
use rand::prelude::*;
use bevy::{
prelude::*,
};
use heron::prelude::*;
use crate::{
player::Player,
Layer,
};
pub const EGG_RADIUS: f32 = 0.15;
#[derive(Component)]
pub struct Projectile;
pub fn collide(
mut events: EventReader<CollisionEvent>,
mut velocities: Query<&mut Velocity>
) {
fn is_projectile(layers: CollisionLayers) -> bool {
layers.contains_group(Layer::Projectile)
}
for event in events.iter() {
let (entity_1, entity_2) = event.rigid_body_entities();
let (layers_1, layers_2) = event.collision_layers();
if is_projectile(layers_1) {
let projectile_velocity = velocities.get(entity_1)
.expect("entity_1");
if let Ok(mut velocity) = velocities.get_mut(entity_2) {
*velocity.linear = (Vec3::from(*velocity.linear) + Vec3::from(*projectile_velocity.linear)).into();
}
}
if is_projectile(layers_2) {
let projectile_velocity = velocities.get(entity_2)
.expect("entity_2");
if let Ok(mut velocity) = velocities.get_mut(entity_1) {
*velocity.linear = (Vec3::from(*velocity.linear) + Vec3::from(*projectile_velocity.linear)).into();
}
}
}
}