Module projectile

Source
Expand description

Per-projectile target deduplication, piercing and ricochet budgets.

This helper admits caller-confirmed contacts; it performs no collision query, target lookup, damage or feedback. A u64 is an opaque host-provided generation identity (for ECS entities, use the existing Entity::to_bits()). The host must reject stale identities and supply a different ID for a new generation. Equal IDs are admitted at most once over this budget’s lifetime; distinct generations can both be admitted. Even rejected game-owned damage consumes an admitted contact: there is no retry or refund here.

Create a fresh budget with ProjectileBudget::new for a new projectile or run. There is no in-place reset. Copying a budget forks its admission history; it does not coordinate two authoritative projectiles or provide a rollback ledger. Operations allocate nothing and inspect at most N <= 256 slots.

This runnable recipe uses the existing first-party sphere sweep in the XY plane, then budget admission, then actual health acceptance and feedback. The second generation is immune, so its nominal admission emits no feedback. This is a single-contact recipe, not a multi-contact traversal/solver.

use ferravex_math::Vec3;
use ferravex_play::{
    attributes::Vitals,
    camera::{Blocker, Sweep},
    feedback::{FeedbackConfig, FeedbackController, FeedbackEvent},
    gameplay::projectile::{ProjectileBudget, TargetOutcome},
};

fn main() {
    let mut budget = ProjectileBudget::<3>::new(2, 1).unwrap();
    let mut health = Vitals::new(20., 0., 0., [0.; 8]).unwrap();
    let mut immune = Vitals::new(20., 0., 0., [1.; 8]).unwrap();
    let mut feedback = FeedbackController::<4>::new(FeedbackConfig::default(), 7).unwrap();
    let sweep = Sweep::new(Vec3::ZERO, Vec3::X * 10., 0.25).unwrap();
    let target = [Blocker::Sphere { center: Vec3::X * 5., radius: 0.75 }];
    let miss = [Blocker::Sphere { center: Vec3::Y * 5., radius: 0.75 }];
    assert!(sweep.probe(&miss[..], 0.).unwrap().hit_fraction.is_none());
    assert_eq!(budget.state().accepted_targets, 0);
    let mut cues = 0;
    // IDs are supplied by the host, not inferred from geometric witnesses.
    for (event_id, id) in [(1, 0x1_0000_0007), (2, 0x1_0000_0007), (3, 0x2_0000_0007)] {
        let contact = sweep.probe(&target[..], 0.).unwrap();
        if let Some(fraction) = contact.hit_fraction {
            assert!((fraction - 0.4).abs() < 1e-6);
            let report = budget.target_report(id);
            let mut accepted_damage = false;
            if report.accepted() {
                let vitals = if event_id == 3 { &mut immune } else { &mut health };
                let damage = vitals.damage(5., 0).unwrap();
                accepted_damage = damage.health > 0. || damage.shield > 0.;
            } else {
                assert_eq!(report.outcome, TargetOutcome::DuplicateTarget);
            }
            let event = FeedbackEvent::hit(sweep.start.lerp(sweep.end, fraction), 1.).unwrap();
            cues += usize::from(feedback.emit_if_accepted(event_id, accepted_damage, event)
                .unwrap().is_some());
        }
    }
    assert_eq!((health.health(), immune.health(), cues), (15., 20., 1));
    assert_eq!(budget.state().accepted_targets, 2);
    assert!(budget.has_hit(0x2_0000_0007));
}

Structs§

ProjectileBudget
Bounded, allocation-free policy for one projectile lifetime. See module recipe.
ProjectileState
Read-only state after a reported contact. See the module’s runnable recipe.
TargetReport
Target admission and resulting budgets. See the module’s runnable recipe.
WallReport
Wall outcome and resulting budgets. velocity is exactly the legacy wall return value: None only for inactive or exhausted bounce. Separating and tangent outcomes are not accepted impacts and must not trigger hit feedback. A bounced report is nominal budget admission; the host owns applying the response and accepting any game-owned impact/damage before feedback. See ProjectileBudget::wall_report for a runnable example.

Enums§

HitResult
Legacy admission result. Stop normally includes an accepted final hit.
TargetOutcome
Budget policy only; acceptance does not prove game-owned damage or validity.
WallOutcome
Wall budget policy after vector validation, not a collision/normal query.