Skip to main content

Parallax

Struct Parallax 

Source
pub struct Parallax(/* private fields */);
Expand description

How much of the camera’s motion a layer takes.

1.0 is fixed in the world, 0.0 is pinned to the camera. Values between are the usual parallax; values outside are legal and occasionally wanted — a factor above 1 runs ahead of the camera, which is how a foreground element sweeps past faster than the ground.

use crcbl_render::Parallax;
// A camera 100 units to the right. Each layer keeps the share of that
// movement it did *not* take: the hills drift by half, the HUD by all of it
// (so it does not move on screen), the world by none.
let hills = Parallax::new(0.5).expect("0.5 is finite");
assert_eq!(hills.offset([100.0, 0.0]), [50.0, 0.0]);
assert_eq!(Parallax::CAMERA.offset([100.0, 0.0]), [100.0, 0.0]);
assert_eq!(Parallax::WORLD.offset([100.0, 0.0]), [0.0, 0.0]);

Deliberately not PartialOrd: a layer’s depth is where it sits in its LayerStack, not how much parallax it takes, and the two disagree the moment a HUD is added.

Implementations§

Source§

impl Parallax

Source

pub const WORLD: Self

Fixed in the world: the layer the simulation actually happens on.

Source

pub const CAMERA: Self

Pinned to the camera, so it never moves on screen — a HUD.

Source

pub fn new(factor: f32) -> Option<Self>

A factor of factor.

None for anything not finite. A NaN factor would put every sprite on the layer at a NaN world position, which draws nothing at all and looks exactly like a renderer that stopped working; refusing it here is one branch, at the only point where a computed factor enters the system.

Source

pub const fn factor(self) -> f32

The factor itself.

Source

pub fn offset(self, camera: [f32; 2]) -> [f32; 2]

How far this layer’s contents shift for a camera at camera.

(1 − factor) × camera, which is the whole of parallax — see this module’s docs for why that is the value that makes one view-projection enough.

Trait Implementations§

Source§

impl Clone for Parallax

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Parallax

Source§

impl Debug for Parallax

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Parallax

Source§

fn default() -> Self

Parallax::WORLD: a layer nobody said anything about is part of the world, which is the answer that makes an unconfigured stack behave like no stack at all.

Source§

impl PartialEq for Parallax

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Parallax

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> HalThreadSafe for T
where T: Send + Sync,

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.