Player now inside playerEye and has collider

This commit is contained in:
Franklin 2023-11-17 10:41:36 -04:00
parent 3b3346ff3e
commit 7569b40fd7
8 changed files with 136 additions and 107 deletions

Binary file not shown.

View File

@ -1,8 +1,9 @@
use bevy::prelude::*;
use bevy_rapier3d::dynamics::RigidBody as RapierRigidBody;
// use bevy::render::primitives::Aabb;
use bevy_rapier3d::geometry::Collider as RapierCollider;
use bevy_rapier3d::prelude::{ActiveEvents, ComputedColliderShape};
use bevy_rapier3d::prelude::ComputedColliderShape;
use crate::comps::core::markers::player::Player;
use super::utils::*;
@ -33,86 +34,96 @@ pub enum AutoAABBCollider {
Capsule,
}
#[derive(Component, Reflect, Default, Debug)]
#[reflect(Component)]
pub struct LinkToPlayer;
// replaces all physics stand-ins with the actual rapier types
pub fn physics_replace_proxies(
meshes: Res<Assets<Mesh>>,
mesh_handles: Query<&Handle<Mesh>>,
mut proxy_colliders: Query<
(Entity, &Collider, &Name, &mut Visibility),
(Entity, &Collider, &Name, &mut Visibility, Option<&LinkToPlayer>, &Parent),
(Without<RapierCollider>, Added<Collider>),
>,
proxy_rigidbodies: Query<
(Entity, &RigidBodyBlender),
(Entity, &RigidBodyBlender, Option<&LinkToPlayer>, &Parent),
(Without<RapierRigidBody>, Added<RigidBodyBlender>),
>,
player_query: Query<(Entity, &Player)>,
// needed for tri meshes
children: Query<&Children>,
mut commands: Commands,
) {
for proxy_colider in proxy_colliders.iter_mut() {
let (entity, collider_proxy, name, mut visibility) = proxy_colider;
// we hide the collider meshes: perhaps they should be removed altogether once processed ?
if name.ends_with("_collider") || name.ends_with("_sensor") {
*visibility = Visibility::Hidden;
}
let mut rapier_collider: RapierCollider;
match collider_proxy {
Collider::Ball(radius) => {
println!("proxy: ball");
rapier_collider = RapierCollider::ball(*radius);
commands.entity(entity)
.insert(rapier_collider)
.insert(ActiveEvents::COLLISION_EVENTS) // FIXME: this is just for demo purposes !!!
;
for (player_entity, _) in player_query.iter() {
for proxy_colider in proxy_colliders.iter_mut() {
let (entity, collider_proxy, name, mut visibility, link_to_player_opt, _) = proxy_colider;
// we hide the collider meshes: perhaps they should be removed altogether once processed ?
if name.ends_with("_collider") || name.ends_with("_sensor") {
*visibility = Visibility::Hidden;
}
Collider::Cuboid(size) => {
println!("proxy: cuboid");
rapier_collider = RapierCollider::cuboid(size.x, size.y, size.z);
commands.entity(entity)
.insert(rapier_collider)
.insert(ActiveEvents::COLLISION_EVENTS) // FIXME: this is just for demo purposes !!!
;
}
Collider::Capsule(a, b, radius) => {
println!("proxy: capsule");
rapier_collider = RapierCollider::capsule(*a, *b, *radius);
commands.entity(entity)
.insert(rapier_collider)
.insert(ActiveEvents::COLLISION_EVENTS) // FIXME: this is just for demo purposes !!!
;
}
Collider::Mesh => {
for (_, collider_mesh) in
Mesh::search_in_children(entity, &children, &meshes, &mesh_handles)
{
println!("Mesh");
rapier_collider = RapierCollider::from_bevy_mesh(
collider_mesh,
&ComputedColliderShape::ConvexHull,
)
.unwrap();
commands
.entity(entity)
.insert(rapier_collider);
// .insert(ActiveEvents::COLLISION_EVENTS)
// break;
let entity_to_insert_collider_on = if link_to_player_opt.is_some() { player_entity } else { entity };
let mut rapier_collider: RapierCollider;
match collider_proxy {
Collider::Ball(radius) => {
println!("proxy: ball");
rapier_collider = RapierCollider::ball(*radius);
commands.entity(entity_to_insert_collider_on)
.insert(rapier_collider)
;
}
Collider::Cuboid(size) => {
println!("proxy: cuboid");
rapier_collider = RapierCollider::cuboid(size.x, size.y, size.z);
commands.entity(entity_to_insert_collider_on)
.insert(rapier_collider)
;
}
Collider::Capsule(a, b, radius) => {
println!("proxy: capsule");
rapier_collider = RapierCollider::capsule(*a, *b, *radius);
commands.entity(entity_to_insert_collider_on)
.insert(rapier_collider)
;
}
Collider::Mesh => {
for (_, collider_mesh) in
Mesh::search_in_children(entity, &children, &meshes, &mesh_handles)
{
println!("Mesh");
rapier_collider = RapierCollider::from_bevy_mesh(
collider_mesh,
&ComputedColliderShape::ConvexHull,
)
.unwrap();
//rapier_collider.set_scale(Vec3 { x: 3.0, y: 3.0, z: 3.0 }, 1);
commands
.entity(entity)
.insert(rapier_collider)
;
// .insert(ActiveEvents::COLLISION_EVENTS)
// break;
commands.entity(entity).remove::<Collider>();
}
}
}
}
}
for (entity, proxy_rigidbody) in proxy_rigidbodies.iter() {
println!("A");
let rapier_rigidbody: RapierRigidBody;
match proxy_rigidbody {
RigidBodyBlender::Fixed => rapier_rigidbody = RapierRigidBody::Fixed,
RigidBodyBlender::Dynamic => rapier_rigidbody = RapierRigidBody::Dynamic,
for (entity, proxy_rigidbody, link_to_player_opt, _) in proxy_rigidbodies.iter() {
let rapier_rigidbody: RapierRigidBody;
match proxy_rigidbody {
RigidBodyBlender::Fixed => rapier_rigidbody = RapierRigidBody::Fixed,
RigidBodyBlender::Dynamic => rapier_rigidbody = RapierRigidBody::Dynamic,
}
commands
.entity(
if link_to_player_opt.is_some() { player_entity } else { entity }
)
.insert(rapier_rigidbody)
;
commands.entity(entity).remove::<RigidBodyBlender>();
}
commands
.entity(entity)
.insert(rapier_rigidbody);
}
}

View File

@ -23,6 +23,7 @@ impl Plugin for ProxyComponentsPlugin {
app.register_type::<AutoAABBCollider>();
app.register_type::<physics::rapier::Collider>();
app.register_type::<physics::rapier::RigidBodyBlender>();
app.register_type::<physics::rapier::LinkToPlayer>();
app.add_systems(Update, (physics_replace_proxies, update_game_load_state));
}
}

View File

@ -6,7 +6,7 @@ use crate::{
inventory::player_inventory::PlayerInventory,
markers::{
camera::MainCamera,
player::{Player, PlayerData, PlayerHand},
player::{Player, PlayerData}, proxy::{character::player_eye::PlayerEye, physics::rapier::{LinkToPlayer, self}},
},
},
logic::core::{
@ -49,21 +49,21 @@ pub fn player_spawner(
}
for (player_spawn_point_entity, player_spawn_point) in player_sp_query.iter() {
let plyer_transform = Transform::from_xyz(25.0, 0.0, 25.0).with_scale(Vec3 { x: 3.0, y: 3.0, z: 3.0 });
commands.spawn(
//let plyer_transform = Transform::from_xyz(25.0, 0.0, 25.0).with_scale(Vec3 { x: 3.0, y: 3.0, z: 3.0 });
let player_scene = commands.spawn(
(
SceneBundle {
scene: loaded_gltf_assets.get(assets.assets[2].asset.clone()).unwrap().scenes[0].clone(),
transform: plyer_transform,
visibility: Visibility::Visible,
transform: Transform::from_translation(Vec3::ZERO).with_scale(Vec3 { x: 3.0, y: 3.0, z: 3.0 }),
visibility: Visibility::Inherited,
..Default::default()
},
Name::new("Player component")
Name::new("Player Scene Bundle")
)
);
).id();
// Spawn hand
let player_hand = commands
/*let player_hand = commands
.spawn((PlayerHand, Name::new("Player Hand")))
.insert(TransformBundle::from(Transform::from_xyz(0.6, -0.45, 0.0)))
.insert(VisibilityBundle {
@ -71,34 +71,15 @@ pub fn player_spawner(
..Default::default()
})
.id();
// Spawn camera
let camera = commands
.spawn(MainCamera)
.insert(Camera3dBundle {
transform: Transform::from_xyz(0.0, 0.0, 0.0).looking_at(Vec3::ZERO, Vec3::Y),
..Default::default()
})
//s.insert(Skybox(skybox_handle.clone()))
.insert(VisibilityBundle {
visibility: Visibility::Inherited,
..Default::default()
})
.push_children(&[player_hand])
.id();
*/
// Spawn player
commands
.spawn((Player(PlayerData::default()), Name::new("Player")))
// Physics
.insert(RigidBody::Dynamic)
//.insert(RigidBody::Dynamic)
.insert(GravityScale(player_values_state.player_gravity_scale))
.insert(Collider::capsule_y(player_values_state.player_height, 2.0))
.insert(Restitution::coefficient(0.0))
.insert(Friction {
coefficient: 0.0,
combine_rule: CoefficientCombineRule::Multiply,
})
//.insert(Collider::capsule_y(player_values_state.player_height, 2.0))
.insert(TransformBundle {
local: player_spawn_point.get_transform(),
..Default::default()
@ -113,9 +94,6 @@ pub fn player_spawner(
| LockedAxes::ROTATION_LOCKED_X
| LockedAxes::ROTATION_LOCKED_Y,
)
.insert(ColliderMassProperties::Mass(
player_values_state.player_initial_weight,
))
.insert(ExternalImpulse {
impulse: Vec3::ZERO,
torque_impulse: Vec3::ZERO,
@ -131,7 +109,7 @@ pub fn player_spawner(
// Data
.insert(PlayerFiringInfo::default())
.insert(PlayerInventory::default())
.push_children(&[camera]);
.add_child(player_scene);
game_load_state.player_loaded = true;
equipment_change_event_writer.send(EquipmentChangeEvent(
@ -140,3 +118,42 @@ pub fn player_spawner(
commands.entity(player_spawn_point_entity).despawn();
}
}
// Camera
pub fn insert_components_into_spawned_player(
mut commands: Commands,
eye_query: Query<Entity, Added<PlayerEye>>,
player_collider_query: Query<Entity, (With<LinkToPlayer>, With<Collider>, Added<Collider>)>,
player_values_state: Res<PlayerValuesState>,
) {
for eye in eye_query.iter() {
// Spawn camera
let camera = commands
.spawn(MainCamera)
.insert(Camera3dBundle {
transform: Transform::from_xyz(0.0, 0.0, 0.0).looking_at(Vec3::ZERO, Vec3::Y),
..Default::default()
})
//s.insert(Skybox(skybox_handle.clone()))
.insert(VisibilityBundle {
visibility: Visibility::Inherited,
..Default::default()
})
//.push_children(&[player_hand])
.id();
commands.entity(eye).add_child(camera);
}
for entity in player_collider_query.iter() {
commands.entity(entity)
.insert(Restitution::coefficient(0.0))
.insert(Friction {
coefficient: 0.0,
combine_rule: CoefficientCombineRule::Multiply,
})
.insert(ColliderMassProperties::Mass(
player_values_state.player_initial_weight,
))
;
}
}

View File

@ -2,7 +2,7 @@ use bevy::prelude::*;
use super::{
item::{item_spawner, ItemSpawnPointPlugin},
player::player_spawner,
player::{player_spawner, insert_components_into_spawned_player},
spawn_point::SpawnPointPlugin,
};
@ -10,6 +10,6 @@ pub struct SpawnerPlugin;
impl Plugin for SpawnerPlugin {
fn build(&self, app: &mut App) {
app.add_plugins((SpawnPointPlugin, ItemSpawnPointPlugin));
app.add_systems(Update, (player_spawner, item_spawner));
app.add_systems(Update, (player_spawner, item_spawner, insert_components_into_spawned_player.after(player_spawner)));
}
}

View File

@ -55,14 +55,14 @@ pub fn update_camera_vertical_position(
);
} else {
// TODO: Add elapsed time to standup so that crouch time and standup time is the same.
camera_transform.translation = camera_transform.translation.lerp(
/*camera_transform.translation = camera_transform.translation.lerp(
Vec3 {
x: camera_transform.translation.x,
y: player_values_state.player_camera_height,
z: camera_transform.translation.z,
},
time.delta_seconds().clamp(0.0, 1.0),
);
);*/
}
}
@ -149,7 +149,7 @@ pub fn follow_cursor_with_camera(
pitch = pitch.clamp(-1.54, 1.54);
let desired_rotation_quat =
Quat::from_axis_angle(Vec3::Y, yaw) * Quat::from_axis_angle(Vec3::X, pitch);
(Quat::from_axis_angle(Vec3::Y, yaw) * -1. )* Quat::from_axis_angle(Vec3::X, pitch);
for mut camera_transform in camera_query.iter_mut() {
let local_z = camera_transform.local_z();

View File

@ -10,7 +10,7 @@ use crate::{
firearm::{FirearmData, MagazineData},
holdable::InPlayerHands,
interactable::Interactable,
player::{Player, PlayerHand},
player::Player, proxy::character::in_player_hands_parent::InPlayerHandsParent,
},
},
logic::core::guns::{player_firing::PlayerFiringInfo, shoot::shoot_bullet},
@ -43,7 +43,7 @@ pub fn capture_hand_usage(
mut resources: CaptureHandUsageResourcesParams,
mut commands: Commands,
mut hand_query: Query<&mut Transform, (With<PlayerHand>, Without<Player>)>,
mut hand_query: Query<&mut Transform, (With<InPlayerHandsParent>, Without<Player>)>,
mut firearm_query: Query<
(
Entity,
@ -51,7 +51,7 @@ pub fn capture_hand_usage(
&'static FirearmData,
&mut MagazineData,
),
(With<InPlayerHands>, Without<PlayerHand>),
(With<InPlayerHands>, Without<InPlayerHandsParent>),
>,
mut player_query: Query<(&Player, &mut PlayerInventory, Entity, &Transform, &mut PlayerFiringInfo)>,
mut animation_players: Query<(Entity, &mut AnimationPlayer)>,

View File

@ -6,7 +6,7 @@ use crate::{
comps::core::markers::{
firearm::{FirearmData, MagazineData},
holdable::InPlayerHands,
player::{Player, PlayerHand},
player::Player, proxy::character::in_player_hands_parent::InPlayerHandsParent,
},
logic::core::guns::{firearm::Firearm, player_firing::PlayerFiringInfo},
utils,
@ -29,7 +29,7 @@ pub fn change_equipment(
mut commands: Commands,
mut equipment_change_event_reader: EventReader<EquipmentChangeEvent>,
mut player_query: Query<(&mut Player, &mut PlayerFiringInfo)>,
mut player_hands_query: Query<Entity, With<PlayerHand>>,
mut player_hands_query: Query<Entity, With<InPlayerHandsParent>>,
assets_gltf: Res<GltfAssets>,
loaded_gltf_assets: Res<Assets<Gltf>>,