aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/fparkan-runtime/src')
-rw-r--r--crates/fparkan-runtime/src/debris_motion.rs85
-rw-r--r--crates/fparkan-runtime/src/destruction.rs14
-rw-r--r--crates/fparkan-runtime/src/lib.rs2
-rw-r--r--crates/fparkan-runtime/src/unit_combat.rs21
4 files changed, 112 insertions, 10 deletions
diff --git a/crates/fparkan-runtime/src/debris_motion.rs b/crates/fparkan-runtime/src/debris_motion.rs
new file mode 100644
index 00000000..286bc2b1
--- /dev/null
+++ b/crates/fparkan-runtime/src/debris_motion.rs
@@ -0,0 +1,85 @@
+//! Ballistic presentation of a detached mesh assembly.
+
+use fparkan_animation::Pose;
+
+const GRAVITY: f32 = 9.81;
+
+/// A visual fragment travelling from its last attached center.
+pub struct DebrisMotion {
+ center: [f32; 3],
+ outward: [f32; 2],
+ launch_speed: f32,
+ angular_speed: f32,
+ landing_seconds: f32,
+}
+
+impl DebrisMotion {
+ /// Launches a fragment outward with enough energy to lift it one radius.
+ pub fn new(center: [f32; 3], hull: [f32; 3], radius: f32, floor_center: f32) -> Option<Self> {
+ if !center
+ .into_iter()
+ .chain(hull)
+ .chain([radius, floor_center])
+ .all(f32::is_finite)
+ || radius <= 0.0
+ {
+ return None;
+ }
+ let [x, y] = [center[0] - hull[0], center[1] - hull[1]];
+ let length = x.hypot(y);
+ let outward = if length > f32::EPSILON {
+ [x / length, y / length]
+ } else {
+ [0.0, 1.0]
+ };
+ let launch_speed = (2.0 * GRAVITY * radius).sqrt();
+ let height = (center[2] - floor_center).max(0.0);
+ let landing_seconds =
+ (launch_speed + (launch_speed.powi(2) + 2.0 * GRAVITY * height).sqrt()) / GRAVITY;
+ Some(Self {
+ center,
+ outward,
+ launch_speed,
+ angular_speed: launch_speed / radius,
+ landing_seconds,
+ })
+ }
+
+ /// Returns the new center and rotation. Rotation and travel stop at landing.
+ pub fn pose(&self, elapsed_seconds: f32) -> Pose {
+ let time = elapsed_seconds.clamp(0.0, self.landing_seconds);
+ let travel = self.launch_speed * time;
+ let [x, y] = self.outward;
+ let (sin, cos) = (0.5 * self.angular_speed * time).sin_cos();
+ Pose {
+ translation: [
+ self.center[0] + x * travel,
+ self.center[1] + y * travel,
+ self.center[2] + travel - 0.5 * GRAVITY * time.powi(2),
+ ],
+ rotation: [y * sin, -x * sin, 0.0, cos],
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn fragment_begins_attached_and_stops_on_landing() {
+ let center = [2.0, 5.0, 4.0];
+ let motion = DebrisMotion::new(center, [0.0, 5.0, 0.0], 1.0, 1.0).unwrap();
+ assert_eq!(
+ motion.pose(0.0),
+ Pose {
+ translation: center,
+ ..Pose::default()
+ }
+ );
+ assert!(motion.pose(0.2).translation[0] > center[0]);
+ assert!(motion.pose(0.2).translation[2] > center[2]);
+ assert_eq!(motion.pose(4.0), motion.pose(6.0));
+ assert!((motion.pose(4.0).translation[2] - 1.0).abs() < 0.00001);
+ }
+}
diff --git a/crates/fparkan-runtime/src/destruction.rs b/crates/fparkan-runtime/src/destruction.rs
index 38a4e38f..89688de5 100644
--- a/crates/fparkan-runtime/src/destruction.rs
+++ b/crates/fparkan-runtime/src/destruction.rs
@@ -1,10 +1,12 @@
//! Presentation events for projectile damage that destroys a unit.
use crate::{autodemo::MOBILE_UNIT, Engine, MissionControlDeviceTableState, ObjectHandle};
-use fparkan_world::transform_state;
+use fparkan_world::{identity_metadata, transform_state};
/// Position and attached EXPL resource of a chassis at lethal contact.
pub struct UnitDestructionEffect {
+ /// Prepared draft index for the destroyed actor when it has an authored layout.
+ pub object_index: Option<usize>,
/// World-space origin of the destroyed chassis.
pub position: [f32; 3],
/// Archive containing the explosion description.
@@ -60,11 +62,21 @@ pub(super) fn pending_effect(
.iter()
.find(|table| table.handle == handle)?;
let (archive, name) = root_effect(table)?;
+ let object_index = identity_metadata(&engine.world, handle)
+ .ok()
+ .and_then(|identity| identity.original_id)
+ .and_then(|id| {
+ mission
+ .object_drafts
+ .iter()
+ .position(|draft| draft.original_id == Some(id))
+ });
let position = transform_state(&engine.world, handle)
.ok()?
.position
.map(f32::from_bits);
Some(UnitDestructionEffect {
+ object_index,
position,
archive,
name,
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index 1ed90b32..b27c0090 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -75,6 +75,8 @@ mod clan_script_init;
mod clan_targeting;
mod construction;
mod construction_site;
+mod debris_motion;
+pub use debris_motion::DebrisMotion;
mod destruction;
pub use destruction::{
collect_unit_destruction_effects, take_unit_destruction_effects,
diff --git a/crates/fparkan-runtime/src/unit_combat.rs b/crates/fparkan-runtime/src/unit_combat.rs
index f1e86aab..60123dcd 100644
--- a/crates/fparkan-runtime/src/unit_combat.rs
+++ b/crates/fparkan-runtime/src/unit_combat.rs
@@ -825,14 +825,6 @@ mod tests {
.iter()
.find(|event| matches!(event, AutoDemoCombatEvent::Destroyed { .. }))
{
- assert!(destruction_effects.iter().any(|event| {
- event.archive == "system.rlb"
- && event.name == "explode_rbr_b.exp"
- && event
- .position
- .iter()
- .all(|coordinate| coordinate.is_finite())
- }));
assert!(identity_metadata(&engine.world, *target).is_err());
let ((_, retired), total) = combat
.report
@@ -842,7 +834,18 @@ mod tests {
object.slot == target.slot && object.generation == target.generation
})
.expect("removed target is still reported");
- assert!(retired.original_id.is_some());
+ let drafts = &engine.loaded.as_ref().unwrap().object_drafts;
+ assert!(destruction_effects.iter().any(|event| {
+ event.archive == "system.rlb"
+ && event.name == "explode_rbr_b.exp"
+ && event.object_index.is_some_and(|index| {
+ drafts[index].original_id.map(|id| id.0) == retired.original_id
+ })
+ && event
+ .position
+ .iter()
+ .all(|coordinate| coordinate.is_finite())
+ }));
assert_eq!(total.first_ms, now);
assert_eq!(total.count, 1);
assert!(observed_shots > 0);