Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions changelog/entries/2026-12-01_battle_defeat_camera.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
issues: []
prs: []
---

# Fixed
The battle camera no longer jumps downward when the last party member falls.
56 changes: 40 additions & 16 deletions crates/battle/src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub(crate) enum Update {
Tracking,
/// Continue framing while holding yaw and scene timers.
PausedTracking,
Defeat,
Held,
Selecting(ActorId),
CloseUp(ActorId),
Expand Down Expand Up @@ -77,6 +78,8 @@ pub(crate) struct Camera {
pub pose: CameraPose,
base_radius: f32,
pending_radius: f32,
/// The last ordinary view used the leader's horizontal zoom and elevated eye.
tracking_leader: bool,
result_tick: u32,
spell: Option<spell::Shot>,
scene: Option<SpellCamera>,
Expand Down Expand Up @@ -207,6 +210,7 @@ impl Camera {
self.constrain_yaw();
self.pose.radius = ENTRY_RADIUS;
self.pending_radius = ENTRY_RADIUS;
self.tracking_leader = true;
let eye = controlled_eye(self.pose);
if update == 0 {
self.pose.eye = eye;
Expand All @@ -221,6 +225,26 @@ impl Camera {
approach_position(&mut self.pose.eye, eye, speed);
}

fn advance_zoom(&mut self, response: f32) {
let delta = self.pending_radius - self.pose.radius;
let speed = ZOOM_SPEED + response * delta.abs();
self.pose.radius += delta.clamp(-speed, speed);
}

fn approach_pitch(&mut self, pitch: f32) {
self.pose.pitch += (pitch - self.pose.pitch).clamp(-SCENE_PITCH_SPEED, SCENE_PITCH_SPEED);
}

fn tracking_eye(&self) -> [f32; 3] {
if self.tracking_leader {
controlled_eye(self.pose)
} else {
let mut desired = self.pose;
desired.place_eye();
desired.eye
}
}

pub fn new(definition: CameraDefinition, actors: &[Actor], target: ActorId) -> Result<Self> {
ensure!(
definition.leader.index() < actors.len() && target.index() < actors.len(),
Expand Down Expand Up @@ -262,6 +286,8 @@ impl Camera {
} else {
FIXED_ZOOM_RADIUS
};
let leader = &actors[definition.leader.index()];
let tracking_leader = leader.control != Control::Auto && leader.available();
let mut camera = Self {
pose: CameraPose {
eye: [0.; 3],
Expand All @@ -273,17 +299,16 @@ impl Camera {
definition,
base_radius,
pending_radius: radius,
tracking_leader,
result_tick: 0,
spell: None,
scene: None,
distance_override: None,
summon_focus: None,
};
let leader = &actors[camera.definition.leader.index()];
camera.pose.focus =
controlled_focus(leader, &actors[target.index()], camera.definition.auto_zoom);
let automatic = leader.control == Control::Auto || !leader.available();
if automatic {
if !tracking_leader {
let subjects: Vec<_> = actors
.iter()
.filter(|actor| actor.available())
Expand Down Expand Up @@ -389,6 +414,10 @@ impl Camera {
let scene_pitch = bounds.map_or(base_pitch, |bounds| {
base_pitch.max(bounds.minimum_elevation)
});
if matches!(update, Update::Defeat) {
self.frame_defeat(actors, scene_pitch);
return self.validate_frame();
}
let leader = &actors[self.definition.leader.index()];
let controlled = leader.control != Control::Auto && leader.available();
let (focus, radius) = if controlled {
Expand All @@ -415,8 +444,7 @@ impl Camera {
} else {
scene_pitch
};
self.pose.pitch +=
(scene_pitch - self.pose.pitch).clamp(-SCENE_PITCH_SPEED, SCENE_PITCH_SPEED);
self.approach_pitch(scene_pitch);
let focus = if let Some(selected) = selected {
let position = actors[selected.index()].position;
[position[0], FOCUS_HEIGHT, position[2]]
Expand Down Expand Up @@ -452,10 +480,11 @@ impl Camera {
// Do not spend the selector's first update zooming toward the
// previous combat target before adopting the selected target.
self.pending_radius = desired_radius;
} else if !self.tracking_leader {
// A returning leader starts the zoom from the displayed group radius.
self.pending_radius = self.pose.radius;
}
let delta = self.pending_radius - self.pose.radius;
let speed = ZOOM_SPEED + ZOOM_RESPONSE * delta.abs();
self.pose.radius += delta.clamp(-speed, speed);
self.advance_zoom(ZOOM_RESPONSE);
// Combat distance framing follows the preceding position sample.
self.pending_radius = desired_radius;
(focus, desired_radius)
Expand Down Expand Up @@ -510,7 +539,7 @@ impl Camera {
|focus| focus.distance,
);
self.pose.radius += (radius - self.pose.radius) * RESPONSE;
self.pending_radius = self.pose.radius;
self.pending_radius = radius;
(focus, radius)
};
// The returning shot supplies its own easing toward the new framing.
Expand All @@ -521,13 +550,8 @@ impl Camera {
self.pending_radius = radius;
}
self.constrain_yaw();
let eye = if controlled {
controlled_eye(self.pose)
} else {
let mut desired = self.pose;
desired.place_eye();
desired.eye
};
self.tracking_leader = controlled;
let eye = self.tracking_eye();
if returning {
self.pose.eye = eye;
} else {
Expand Down
67 changes: 64 additions & 3 deletions crates/battle/src/camera/result.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! The result scene's held opening, zoom and slow orbit.
use super::Camera;
use crate::{ActorId, Battle, BattlePhase, Side};
//! Camera framing at party defeat and in the victory results scene.
use super::{
Camera, CameraPose, MAXIMUM_FOCUS_SPEED, ZOOM_RESPONSE, add, approach_position, scale, sub,
};
use crate::{Actor, ActorId, Battle, BattlePhase, Control, Side, project_screen_x};
use anyhow::{Result, ensure};

const RESULT_HEADING: f32 = 120.;
Expand Down Expand Up @@ -55,7 +57,66 @@ impl Battle {
}
}

fn defeat_midpoint(actors: &[Actor], pose: CameraPose) -> [f32; 3] {
const EDGE_MARGIN_PIXELS: f32 = 24.;
let project = |actor: &Actor| {
let [x, _, z] = actor.position;
let ground = [x, 0., z];
(project_screen_x(pose, ground), ground)
};
// The first formation slot anchors the view even when another slot owns the camera.
let first = actors
.iter()
.filter(|actor| actor.side == Side::Party)
.min_by_key(|actor| actor.control_slot.index())
.expect("battle camera requires a party leader");
let mut left = project(first);
let mut right = left;
for candidate in actors.iter().filter(|actor| actor.available()).map(project) {
if candidate.0 <= left.0 - EDGE_MARGIN_PIXELS {
left = candidate;
}
if candidate.0 >= right.0 + EDGE_MARGIN_PIXELS {
right = candidate;
}
}
scale(add(left.1, right.1), 0.5)
}

impl Camera {
/// Finish the combat view with one lateral pan before the defeat notice holds it.
pub(super) fn frame_defeat(&mut self, actors: &[Actor], pitch: f32) {
const PAN_SPEED: f32 = 5.;
const PAN_RESPONSE: f32 = 0.002;
const GROUP_ZOOM_RESPONSE: f32 = 0.025;
const AUTO_ORBIT_STEP: f32 = 0.05;

self.pose = self.frame();
let focus = self.pose.focus;
let [x, _, z] = self.summon_focus.map_or_else(
|| defeat_midpoint(actors, self.pose),
|summon| summon.target,
);
let distance = (x - focus[0]).hypot(z - focus[2]);
let speed = (PAN_SPEED + PAN_RESPONSE * distance).min(MAXIMUM_FOCUS_SPEED);
approach_position(&mut self.pose.focus, [x, focus[1], z], speed);
if self.spell.take().is_some() {
self.pose.eye = add(self.pose.eye, sub(self.pose.focus, focus));
} else {
self.approach_pitch(pitch);
if actors[self.definition.leader.index()].control == Control::Auto {
self.pose.yaw += AUTO_ORBIT_STEP;
self.constrain_yaw();
}
self.advance_zoom(if self.tracking_leader {
ZOOM_RESPONSE
} else {
GROUP_ZOOM_RESPONSE
});
self.approach_eye(self.tracking_eye());
}
}

pub(crate) fn frame_results(&mut self) {
self.spell = None;
self.scene = None;
Expand Down
70 changes: 69 additions & 1 deletion crates/battle/src/camera/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,65 @@ fn leader_death_and_revival_ease_between_controlled_and_group_views() -> Result<
Ok(())
}

#[test]
fn party_defeat_finishes_the_current_camera_update_then_holds() -> Result<()> {
// The first recording has settled zoom. In the second the party is running toward an enemy.
for (first_x, near_x, far, focus_x, radius, final_focus, final_eye) in [
(
-500.,
-385.425,
[232.9575, 0., -251.81845],
-441.74377,
2450.,
[-436.49863, 120., -2.14264],
[-222.9672, 580.974, 2438.5344],
),
(
-84.26226,
24.177275,
[400., 0., -250.],
-30.596817,
2507.1401,
[-26.053076, 120., -3.013639],
[191.73633, 587.7733, 2486.3323],
),
] {
let mut subjects = actors();
// Slot one anchors the pan even when the player controls slot two.
subjects.insert(1, subjects[0].clone());
subjects[0].position[0] = first_x;
subjects[0].control = Control::Auto;
subjects[0].hp = 0;
subjects[1].control_slot = crate::ControllerPort::Two;
subjects[1].position = [-700., 0., 150.];
subjects[2].position[0] = near_x;
subjects[3].position = far;
let leader = ActorId(1);
let mut battle = prepare_actors(subjects, 1)
.with_camera(CameraDefinition {
leader,
..definition(true)
})?
.finish()?;
let camera = battle.camera.as_mut().unwrap();
camera.pose.focus = [focus_x, 120., 0.];
camera.pose.yaw = 85.;
camera.pose.radius = radius;
camera.pose.eye = controlled_eye(camera.pose);
camera.pending_radius = 2450.;
battle.actors[leader.index()].hp = 0;
battle.enter_death(leader, &mut vec![]);

let frame = battle.step(Default::default())?;
assert_eq!(frame.recognized_result, Some(crate::BattleResult::Defeat));
let after = frame.camera.unwrap();
crate::tests::assert_vector_close(after.focus, final_focus, 0.01);
crate::tests::assert_vector_close(after.eye, final_eye, 0.01);
assert_eq!(battle.step(Default::default())?.camera, Some(after));
}
Ok(())
}

#[test]
fn results_camera_matches_recorded_victory_transition() {
// Eye positions sampled at result-scene ages in the opening Iselia battle.
Expand Down Expand Up @@ -361,7 +420,7 @@ fn stored_spell_framing_matches_recorded_casts_and_returns() -> Result<()> {
}

#[test]
fn active_scene_camera_starts_at_initialization_holds_then_restores_tracking() -> Result<()> {
fn active_scene_framing_respects_holds_expiry_and_party_defeat() -> Result<()> {
use resonance_content::battle_scene::CameraBounds;
let mut actors = actors();
actors.truncate(2);
Expand Down Expand Up @@ -435,6 +494,15 @@ fn active_scene_camera_starts_at_initialization_holds_then_restores_tracking() -
assert_eq!((camera.pose.radius, camera.pose.pitch), (2050., 8.));
camera.request_scene(framing);
assert_eq!(camera.scene, Some(framing));
// A summon can still own the focus and elevation when the last party member falls.
camera.request_summon_focus([800., 0., 0.], 2450., 20);
let before = battle.step(Default::default())?.camera.unwrap();
battle.actors[0].hp = 0;
battle.enter_death(ActorId(0), &mut vec![]);
let after = battle.step(Default::default())?.camera.unwrap();
assert!(after.focus[0] > before.focus[0]);
assert_eq!(after.pitch, before.pitch + SCENE_PITCH_SPEED);
assert_eq!(battle.step(Default::default())?.camera, Some(after));
Ok(())
}

Expand Down
44 changes: 44 additions & 0 deletions crates/battle/src/contact/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,50 @@ fn petrification_stops_target_traversal_but_ordinary_hits_keep_recoil() -> Resul
Ok(())
}

#[test]
fn last_party_member_petrified_during_group_camera_zoom() -> Result<()> {
for (leader_control, orbit_step) in [(Control::Manual, 0.), (Control::Auto, 0.05)] {
let mut enemy = crate::tests::actor(Side::Enemy);
enemy.position[0] = 3000.;
let mut survivor = crate::tests::actor(Side::Party);
survivor.control = Control::Auto;
survivor.control_slot = crate::ControllerPort::Two;
survivor.body.collider = Some(crate::Collider::sphere(1.));
let mut leader = crate::tests::actor(Side::Party);
leader.control = leader_control;
leader.hp = 0;
let mut battle = crate::tests::prepare_actors(vec![enemy, survivor, leader], 1)
.with_camera(crate::CameraDefinition {
leader: ActorId(2),
stage_pitch: 0.,
auto_zoom: true,
yaw_limits: None,
})?
.finish()?;
// The enemy closes on the surviving companion. Group zoom is still moving inward.
battle.actors[0].position = [0.; 3];
let before = battle.step(Default::default())?.camera.unwrap();
assert!(before.radius > 2000.);
let mut hit = rule(1);
hit.condition = Some(crate::HitCondition {
condition: HitAilment::Petrified,
chance: 100,
value: 0,
});
strike(&mut battle, hit)?;
assert!(battle.actors[1].is_petrified() && battle.actors[1].hp > 0);
let frame = battle.step(Default::default())?;
assert_eq!(frame.recognized_result, Some(crate::BattleResult::Defeat));
let after = frame.camera.unwrap();
let zoom_step = 4. + 0.025 * (before.radius - 1950.);
crate::tests::assert_close(before.radius - after.radius, zoom_step, 0.001);
crate::tests::assert_close(after.yaw - before.yaw, orbit_step, 0.001);
assert_eq!(after.focus[1], before.focus[1]);
assert_eq!(battle.step(Default::default())?.camera, Some(after));
}
Ok(())
}

#[test]
fn petrified_contact_is_skipped_without_condition_or_rng() -> Result<()> {
let owner = crate::tests::actor(Side::Party);
Expand Down
2 changes: 2 additions & 0 deletions crates/battle/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1801,6 +1801,8 @@ impl Battle {
self.volleys.values().find(|volley| volley.transitioning())
{
crate::camera::Update::CloseUp(volley.actor)
} else if self.terminal.result == Some(BattleResult::Defeat) {
crate::camera::Update::Defeat
} else {
crate::camera::Update::Tracking
})?;
Expand Down
Loading