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