fix impulse of head/key drops
This commit is contained in:
@@ -12,7 +12,7 @@ use shared::{
|
|||||||
player::Player,
|
player::Player,
|
||||||
protocol::{GltfSceneRoot, PlaySound},
|
protocol::{GltfSceneRoot, PlaySound},
|
||||||
utils::{
|
utils::{
|
||||||
billboards::Billboard, one_shot_force::OneShotForce, squish_animation::SquishAnimation,
|
billboards::Billboard, one_shot_force::OneShotImpulse, squish_animation::SquishAnimation,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use std::f32::consts::PI;
|
use std::f32::consts::PI;
|
||||||
@@ -28,15 +28,9 @@ fn on_head_drop(
|
|||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
) -> Result<(), BevyError> {
|
) -> Result<(), BevyError> {
|
||||||
let drop = trigger.event();
|
let drop = trigger.event();
|
||||||
let should_impulse = drop.impulse;
|
|
||||||
|
|
||||||
let angle = rand::random::<f32>() * PI * 2.;
|
let angle = rand::random::<f32>() * PI * 2.;
|
||||||
let spawn_dir = Quat::from_rotation_y(angle) * Vec3::new(0.5, 0.6, 0.).normalize();
|
let spawn_impulse = Quat::from_rotation_y(angle) * Vec3::new(0.5, 0.6, 0.).normalize() * 180.;
|
||||||
let spawn_force = if should_impulse {
|
|
||||||
spawn_dir * 180.0 / time.delta_secs()
|
|
||||||
} else {
|
|
||||||
Vec3::ZERO
|
|
||||||
};
|
|
||||||
|
|
||||||
if drop.impulse {
|
if drop.impulse {
|
||||||
commands.server_trigger(ToClients {
|
commands.server_trigger(ToClients {
|
||||||
@@ -55,11 +49,8 @@ fn on_head_drop(
|
|||||||
Collider::sphere(1.5),
|
Collider::sphere(1.5),
|
||||||
LockedAxes::ROTATION_LOCKED,
|
LockedAxes::ROTATION_LOCKED,
|
||||||
RigidBody::Dynamic,
|
RigidBody::Dynamic,
|
||||||
OneShotForce(spawn_force),
|
OneShotImpulse(spawn_impulse),
|
||||||
CollisionLayers::new(
|
CollisionLayers::new(GameLayer::CollectiblePhysics, GameLayer::Level),
|
||||||
GameLayer::CollectiblePhysics,
|
|
||||||
LayerMask::ALL & !GameLayer::Player.to_bits(),
|
|
||||||
),
|
|
||||||
Restitution::new(0.6),
|
Restitution::new(0.6),
|
||||||
Children::spawn(SpawnWith({
|
Children::spawn(SpawnWith({
|
||||||
let head_id = drop.head_id;
|
let head_id = drop.head_id;
|
||||||
|
|||||||
@@ -48,6 +48,10 @@ pub struct HeadCollected {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn plugin(app: &mut App) {
|
pub fn plugin(app: &mut App) {
|
||||||
|
app.register_type::<HeadDrop>();
|
||||||
|
app.register_type::<HeadDropEnableTime>();
|
||||||
|
app.register_type::<SecretHeadMarker>();
|
||||||
|
|
||||||
app.add_systems(
|
app.add_systems(
|
||||||
Update,
|
Update,
|
||||||
enable_collectible.run_if(in_state(GameState::Playing)),
|
enable_collectible.run_if(in_state(GameState::Playing)),
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use crate::{
|
|||||||
player::Player,
|
player::Player,
|
||||||
protocol::{GltfSceneRoot, PlaySound},
|
protocol::{GltfSceneRoot, PlaySound},
|
||||||
squish_animation::SquishAnimation,
|
squish_animation::SquishAnimation,
|
||||||
utils::one_shot_force::OneShotForce,
|
utils::one_shot_force::OneShotImpulse,
|
||||||
};
|
};
|
||||||
use avian3d::prelude::*;
|
use avian3d::prelude::*;
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
@@ -26,14 +26,13 @@ pub fn plugin(app: &mut App) {
|
|||||||
global_observer!(app, on_spawn_key);
|
global_observer!(app, on_spawn_key);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_spawn_key(trigger: On<KeySpawn>, mut commands: Commands, time: Res<Time<Fixed>>) {
|
fn on_spawn_key(trigger: On<KeySpawn>, mut commands: Commands) {
|
||||||
let KeySpawn(position, id) = trigger.event();
|
let KeySpawn(position, id) = trigger.event();
|
||||||
|
|
||||||
let id = id.clone();
|
let id = id.clone();
|
||||||
|
|
||||||
let angle = rand::random::<f32>() * PI * 2.;
|
let angle = rand::random::<f32>() * PI * 2.;
|
||||||
let spawn_dir = Quat::from_rotation_y(angle) * Vec3::new(0.5, 0.6, 0.).normalize();
|
let spawn_force = Quat::from_rotation_y(angle) * Vec3::new(0.5, 0.6, 0.).normalize() * 180.;
|
||||||
let spawn_force = spawn_dir * 180.0 / time.delta_secs();
|
|
||||||
|
|
||||||
commands
|
commands
|
||||||
.spawn((
|
.spawn((
|
||||||
@@ -44,7 +43,7 @@ fn on_spawn_key(trigger: On<KeySpawn>, mut commands: Commands, time: Res<Time<Fi
|
|||||||
Collider::sphere(1.5),
|
Collider::sphere(1.5),
|
||||||
LockedAxes::ROTATION_LOCKED,
|
LockedAxes::ROTATION_LOCKED,
|
||||||
RigidBody::Dynamic,
|
RigidBody::Dynamic,
|
||||||
OneShotForce(spawn_force),
|
OneShotImpulse(spawn_force),
|
||||||
CollisionLayers::new(GameLayer::CollectiblePhysics, GameLayer::Level),
|
CollisionLayers::new(GameLayer::CollectiblePhysics, GameLayer::Level),
|
||||||
Restitution::new(0.6),
|
Restitution::new(0.6),
|
||||||
Replicated,
|
Replicated,
|
||||||
|
|||||||
@@ -6,14 +6,14 @@ pub fn plugin(app: &mut App) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct OneShotForce(pub Vec3);
|
pub struct OneShotImpulse(pub Vec3);
|
||||||
|
|
||||||
pub fn apply_one_shot_forces(
|
pub fn apply_one_shot_forces(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut query: Query<(Entity, &OneShotForce, Forces)>,
|
mut query: Query<(Entity, &OneShotImpulse, Forces)>,
|
||||||
) {
|
) {
|
||||||
for (entity, force, mut forces) in query.iter_mut() {
|
for (entity, force, mut forces) in query.iter_mut() {
|
||||||
forces.apply_force(force.0);
|
forces.apply_linear_impulse(force.0);
|
||||||
commands.entity(entity).remove::<OneShotForce>();
|
commands.entity(entity).remove::<OneShotImpulse>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user