pub trait State {
fn update(&mut self, ctx: &mut Context) -> Result { ... }
fn draw(&mut self, ctx: &mut Context, dt: f64) -> Result { ... }
}
Expand description
A trait representing a type that contains game state and provides logic for updating it and drawing it to the screen. This is where you’ll write your game logic!
The methods on State
allow you to return a Result
, either explicitly or via the ?
operator. If an error is returned, the game will close and the error will be returned from
the run
function that was used to start it.
Provided Methods§
sourcefn update(&mut self, ctx: &mut Context) -> Result
fn update(&mut self, ctx: &mut Context) -> Result
Called when it is time for the game to update, at the interval specified by the context’s tick rate.
The game will update at a fixed time step (defaulting to 60fps), and draw as fast as it is
allowed to (depending on CPU/vsync settings). This allows for deterministic updates even at
varying framerates, but may require you to do some interpolation in the draw
method in
order to make things look smooth.
See Fix Your Timestep for more info.
sourcefn draw(&mut self, ctx: &mut Context, dt: f64) -> Result
fn draw(&mut self, ctx: &mut Context, dt: f64) -> Result
Called when it is time for the game to be drawn.
As drawing will not necessarily be in step with updating, the dt
argument is provided -
this will be a number between 0 and 1, specifying how far through the current tick you are.
For example, if the player is meant to move 16 pixels per frame, and the current dt
is 0.5,
you should draw them 8 pixels along.