aboutsummaryrefslogtreecommitdiff
path: root/apps/fparkan-game/src/model_mounts.rs
blob: 331e616a36c13a5bfc6d1ebefe8de1727fa94d97 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! Initial AniMesh attachment transforms shared by placed and previewed units.

use super::{
    compose_unit_pose, node38_pose, node38_pose_from_hierarchy, node38_pose_relative_to_root,
    node38_pose_relative_to_root_from_hierarchy, unit_component_is_visible, NodePoseBuffer,
    PreparedModel, PreparedVisual, VulkanNodePose,
};
use fparkan_prototype::{UnitComponentRecord, UnitComponentTreeNode};

pub(super) fn initial_mount(
    index: usize,
    records: &[UnitComponentRecord],
    tree: &[UnitComponentTreeNode],
    components: &[(&PreparedVisual, Option<&PreparedModel>)],
    poses: &[Option<NodePoseBuffer>],
    mounts: &[Option<VulkanNodePose>],
    animation_frame: Option<u16>,
) -> Result<Option<VulkanNodePose>, String> {
    let Some(parent) = tree[index].parent_index else {
        return Ok(None);
    };
    let Some((visual, model)) = components.get(parent) else {
        return Ok(None);
    };
    if !unit_component_is_visible(parent, visual.source_type) {
        return Ok(None);
    }
    let model = model.ok_or_else(|| {
        format!("unit component {index} parent {parent} has no model for mount socket")
    })?;
    let link = records[index].parent_or_link;
    let socket = usize::try_from(link)
        .map_err(|_| format!("unit component {index} has negative mount socket {link}"))?;
    let parent_mount = mounts[parent];
    let parent_socket = match (poses[parent].as_ref(), parent_mount.is_some()) {
        (Some(hierarchy), true) => node38_pose_relative_to_root_from_hierarchy(hierarchy, socket),
        (Some(hierarchy), false) => node38_pose_from_hierarchy(hierarchy, socket),
        (None, true) => node38_pose_relative_to_root(&model.validated, animation_frame, socket),
        (None, false) => node38_pose(&model.validated, animation_frame, socket),
    }.ok_or_else(|| {
        format!(
            "unit component {index} mount socket {socket} is absent from parent {parent} (parent source={:?}, node_count={}, slots={}, animation={})",
            model.source, model.validated.node_count, model.validated.slots.len(), model.validated.animation.is_some()
        )
    })?;
    Ok(Some(compose_unit_pose(
        parent_mount.unwrap_or(VulkanNodePose {
            translation: [0.0; 3],
            rotation: [0.0, 0.0, 0.0, 1.0],
        }),
        parent_socket,
    )))
}