ZTXU3KDWJAP47BPTRDJGQSR3C2YNKJE5NKFBYZKXR7N6LWGJWC7QC
use bevy_egui::EguiContexts;
use bevy_tnua::{
builtins::{TnuaBuiltinJump, TnuaBuiltinWalk},
control_helpers::TnuaSimpleAirActionsCounter,
controller::{TnuaController, TnuaControllerBundle, TnuaControllerPlugin},
TnuaAction,
};
use bevy_tnua_xpbd2d::{TnuaXpbd2dPlugin, TnuaXpbd2dSensorShape};
use bevy_xpbd_2d::prelude::*;
use leafwing_input_manager::prelude::*;
}
#[derive(Actionlike, PartialEq, Eq, Hash, Clone, Copy, Debug, Reflect)]
enum PlayerAction {
Walk,
Jump,
}
#[derive(Component)]
struct Player;
fn default_player_map() -> InputMap<PlayerAction> {
InputMap::new([(PlayerAction::Jump, KeyCode::Space)])
.insert(PlayerAction::Walk, VirtualAxis::horizontal_arrow_keys())
.to_owned()
}
#[derive(Component)]
struct TnuaConfig {
max_speed: f32,
air_actions: usize,
walk: TnuaBuiltinWalk,
jump: TnuaBuiltinJump,
RigidBody::Dynamic,
Collider::capsule(32.0, 16.0),
// Restitution::new(0.7),
InputManagerBundle::with_map(default_player_map()),
TnuaConfig {
max_speed: 5000.,
air_actions: 2,
walk: TnuaBuiltinWalk {
acceleration: 2000.,
air_acceleration: 1500.,
float_height: 64.0,
..default()
},
jump: TnuaBuiltinJump {
height: 128.,
reschedule_cooldown: Some(0.1),
// Defaults x 10
upslope_extra_gravity: 300.0,
// takeoff_extra_gravity: 300.0,
// takeoff_above_velocity: 64.0,
fall_extra_gravity: 200.0,
shorten_extra_gravity: 600.0,
peak_prevention_at_upward_velocity: 10.0,
peak_prevention_extra_gravity: 200.0,
..default()
},
},
Mass(1.0),
TnuaControllerBundle::default(),
TnuaXpbd2dSensorShape(Collider::circle(32.0)),
TnuaSimpleAirActionsCounter::default(),
..default()
},
));
commands.spawn((
camera::CameraTarget(1),
RigidBody::Dynamic,
Collider::capsule(32.0, 32.0),
Restitution::new(1.0),
MaterialMesh2dBundle {
mesh: Mesh2dHandle(meshes.add(Capsule2d::new(32.0, 32.0))),
material: materials.add(Color::hsl(10., 0.95, 0.5)),
transform: Transform::from_xyz(320.0, 640.0, 0.0),
..default()
},
));
commands.spawn((
camera::CameraTarget(2),
RigidBody::Static,
Collider::rectangle(1000.0, 100.0),
TransformBundle::from(Transform::from_xyz(0.0, -100.0, 0.0)),
));
commands.spawn((
camera::CameraTarget(3),
MaterialMesh2dBundle {
mesh: Mesh2dHandle(meshes.add(Circle { radius: 64.0 })),
material: materials.add(Color::hsl(180., 0.95, 0.7)),
transform: Transform::from_xyz(-320.0, -640.0, 0.0),
}
fn update_test_stuff(
mut query: Query<
(
&ActionState<PlayerAction>,
&mut TnuaController,
&TnuaConfig,
&mut TnuaSimpleAirActionsCounter,
),
With<Player>,
>,
) {
let (action_state, mut tnua_controller, config, mut air_counter) = query.single_mut();
let mov_dir = action_state.clamped_value(&PlayerAction::Walk);
let direction = mov_dir * Vec3::X;
air_counter.update(tnua_controller.as_mut());
let speed_factor = 1.0;
tnua_controller.basis(TnuaBuiltinWalk {
desired_velocity: direction * speed_factor * config.max_speed,
// TODO Hanel shooting maybe?
desired_forward: direction.normalize_or_zero(),
..config.walk
});
if action_state.just_pressed(&PlayerAction::Jump) {
tnua_controller.action(TnuaBuiltinJump {
allow_in_air: air_counter.air_count_for(TnuaBuiltinJump::NAME) <= config.air_actions,
..config.jump
});
}
]
[[package]]
name = "bevy-tnua"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fdb7fe0e9b2099555dd0b58d53789eb39d049f730a9895dd4c90c35449f19ae"
dependencies = [
"bevy",
"bevy-tnua-physics-integration-layer",
"thiserror",
]
[[package]]
name = "bevy-tnua-physics-integration-layer"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3924097b24a5411d31de920521d0703be1db6d12c67156c7149d194e3e2ddf68"
dependencies = [
"bevy",
]
[[package]]
name = "bevy-tnua-xpbd2d"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9c4d5cb0dc7be0b40640a01f1cf6ae2b15f58031f00f6639b54c2a8a08a11ec"
dependencies = [
"bevy",
"bevy-tnua-physics-integration-layer",
"bevy_xpbd_2d",
]
[[package]]
name = "bevy_xpbd_2d"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "627b7f58d53fc60ab233225e14b1357af6838623e47bd0f5c6961337a5b07b5f"
dependencies = [
"bevy",
"bevy_math",
"bevy_xpbd_derive",
"derive_more",
"fxhash",
"indexmap",
"itertools",
"nalgebra",
"parry2d",
"parry2d-f64",
]
[[package]]
name = "bevy_xpbd_derive"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e1ef1d5e328abe1b76df974245f78e17fd17867583883d5e77444c6a8223a64"
dependencies = [
"quote",
"syn 2.0.59",
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d"
dependencies = [
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
]
[[package]]
name = "nalgebra"
version = "0.32.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ea4908d4f23254adda3daa60ffef0f1ac7b8c3e9a864cf3cc154b251908a2ef"
dependencies = [
"approx",
"glam",
"matrixmultiply",
"nalgebra-macros",
"num-complex",
"num-rational",
"num-traits",
"simba",
"typenum",
]
[[package]]
name = "nalgebra-macros"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91761aed67d03ad966ef783ae962ef9bbaca728d2dd7ceb7939ec110fffad998"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "num-integer"
version = "0.1.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f"
dependencies = [
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "parry2d"
version = "0.13.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a996b83a7a12522b395c54f4ba59593b782e8004794c0212a2f487ed7ac6e419"
dependencies = [
"approx",
"arrayvec",
"bitflags 1.3.2",
"downcast-rs",
"either",
"nalgebra",
"num-derive",
"num-traits",
"rayon",
"rustc-hash",
"simba",
"slab",
"smallvec",
"spade",
]
[[package]]
name = "parry2d-f64"
version = "0.13.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5565e2f28bad470868b5e5c146fd0e2716345b67d8b9f27e0fc147a1dd4790b5"
dependencies = [
"approx",
"arrayvec",
"bitflags 1.3.2",
"downcast-rs",
"either",
"nalgebra",
"num-derive",
"num-traits",
"rayon",
"rustc-hash",
"simba",
"slab",
"smallvec",
"spade",
name = "rawpointer"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3"
[[package]]
name = "rayon"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2"
dependencies = [
"crossbeam-deque",
"crossbeam-utils",
]
[[package]]