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: ControlsThe controls, read from game-toolkit’s input in Platformer::update.
Implementations§
Source§impl Platformer
impl Platformer
Source§impl Platformer
impl Platformer
Sourcepub fn on(level: &Level, preset: Preset) -> Self
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.
Sourcepub fn at(spot: Spot, preset: Preset) -> Self
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.
Sourcepub fn builder() -> Builder
pub fn builder() -> Builder
Set one up step by step; starts from the hybrid preset with the hitbox at 0, 0.
Sourcepub fn update(
&mut self,
input: &Input,
terrain: &mut dyn Terrain,
dt: f32,
) -> u32
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.
Sourcepub fn advance(
&mut self,
intent: Intent,
terrain: &mut dyn Terrain,
dt: f32,
) -> u32
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.
Sourcepub fn step(&mut self, intent: &Intent, terrain: &mut dyn Terrain)
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.
Sourcepub fn scale(&self) -> i32
pub fn scale(&self) -> i32
Pixels of the terrain per unit of the movement (see Builder::scale).
Sourcepub fn drain_events(&mut self) -> Drain<'_, Event> ⓘ
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).
Sourcepub fn drain_anim_events(&mut self) -> Drain<'_, AnimEvent> ⓘ
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.
Sourcepub fn grounded(&self) -> bool
pub fn grounded(&self) -> bool
Standing on something (the ground, a platform, moving ground, another character).
Sourcepub fn velocity(&self) -> (f32, f32)
pub fn velocity(&self) -> (f32, f32)
The speed in units/s (pixels/s with a Builder::scale), y down.
Sourcepub fn mode(&self) -> Mode
pub fn mode(&self) -> Mode
What the body is busy with: running and jumping freely, dashing, climbing, a move of your own, …
Sourcepub fn stamina(&self) -> f32
pub fn stamina(&self) -> f32
Climbing stamina left (out of Tuning::stamina).
Sourcepub fn on_wall(&self) -> Option<i32>
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.
Sourcepub fn position(&self) -> (f32, f32)
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.
Sourcepub fn hitbox(&self) -> Rect
pub fn hitbox(&self) -> Rect
The hitbox in units (pixels with a Builder::scale), as of the last step.
Sourcepub fn hitboxes(&self) -> &[Rect]
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).
Sourcepub fn touched(&self, thing: Rect) -> Option<Rect>
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.
Sourcepub fn launch(&mut self, vx: f32, vy: f32)
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.
Sourcepub fn set_velocity(&mut self, vx: f32, vy: f32)
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.
Sourcepub fn set_facing(&mut self, dir: i32)
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.
Sourcepub fn teleport(&mut self, spot: Spot)
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.
Sourcepub fn respawn(&mut self)
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.
Sourcepub fn respawn_at(&mut self, spot: Spot)
pub fn respawn_at(&mut self, spot: Spot)
Start over at spot, like Platformer::respawn, without moving the spawn point.
Sourcepub fn set_spawn(&mut self, spot: Spot)
pub fn set_spawn(&mut self, spot: Spot)
Where Platformer::respawn (and falling out) puts the character from now on: a
checkpoint.
Sourcepub fn spawn(&self) -> Spot
pub fn spawn(&self) -> Spot
The spawn point (units, or pixels with a Builder::scale).
Sourcepub fn refill(&mut self)
pub fn refill(&mut self)
Refill dashes, air jumps and climbing stamina: a pickup, a checkpoint.
Sourcepub fn set_preset(&mut self, preset: Preset)
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.
Sourcepub fn tune(&mut self, f: impl FnOnce(&mut Tuning))
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.
Sourcepub fn add_ability(&mut self, ability: impl Ability + Clone + 'static)
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.
Sourcepub fn add_animated<T: Ability + Animate + Clone + 'static>(
&mut self,
ability: T,
) -> Result<(), SpecError>
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.
Sourcepub fn remove_ability<T: Ability + Clone + 'static>(&mut self) -> Option<T>
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.
Sourcepub fn ability<T: Ability + 'static>(&self) -> Option<&T>
pub fn ability<T: Ability + 'static>(&self) -> Option<&T>
The first move of type T, to read its state (a charge level, a phase).
Sourcepub fn ability_mut<T: Ability + 'static>(&mut self) -> Option<&mut T>
pub fn ability_mut<T: Ability + 'static>(&mut self) -> Option<&mut T>
Platformer::ability, to change (top up a charge, reset a cooldown).
Sourcepub fn contacts(&self) -> Contacts
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).
Sourcepub fn tuning(&self) -> &Tuning
pub fn tuning(&self) -> &Tuning
The movement feel in effect (change it with Platformer::tune).
Sourcepub fn world(&self) -> &World
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.
Sourcepub fn world_mut(&mut self) -> &mut World
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.
Sourcepub fn animator(&self) -> &Animator
pub fn animator(&self) -> &Animator
The animation graph’s runtime: its state, parameters, clip times.
Sourcepub fn animator_mut(&mut self) -> &mut Animator
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.
Sourcepub fn set_graph(&mut self, graph: GraphSpec) -> Result<(), SpecError>
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.
Sourcepub fn set_partial_graph(&mut self, graph: GraphSpec) -> Result<(), SpecError>
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§
impl !RefUnwindSafe for Platformer
impl !Send for Platformer
impl !Sync for Platformer
impl !UnwindSafe for Platformer
impl Freeze for Platformer
impl Unpin for Platformer
impl UnsafeUnpin for Platformer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.