cleanup (remove unnecessary scope block, unindenting by 1)

(I had this scope block before in order to release ownership of some
variable so that it can be used again after the scope block, but seems
like this is not necessary anymore by now)
This commit is contained in:
yuni 2024-07-12 14:22:56 +02:00
parent eb094945f2
commit 4f787c33e8

View file

@ -840,223 +840,221 @@ fn spawn_entities(
// Spawn the actor // Spawn the actor
let actor_entity; let actor_entity;
{ let mut actor = commands.spawn_empty();
let mut actor = commands.spawn_empty(); actor.insert(actor::Actor {
actor.insert(actor::Actor { id: state.id.clone(),
id: state.id.clone(), name: state.name.clone(),
name: state.name.clone(), camdistance: state.camdistance,
camdistance: state.camdistance, ..default()
});
actor.insert(SleepingDisabled);
if orbits_jupiter {
actor.insert(actor::OrbitsJupiter);
}
actor.insert(world::DespawnOnPlayerDeath);
actor.insert(actor::HitPoints::default());
actor.insert(Position::from(absolute_pos));
if state.is_sphere {
let sphere_texture_handle = if let Some(model) = &state.model {
Some(asset_server.load(format!("textures/{}.jpg", model)))
} else {
None
};
rotation = Quat::from_rotation_x(-90f32.to_radians()) * rotation;
let sphere_handle = meshes.add(Sphere::new(1.0).mesh().uv(128, 128));
let sphere_material_handle = materials.add(StandardMaterial {
base_color_texture: sphere_texture_handle,
perceptual_roughness: 1.0,
metallic: 0.0,
..default() ..default()
}); });
actor.insert(SleepingDisabled); actor.insert(PbrBundle {
if orbits_jupiter { mesh: sphere_handle,
actor.insert(actor::OrbitsJupiter); material: sphere_material_handle,
} transform: Transform::from_scale(scale),
actor.insert(world::DespawnOnPlayerDeath); ..default()
actor.insert(actor::HitPoints::default()); });
actor.insert(Position::from(absolute_pos)); } else if let Some(model) = &state.model {
if state.is_sphere { actor.insert(SpatialBundle {
let sphere_texture_handle = if let Some(model) = &state.model { transform: Transform::from_scale(scale),
Some(asset_server.load(format!("textures/{}.jpg", model))) ..default()
} else { });
None load_asset(model.as_str(), &mut actor, &*asset_server);
}; }
rotation = Quat::from_rotation_x(-90f32.to_radians()) * rotation; actor.insert(Rotation::from(rotation));
let sphere_handle = meshes.add(Sphere::new(1.0).mesh().uv(128, 128));
let sphere_material_handle = materials.add(StandardMaterial {
base_color_texture: sphere_texture_handle,
perceptual_roughness: 1.0,
metallic: 0.0,
..default()
});
actor.insert(PbrBundle {
mesh: sphere_handle,
material: sphere_material_handle,
transform: Transform::from_scale(scale),
..default()
});
} else if let Some(model) = &state.model {
actor.insert(SpatialBundle {
transform: Transform::from_scale(scale),
..default()
});
load_asset(model.as_str(), &mut actor, &*asset_server);
}
actor.insert(Rotation::from(rotation));
// Physics Parameters // Physics Parameters
if state.has_physics { if state.has_physics {
actor.insert(RigidBody::Dynamic); actor.insert(RigidBody::Dynamic);
actor.insert(LinearVelocity(velocity)); actor.insert(LinearVelocity(velocity));
actor.insert(AngularVelocity(state.angular_momentum)); actor.insert(AngularVelocity(state.angular_momentum));
actor.insert(ColliderDensity(state.density)); actor.insert(ColliderDensity(state.density));
if state.collider_is_mesh { if state.collider_is_mesh {
actor.insert(MassPropertiesBundle::new_computed( actor.insert(MassPropertiesBundle::new_computed(
&Collider::sphere(0.5 * state.model_scale as f64), &Collider::sphere(0.5 * state.model_scale as f64),
state.density, state.density,
));
actor.insert(AsyncSceneCollider::new(Some(
ComputedCollider::TriMesh, //ComputedCollider::ConvexDecomposition(VHACDParameters::default())
)));
} else if state.collider_is_one_mesh_of_scene {
actor.insert(MassPropertiesBundle::new_computed(
&Collider::sphere(0.5 * state.model_scale as f64),
state.density,
));
actor.insert(
AsyncSceneCollider::new(None)
.with_shape_for_name("Collider", ComputedCollider::TriMesh)
.with_layers_for_name("Collider", CollisionLayers::ALL), //.with_density_for_name("Collider", state.density)
);
actor.insert(NeedsSceneColliderRemoved);
} else {
actor.insert(state.collider.clone());
}
}
// TODO: angular velocity for objects without collisions, static objects
// Optional Components
if state.is_player {
actor.insert(actor::Player);
actor.insert(actor::PlayerCamera);
actor.insert(hud::AugmentedRealityOverlayBroadcaster);
ew_updateavatar.send(hud::UpdateAvatarEvent);
}
if state.is_sun {
let (r, g, b) = nature::star_color_index_to_rgb(0.656);
actor.insert(materials.add(StandardMaterial {
base_color: Color::srgb(r * 13.0, g * 13.0, b * 13.0),
unlit: true,
..default()
}));
actor.insert((NotShadowCaster, NotShadowReceiver));
}
if state.is_targeted_on_startup {
actor.insert(hud::IsTargeted);
}
if let Some((mindist, id)) = &state.show_only_in_map_at_distance {
actor.insert(camera::ShowOnlyInMap {
min_distance: *mindist,
distance_to_id: id.clone(),
});
}
if state.is_player || state.is_vehicle {
// used to apply mouse movement to actor rotation
actor.insert(ExternalTorque::ZERO.with_persistence(false));
}
if state.is_lifeform {
actor.insert(actor::LifeForm::default());
actor.insert(actor::ExperiencesGForce::default());
actor.insert(actor::Suit {
oxygen: state.oxygen,
oxygen_max: nature::OXY_D,
integrity: state.suit_integrity,
..default()
});
actor.insert(actor::Battery::default());
}
if state.is_clickable {
actor.insert(hud::IsClickable {
name: state.name.clone(),
pronoun: state.pronoun.clone(),
..default()
});
}
if let Some(value) = state.wants_maxrotation {
actor.insert(actor::WantsMaxRotation(value));
}
if let Some(value) = state.wants_maxvelocity {
actor.insert(actor::WantsMaxVelocity(value));
}
if let Some(value) = &state.wants_tolookat_id {
actor.insert(actor::WantsToLookAt(value.clone()));
}
if let Some(value) = &state.wants_matchvelocity_id {
actor.insert(actor::WantsMatchVelocityWith(value.clone()));
}
if let Some(color) = state.light_color {
actor.insert((
PointLight {
intensity: state.light_brightness,
color,
range: 2000.0,
shadows_enabled: settings.shadows_pointlights,
..default()
},
bevy::pbr::CubemapVisibleEntities::default(),
bevy::render::primitives::CubemapFrusta::default(),
)); ));
actor.insert(AsyncSceneCollider::new(Some(
ComputedCollider::TriMesh, //ComputedCollider::ConvexDecomposition(VHACDParameters::default())
)));
} else if state.collider_is_one_mesh_of_scene {
actor.insert(MassPropertiesBundle::new_computed(
&Collider::sphere(0.5 * state.model_scale as f64),
state.density,
));
actor.insert(
AsyncSceneCollider::new(None)
.with_shape_for_name("Collider", ComputedCollider::TriMesh)
.with_layers_for_name("Collider", CollisionLayers::ALL), //.with_density_for_name("Collider", state.density)
);
actor.insert(NeedsSceneColliderRemoved);
} else {
actor.insert(state.collider.clone());
} }
if !state.id.is_empty() { }
actor.insert(actor::Identifier(state.id.clone())); // TODO: angular velocity for objects without collisions, static objects
id2pos.0.insert(state.id.clone(), absolute_pos);
} // Optional Components
if !state.chat.is_empty() { if state.is_player {
actor.insert(chat::Talker { actor.insert(actor::Player);
actor_id: state.id.clone(), actor.insert(actor::PlayerCamera);
chat_name: state.chat.clone(), actor.insert(hud::AugmentedRealityOverlayBroadcaster);
name: state.name.clone(), ew_updateavatar.send(hud::UpdateAvatarEvent);
pronoun: state.pronoun.clone(), }
talking_speed: 1.0, if state.is_sun {
}); let (r, g, b) = nature::star_color_index_to_rgb(0.656);
if let Some(name) = &state.name { actor.insert(materials.add(StandardMaterial {
achievement_tracker.all_people.insert(name.clone()); base_color: Color::srgb(r * 13.0, g * 13.0, b * 13.0),
} unlit: true,
} ..default()
if state.is_vehicle { }));
actor.insert(actor::Vehicle::default()); actor.insert((NotShadowCaster, NotShadowReceiver));
if let Some(name) = &state.name { }
achievement_tracker.all_vehicles.insert(name.clone()); if state.is_targeted_on_startup {
} actor.insert(hud::IsTargeted);
} }
if state.is_vehicle if let Some((mindist, id)) = &state.show_only_in_map_at_distance {
|| state.is_suited actor.insert(camera::ShowOnlyInMap {
|| state.thrust_forward > 0.0 min_distance: *mindist,
|| state.thrust_sideways > 0.0 distance_to_id: id.clone(),
|| state.thrust_back > 0.0 });
|| state.reaction_wheels > 0.0 }
{ if state.is_player || state.is_vehicle {
actor.insert(actor::Engine { // used to apply mouse movement to actor rotation
thrust_forward: state.thrust_forward, actor.insert(ExternalTorque::ZERO.with_persistence(false));
thrust_back: state.thrust_back, }
thrust_sideways: state.thrust_sideways, if state.is_lifeform {
reaction_wheels: state.reaction_wheels, actor.insert(actor::LifeForm::default());
warmup_seconds: state.warmup_seconds, actor.insert(actor::ExperiencesGForce::default());
engine_type: state.engine_type, actor.insert(actor::Suit {
oxygen: state.oxygen,
oxygen_max: nature::OXY_D,
integrity: state.suit_integrity,
..default()
});
actor.insert(actor::Battery::default());
}
if state.is_clickable {
actor.insert(hud::IsClickable {
name: state.name.clone(),
pronoun: state.pronoun.clone(),
..default()
});
}
if let Some(value) = state.wants_maxrotation {
actor.insert(actor::WantsMaxRotation(value));
}
if let Some(value) = state.wants_maxvelocity {
actor.insert(actor::WantsMaxVelocity(value));
}
if let Some(value) = &state.wants_tolookat_id {
actor.insert(actor::WantsToLookAt(value.clone()));
}
if let Some(value) = &state.wants_matchvelocity_id {
actor.insert(actor::WantsMatchVelocityWith(value.clone()));
}
if let Some(color) = state.light_color {
actor.insert((
PointLight {
intensity: state.light_brightness,
color,
range: 2000.0,
shadows_enabled: settings.shadows_pointlights,
..default() ..default()
}); },
bevy::pbr::CubemapVisibleEntities::default(),
bevy::render::primitives::CubemapFrusta::default(),
));
}
if !state.id.is_empty() {
actor.insert(actor::Identifier(state.id.clone()));
id2pos.0.insert(state.id.clone(), absolute_pos);
}
if !state.chat.is_empty() {
actor.insert(chat::Talker {
actor_id: state.id.clone(),
chat_name: state.chat.clone(),
name: state.name.clone(),
pronoun: state.pronoun.clone(),
talking_speed: 1.0,
});
if let Some(name) = &state.name {
achievement_tracker.all_people.insert(name.clone());
} }
if let Some(_) = state.ar_model { }
actor.insert(hud::AugmentedRealityOverlayBroadcaster); if state.is_vehicle {
actor.insert(actor::Vehicle::default());
if let Some(name) = &state.name {
achievement_tracker.all_vehicles.insert(name.clone());
} }
if state.is_player { }
actor.with_children(|builder| { if state.is_vehicle
builder.spawn(( || state.is_suited
world::DespawnOnPlayerDeath, || state.thrust_forward > 0.0
actor::PlayersFlashLight, || state.thrust_sideways > 0.0
SpotLightBundle { || state.thrust_back > 0.0
transform: Transform { || state.reaction_wheels > 0.0
translation: Vec3::new(0.0, 0.0, 1.0), {
rotation: Quat::from_rotation_y(180f32.to_radians()), actor.insert(actor::Engine {
..default() thrust_forward: state.thrust_forward,
}, thrust_back: state.thrust_back,
spot_light: SpotLight { thrust_sideways: state.thrust_sideways,
intensity: 40_000_000.0, // lumens reaction_wheels: state.reaction_wheels,
color: Color::WHITE, warmup_seconds: state.warmup_seconds,
shadows_enabled: true, engine_type: state.engine_type,
inner_angle: PI32 / 8.0 * 0.85, ..default()
outer_angle: PI32 / 4.0, });
range: 2000.0, }
..default() if let Some(_) = state.ar_model {
}, actor.insert(hud::AugmentedRealityOverlayBroadcaster);
visibility: Visibility::Hidden, }
if state.is_player {
actor.with_children(|builder| {
builder.spawn((
world::DespawnOnPlayerDeath,
actor::PlayersFlashLight,
SpotLightBundle {
transform: Transform {
translation: Vec3::new(0.0, 0.0, 1.0),
rotation: Quat::from_rotation_y(180f32.to_radians()),
..default() ..default()
}, },
)); spot_light: SpotLight {
}); intensity: 40_000_000.0, // lumens
} color: Color::WHITE,
actor_entity = actor.id(); shadows_enabled: true,
inner_angle: PI32 / 8.0 * 0.85,
outer_angle: PI32 / 4.0,
range: 2000.0,
..default()
},
visibility: Visibility::Hidden,
..default()
},
));
});
} }
actor_entity = actor.id();
if let Some(ar_asset_name) = &state.ar_model { if let Some(ar_asset_name) = &state.ar_model {
let mut entitycmd = commands.spawn(( let mut entitycmd = commands.spawn((