#![allow(unused)]fnmain() {
// components.rspubfnpath(&self, path_index: usize) -> String {
// If we get asked for a path that is larger than the// number of paths we actually have, we simply mod the index// with the length to get an index that is in range.self.paths[path_index % self.paths.len()].clone()
}
}
#![allow(unused)]fnmain() {
// rendering.rspubfnget_image(context: &mut Context, renderable: &Renderable, delta: Duration) -> Image {
let path_index = match renderable.kind() {
RenderableKind::Static => {
// We only have one image, so we just return that0
}
RenderableKind::Animated => {
// If we have multiple, we want to select the right one based on the delta time.// First we get the delta in milliseconds, we % by 1000 to get the milliseconds// only and finally we divide by 250 to get a number between 0 and 4. If it's 4// we technically are on the next iteration of the loop (or on 0), but we will let// the renderable handle this logic of wrapping frames.
((delta.as_millis() % 1000) / 250) asusize
}
};
let image_path = renderable.path(path_index);
Image::from_path(context, image_path).unwrap()
}
}
最后,我们在 run 函数中使用新的 get_image 函数(我们还需要将时间添加到 SystemData 定义中,并添加一些导入,但基本上就是这样了)。
#![allow(unused)]fnmain() {
// rendering.rspubfnrun_rendering(world: &World, context: &mut Context) {
// Clearing the screen (this gives us the background colour)letmut canvas =
graphics::Canvas::from_frame(context, graphics::Color::from([0.95, 0.95, 0.95, 1.0]));
// Get timeletmut query = world.query::<&Time>();
let time = query.iter().next().unwrap().1;
// Get all the renderables with their positions and sort by the position z// This will allow us to have entities layered visually.letmut query = world.query::<(&Position, &Renderable)>();
letmut rendering_data: Vec<(Entity, (&Position, &Renderable))> = query.into_iter().collect();
rendering_data.sort_by_key(|&k| k.1 .0.z);
// Iterate through all pairs of positions & renderables, load the image// and draw it at the specified position.for (_, (position, renderable)) in rendering_data.iter() {
// Load the imagelet image = get_image(context, renderable, time.delta);
let x = position.x asf32 * TILE_WIDTH;
let y = position.y asf32 * TILE_WIDTH;
// drawlet draw_params = DrawParam::new().dest(Vec2::new(x, y));
canvas.draw(&image, draw_params);
}
// Render any textletmut query = world.query::<&Gameplay>();
let gameplay = query.iter().next().unwrap().1;
draw_text(&mut canvas, &gameplay.state.to_string(), 525.0, 80.0);
draw_text(&mut canvas, &gameplay.moves_count.to_string(), 525.0, 100.0);
// Finally, present the canvas, this will actually display everything// on the screen.
canvas.finish(context).expect("expected to present");
}
}