pub struct EnvironmentPlan { /* private fields */ }Expand description
A bounded sequence of authored boxes. Ramps use one box; stairs and bridges expose checked piece counts for capacity planning. Bounds include all solids, including ramp thickness and bridge rails/posts.
use ferravex_math::Vec3;
use ferravex_play::environment::{BuildError, EnvironmentPlan};
let count = EnvironmentPlan::stairs_piece_count(4)?
+ EnvironmentPlan::bridge_piece_count(2)? + 1; // One ramp.
let mut plan = EnvironmentPlan::new(count)?;
assert_eq!(plan.capacity(), count);
assert_eq!(plan.remaining_capacity(), count);
assert_eq!(plan.piece_count(), 0);
assert_eq!(plan.bounds(), None);
plan.stairs(Vec3::ZERO, 2.0, 0.25, 0.5, 4, 3)?;
assert_eq!(plan.piece_count(), 4);
assert_eq!(plan.remaining_capacity(), count - 4);
assert_eq!(plan.bounds(), Some((Vec3::new(-1.0, 0.0, 0.0),
Vec3::new(1.0, 1.0, 2.0))));Implementations§
Source§impl EnvironmentPlan
impl EnvironmentPlan
pub fn new(capacity: usize) -> Result<EnvironmentPlan, BuildError>
pub fn pieces(&self) -> &[BoxPiece]
Sourcepub fn piece_count(&self) -> usize
pub fn piece_count(&self) -> usize
Number of authored boxes, in the order returned by Self::pieces.
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Maximum total piece count for this plan, independent of allocator sizing.
Sourcepub fn remaining_capacity(&self) -> usize
pub fn remaining_capacity(&self) -> usize
Number of additional boxes this plan can accept.
Sourcepub fn bounds(&self) -> Option<(Vec3, Vec3)>
pub fn bounds(&self) -> Option<(Vec3, Vec3)>
Union of every piece’s world-axis-aligned BoxPiece::bounds, or None
for an empty plan. Visits each piece once without allocating.
Sourcepub fn stairs_piece_count(count: u16) -> Result<usize, BuildError>
pub fn stairs_piece_count(count: u16) -> Result<usize, BuildError>
Boxes required by Self::stairs. Rejects counts outside 1..=128.
This checks the count only; the builder also checks origin and dimensions.
Sourcepub fn bridge_piece_count(segments: u16) -> Result<usize, BuildError>
pub fn bridge_piece_count(segments: u16) -> Result<usize, BuildError>
Boxes required by Self::bridge: one deck, two rails and two posts at
each of segments + 1 stations. Rejects segment counts outside 1..=128.
This checks the count only; the builder also checks origin and dimensions.
Sourcepub fn append_atomic(
&mut self,
build: impl FnOnce(&mut EnvironmentPlan) -> Result<(), BuildError>,
) -> Result<(), BuildError>
pub fn append_atomic( &mut self, build: impl FnOnce(&mut EnvironmentPlan) -> Result<(), BuildError>, ) -> Result<(), BuildError>
Append a group of shapes only if build succeeds and every staged piece
fits. An error leaves this plan’s pieces, bounds and capacity unchanged.
The callback receives an empty temporary plan limited to this plan’s
remaining capacity, including zero when full. Propagate builder errors
with ?: deliberately handled errors do not abort the group. The final
count is checked again even if the callback replaces the temporary plan.
Existing pieces are not visible in the temporary plan.
This authoring operation allocates temporary storage for at most the remaining capacity (at most 1024 boxes). Individual shape builders still write directly into their plan. The callback’s external side effects are not rolled back; if it unwinds, the destination plan remains unchanged.
use ferravex_math::Vec3;
use ferravex_play::environment::{BuildError, EnvironmentPlan};
let mut plan = EnvironmentPlan::new(12)?;
plan.ramp(Vec3::ZERO, 2.0, 1.0, 3.0, 0.2, 0)?;
let before = plan.pieces().to_vec();
let result = plan.append_atomic(|group| {
group.stairs(Vec3::ZERO, 2.0, 0.25, 0.5, 4, 1)?;
group.bridge(Vec3::ZERO, 2.0, 4.0, 1.0, 2, 2) // Needs nine more.
});
assert_eq!(result, Err(BuildError::Capacity));
assert_eq!(plan.pieces(), before);
plan.append_atomic(|group| {
group.stairs(Vec3::ZERO, 2.0, 0.25, 0.5, 4, 1)?;
group.bridge(Vec3::ZERO, 2.0, 4.0, 1.0, 1, 2) // Seven fits exactly.
})?;
assert_eq!(plan.remaining_capacity(), 0);Sourcepub fn stairs(
&mut self,
origin: Vec3,
width: f32,
rise: f32,
run: f32,
count: u16,
material: u16,
) -> Result<(), BuildError>
pub fn stairs( &mut self, origin: Vec3, width: f32, rise: f32, run: f32, count: u16, material: u16, ) -> Result<(), BuildError>
Origin is the low edge at floor height. Each solid riser extends to floor.
Trait Implementations§
Source§impl Clone for EnvironmentPlan
impl Clone for EnvironmentPlan
Source§fn clone(&self) -> EnvironmentPlan
fn clone(&self) -> EnvironmentPlan
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more