Skip to main content

Platformer

Struct Platformer 

Source
pub struct Platformer {
    pub controls: Controls,
    /* private fields */
}
Expand description

One playable character. The world around it is yours: pass your Terrain (or a Level) to each update. For spikes, springs, checkpoints and pickups, test Platformer::hitboxes after the update and answer with Platformer::respawn, Platformer::launch, Platformer::set_spawn or Platformer::refill (docs/hazards.md has the recipe).

The methods keep the character consistent: Platformer::tune or Platformer::set_preset for the tuning, Platformer::add_ability and Platformer::remove_ability for abilities, Platformer::set_graph for the graph, Platformer::set_spawn for the spawn. For what they don’t cover, Platformer::world_mut and Platformer::animator_mut open the simulation and the animation; see there for what is safe to change.

Fields§

§controls: Controls

The controls, read from game-toolkit’s input in Platformer::update.

Implementations§

Source§

impl Platformer

Source

pub fn draw_debug(&self, p: &mut Painter<'_>, origin: [f32; 2], scale: f32)

Draw the character as a box colored by its animation state, an eye on the side it faces and the state’s name above, with p: the world’s origin at origin (window pixels), scale window pixels per unit.

Source§

impl Platformer

Source

pub fn on(level: &Level, preset: Preset) -> Self

A character on level’s spawn tile with preset’s feel and controls and the built-in animation.

Source

pub fn at(spot: Spot, preset: Preset) -> Self

A character at spot (units), otherwise like Platformer::on: the one-liner for a terrain of your own, with Spot::Feet(x, floor_y) to stand on its ground.

Source

pub fn builder() -> Builder

Set one up step by step; starts from the hybrid preset with the hitbox at 0, 0.

Source

pub fn update( &mut self, input: &Input, terrain: &mut dyn Terrain, dt: f32, ) -> u32

Read game-toolkit’s input with Platformer::controls and advance by dt seconds of frame time in terrain. Returns the fixed steps taken.

Source

pub fn advance( &mut self, intent: Intent, terrain: &mut dyn Terrain, dt: f32, ) -> u32

Advance by dt seconds of frame time with intent in terrain, in fixed steps of DT. Presses are kept until the next step, so a frame without one doesn’t lose them. Returns the steps taken; Platformer::hitboxes then has where each one left the character.

Source

pub fn step(&mut self, intent: &Intent, terrain: &mut dyn Terrain)

Exactly one fixed step of DT with intent in terrain (for your own clock, replays, tests). It counts as a frame of one step for Platformer::hitboxes.

Source

pub fn scale(&self) -> i32

Pixels of the terrain per unit of the movement (see Builder::scale).

Source

pub fn drain_events(&mut self) -> Drain<'_, Event> ⓘ

The movement events since the last call: jumps, landings, dashes, rolls, … In your terrain’s space: with a Builder::scale, a landing’s drop and impact are in pixels (see Event::in_pixels). The buffer is kept, so draining every frame doesn’t allocate (.collect() for a Vec).

Source

pub fn drain_anim_events(&mut self) -> Drain<'_, AnimEvent> ⓘ

The animation events since the last call: states entered, markers ("step"), clips finished. Like Platformer::drain_events, without allocating.

Source

pub fn sprite(&self) -> u16

The sprite number to draw (see hero::sprite).

Source

pub fn state(&self) -> &str

The animation state’s name.

Source

pub fn facing(&self) -> i32

1 facing right, -1 facing left.

Source

pub fn grounded(&self) -> bool

Standing on something (the ground, a platform, moving ground, another character).

Source

pub fn velocity(&self) -> (f32, f32)

The speed in units/s (pixels/s with a Builder::scale), y down.

Source

pub fn mode(&self) -> Mode

What the body is busy with: running and jumping freely, dashing, climbing, a move of your own, …

Source

pub fn dashes(&self) -> u8

Dashes left before touching the ground (or a refill) again.

Source

pub fn air_jumps(&self) -> u8

Air jumps left.

Source

pub fn stamina(&self) -> f32

Climbing stamina left (out of Tuning::stamina).

Source

pub fn on_wall(&self) -> Option<i32>

The side of the wall it holds on to or slides down (-1 left, 1 right), if any.

Source

pub fn position(&self) -> (f32, f32)

The hitbox’s top-left in units (pixels with a Builder::scale), smoothly between fixed steps: where to draw.

Source

pub fn hitbox(&self) -> Rect

The hitbox in units (pixels with a Builder::scale), as of the last step.

Source

pub fn hitboxes(&self) -> &[Rect]

The hitbox after each fixed step of the last frame (Platformer::advance, Platformer::update, Platformer::step or a crate::Party’s), oldest first; empty when the frame took no step. Test hazards and triggers against every one: at a low frame rate a frame takes several steps, and a spike touched in one of them may be left again by the last (Platformer::hitbox).

Source

pub fn touched(&self, thing: Rect) -> Option<Rect>

The first of Platformer::hitboxes that overlaps thing (a spike, an enemy, a pickup, in your terrain’s space), if any. A copy, so you can act on the character right away: if hero.touched(spikes).is_some() { hero.respawn(); }. On a Level’s tiles, see Level::touches.

Source

pub fn launch(&mut self, vx: f32, vy: f32)

Set the speed from outside: a spring, a bounce pad, knockback (units/s, y down). It ends a dash or a climb, can’t be cut by releasing jump, and going up leaves the ground. See Player::launch.

Source

pub fn set_velocity(&mut self, vx: f32, vy: f32)

Set the speed (units/s, pixels/s with a Builder::scale, y down) without anything else Platformer::launch does: a dash or climb goes on, a jump can still be cut. A conveyor of your own, a slowdown, a stop.

Source

pub fn set_facing(&mut self, dir: i32)

Face right (1) or left (-1; any other sign counts as its side, 0 keeps it): a cutscene, a respawn facing into the level.

Source

pub fn teleport(&mut self, spot: Spot)

Put the character at spot at once, without drawing it sliding there: a door, a portal. Speed and state stay; see Player::teleport.

Source

pub fn respawn(&mut self)

Start over at the spawn point: a fresh body, the animation from its entry state, and an Event::Respawned. For hazards; see Platformer::set_spawn for checkpoints.

Source

pub fn respawn_at(&mut self, spot: Spot)

Start over at spot, like Platformer::respawn, without moving the spawn point.

Source

pub fn set_spawn(&mut self, spot: Spot)

Where Platformer::respawn (and falling out) puts the character from now on: a checkpoint.

Source

pub fn spawn(&self) -> Spot

The spawn point (units, or pixels with a Builder::scale).

Source

pub fn refill(&mut self)

Refill dashes, air jumps and climbing stamina: a pickup, a checkpoint.

Source

pub fn preset(&self) -> Option<Preset>

The preset the tuning came from, if any.

Source

pub fn set_preset(&mut self, preset: Preset)

Switch to preset’s feel (and, with game-toolkit, its controls, keeping your custom buttons), keeping the character where it is and its size.

Source

pub fn tune(&mut self, f: impl FnOnce(&mut Tuning))

Change the tuning at runtime, e.g. hero.tune(|t| t.air_jumps = 2) for a power-up. The feel is no longer a preset’s: Platformer::preset says None from now on (the controls stay). A new Tuning::size takes effect at the next respawn.

Source

pub fn add_ability(&mut self, ability: impl Ability + Clone + 'static)

Add a move of your own at runtime (a power-up), run after the ones already there. See Builder::ability.

Source

pub fn add_animated<T: Ability + Animate + Clone + 'static>( &mut self, ability: T, ) -> Result<(), SpecError>

Add a move of your own with animation of its own at runtime. See Builder::animated. If the graph lacks one of its Animate::PARAMS, nothing is added and the error says which.

Source

pub fn remove_ability<T: Ability + Clone + 'static>(&mut self) -> Option<T>

Take away the first move of type T (a power-up that runs out) and return it. Its Animate half stops with it; the other abilities keep driving their own animation.

Source

pub fn ability<T: Ability + 'static>(&self) -> Option<&T>

The first move of type T, to read its state (a charge level, a phase).

Source

pub fn ability_mut<T: Ability + 'static>(&mut self) -> Option<&mut T>

Platformer::ability, to change (top up a charge, reset a cooldown).

Source

pub fn contacts(&self) -> Contacts

What the body’s own movement ran into in the last step: a wall, a ceiling, the floor (see Contacts).

Source

pub fn tuning(&self) -> &Tuning

The movement feel in effect (change it with Platformer::tune).

Source

pub fn world(&self) -> &World

The simulation, to read anything the methods don’t cover: the body (world().player), the step count, the last step’s events.

Source

pub fn world_mut(&mut self) -> &mut World

The simulation, to change what the methods don’t cover: the escape hatch. Games shouldn’t need it: Platformer::launch, Platformer::set_velocity, Platformer::set_facing, Platformer::refill, Platformer::teleport, Platformer::respawn, Platformer::tune, the ability and spawn methods cover what games change. What’s writable on the body (crate::sim::Player) is safe; the rest of the world you keep consistent yourself. Everything here is in units, not in a Builder::scale’s pixels.

Source

pub fn animator(&self) -> &Animator

The animation graph’s runtime: its state, parameters, clip times.

Source

pub fn animator_mut(&mut self) -> &mut Animator

The animation graph’s runtime, to change. Safe: parameters only your game sets (params); the drivers overwrite theirs every step. Swap the graph with Platformer::set_graph.

Source

pub fn set_graph(&mut self, graph: GraphSpec) -> Result<(), SpecError>

Swap the animation graph at runtime, checked like Builder::graph: it must declare the built-in driver’s parameters (unless that is off) and those of every Animate ability (Animate::PARAMS). The animation starts over from the new graph’s entry state; on an error nothing changes.

Source

pub fn set_partial_graph(&mut self, graph: GraphSpec) -> Result<(), SpecError>

Platformer::set_graph for a graph that declares only what it uses, like Builder::partial_graph.

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

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.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more