re-org bike mod, optimize external deps for fast startup
This commit is contained in:
parent
6031eeab5c
commit
e4c491768c
2 changed files with 98 additions and 61 deletions
|
@ -10,3 +10,6 @@ bevy-inspector-egui = "0.25.1"
|
|||
|
||||
[features]
|
||||
no-mesh = []
|
||||
|
||||
[profile.dev.package."*"]
|
||||
opt-level = 3
|
||||
|
|
140
src/bike.rs
140
src/bike.rs
|
@ -8,6 +8,21 @@ use crate::physics::CatControllerState;
|
|||
#[derive(Component)]
|
||||
pub struct CyberBikeBody;
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct CyberWheel;
|
||||
|
||||
// marker for front suspension joint
|
||||
#[derive(Component)]
|
||||
pub struct Steering;
|
||||
#[derive(Component)]
|
||||
pub struct FrontHub;
|
||||
|
||||
// marker for rear suspension joint
|
||||
#[derive(Component)]
|
||||
pub struct Rearing;
|
||||
#[derive(Component)]
|
||||
pub struct RearHub;
|
||||
|
||||
fn spawn_bike(
|
||||
mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
|
@ -53,27 +68,53 @@ fn spawn_bike(
|
|||
spawn_wheels(commands, meshes, materials, xform, bike);
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct CyberWheel;
|
||||
fn spawn_wheels(
|
||||
mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
xform: Transform,
|
||||
body: Entity,
|
||||
) {
|
||||
let rake_vec = Vec3::new(0.0, -1.0, -0.57).normalize(); // about 30 degrees
|
||||
let front_pos = xform.translation + rake_vec;
|
||||
|
||||
// marker for front suspension joint
|
||||
#[derive(Component)]
|
||||
pub struct Steering;
|
||||
#[derive(Component)]
|
||||
pub struct FrontHub;
|
||||
let (mesh, collider) = gen_tire();
|
||||
|
||||
// marker for rear suspension joint
|
||||
#[derive(Component)]
|
||||
pub struct Rearing;
|
||||
#[derive(Component)]
|
||||
pub struct RearHub;
|
||||
let front_hub = wheels_helper(
|
||||
&mut commands,
|
||||
&mut meshes,
|
||||
&mut materials,
|
||||
front_pos,
|
||||
mesh.clone(),
|
||||
collider.clone(),
|
||||
);
|
||||
commands.entity(front_hub).insert(FrontHub);
|
||||
commands.spawn((
|
||||
Steering,
|
||||
FixedJoint::new(front_hub, body).with_local_anchor_2(*xform.forward() + rake_vec),
|
||||
));
|
||||
|
||||
fn helper(
|
||||
commands: &mut Commands,
|
||||
meshes: &mut ResMut<Assets<Mesh>>,
|
||||
materials: &mut ResMut<Assets<StandardMaterial>>,
|
||||
position: Vec3,
|
||||
) -> Entity {
|
||||
let rear_pos = xform.translation + Vec3::new(0.0, -1.0, 0.57).normalize();
|
||||
let rear_hub = wheels_helper(
|
||||
&mut commands,
|
||||
&mut meshes,
|
||||
&mut materials,
|
||||
rear_pos,
|
||||
mesh,
|
||||
collider,
|
||||
);
|
||||
commands.entity(rear_hub).insert(RearHub);
|
||||
commands.spawn((
|
||||
Rearing,
|
||||
FixedJoint::new(rear_hub, body).with_local_anchor_2(*xform.back() + rake_vec.y),
|
||||
));
|
||||
}
|
||||
|
||||
//-************************************************************************
|
||||
// helper fns for the wheels
|
||||
//-************************************************************************
|
||||
|
||||
fn gen_tire() -> (Mesh, Collider) {
|
||||
let mut tire_mesh: Mesh = Torus::new(0.30, 0.40).into();
|
||||
let tire_verts = tire_mesh
|
||||
.attribute(Mesh::ATTRIBUTE_POSITION)
|
||||
|
@ -91,15 +132,26 @@ fn helper(
|
|||
.collect::<Vec<[f32; 3]>>();
|
||||
tire_mesh.remove_attribute(Mesh::ATTRIBUTE_POSITION);
|
||||
tire_mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, tire_verts);
|
||||
let wheel_material = StandardMaterial {
|
||||
|
||||
let collider = Collider::convex_decomposition_from_mesh(&tire_mesh).unwrap();
|
||||
|
||||
(tire_mesh, collider)
|
||||
}
|
||||
|
||||
fn wheels_helper(
|
||||
commands: &mut Commands,
|
||||
meshes: &mut ResMut<Assets<Mesh>>,
|
||||
materials: &mut ResMut<Assets<StandardMaterial>>,
|
||||
position: Vec3,
|
||||
tire_mesh: Mesh,
|
||||
collider: Collider,
|
||||
) -> Entity {
|
||||
let wheel_material = &StandardMaterial {
|
||||
base_color: Color::srgb(0.01, 0.01, 0.01),
|
||||
alpha_mode: AlphaMode::Opaque,
|
||||
perceptual_roughness: 0.5,
|
||||
..Default::default()
|
||||
};
|
||||
let collider = Collider::convex_decomposition_from_mesh(&tire_mesh).unwrap();
|
||||
let mut tire = None;
|
||||
let tref = &mut tire;
|
||||
|
||||
let xform = Transform::from_translation(position);
|
||||
|
||||
|
@ -109,15 +161,19 @@ fn helper(
|
|||
RigidBody::Dynamic,
|
||||
MassPropertiesBundle::new_computed(&Collider::sphere(0.1), 0.01),
|
||||
))
|
||||
.with_children(move |parent| {
|
||||
.with_children(|parent| {
|
||||
let mesh: Mesh = Sphere::new(0.1).into();
|
||||
parent.spawn(PbrBundle {
|
||||
mesh: meshes.add(mesh),
|
||||
material: materials.add(wheel_material.clone()),
|
||||
..Default::default()
|
||||
});
|
||||
let tire_id = parent
|
||||
})
|
||||
.id();
|
||||
|
||||
let tire = commands
|
||||
.spawn((
|
||||
SpatialBundle::from_transform(xform),
|
||||
CyberWheel,
|
||||
RigidBody::Dynamic,
|
||||
collider.clone(),
|
||||
|
@ -125,42 +181,20 @@ fn helper(
|
|||
CollisionLayers::from_bits(2, 2),
|
||||
ExternalTorque::ZERO.with_persistence(false),
|
||||
))
|
||||
.id();
|
||||
let _ = tref.insert(tire_id);
|
||||
.with_children(|p| {
|
||||
p.spawn(PbrBundle {
|
||||
mesh: meshes.add(tire_mesh),
|
||||
material: materials.add(wheel_material.clone()),
|
||||
..Default::default()
|
||||
});
|
||||
})
|
||||
.id();
|
||||
|
||||
// connect hubs and tires to make wheels
|
||||
commands.spawn(RevoluteJoint::new(hub, tire.unwrap()).with_aligned_axis(Vec3::X));
|
||||
commands.spawn(RevoluteJoint::new(hub, tire).with_aligned_axis(Vec3::X));
|
||||
hub
|
||||
}
|
||||
|
||||
fn spawn_wheels(
|
||||
mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
xform: Transform,
|
||||
body: Entity,
|
||||
) {
|
||||
let rake_vec = Vec3::new(0.0, -1.0, -0.57).normalize(); // about 30 degrees
|
||||
let front_pos = xform.translation + rake_vec;
|
||||
|
||||
let front_hub = helper(&mut commands, &mut meshes, &mut materials, front_pos);
|
||||
commands.entity(front_hub).insert(FrontHub);
|
||||
commands.spawn((
|
||||
Steering,
|
||||
FixedJoint::new(front_hub, body).with_local_anchor_2(*xform.forward() + rake_vec),
|
||||
));
|
||||
|
||||
let rear_pos = xform.translation + Vec3::new(0.0, -1.0, 0.57).normalize();
|
||||
let rear_hub = helper(&mut commands, &mut meshes, &mut materials, rear_pos);
|
||||
commands.entity(rear_hub).insert(RearHub);
|
||||
commands.spawn((
|
||||
Rearing,
|
||||
FixedJoint::new(rear_hub, body).with_local_anchor_2(*xform.back() + rake_vec.y),
|
||||
));
|
||||
}
|
||||
|
||||
pub struct CyberBikePlugin;
|
||||
|
||||
impl Plugin for CyberBikePlugin {
|
||||
|
|
Loading…
Reference in a new issue