doukutsu-rs/src/scene/loading_scene.rs

53 lines
1.5 KiB
Rust
Raw Normal View History

2020-08-18 16:46:07 +00:00
use ggez::{Context, GameResult};
2020-08-19 11:21:40 +00:00
use crate::game_state::GameState;
use crate::GameContext;
2020-08-18 16:46:07 +00:00
use crate::scene::Scene;
2020-08-19 00:55:21 +00:00
use crate::stage::StageData;
2020-08-18 16:46:07 +00:00
pub struct LoadingScene {
tick: usize,
}
impl LoadingScene {
2020-08-19 00:55:21 +00:00
pub fn new() -> Self {
Self {
2020-08-18 16:46:07 +00:00
tick: 0,
}
}
}
impl Scene for LoadingScene {
2020-08-19 11:21:40 +00:00
fn init(&mut self, state: &mut GameState, game_ctx: &mut GameContext, ctx: &mut Context) -> GameResult {
game_ctx.texture_set.ensure_texture_loaded(ctx, &game_ctx.constants, "Loading")?;
2020-08-18 16:46:07 +00:00
Ok(())
}
2020-08-19 11:21:40 +00:00
fn tick(&mut self, state: &mut GameState, game_ctx: &mut GameContext, ctx: &mut Context) -> GameResult {
2020-08-18 16:46:07 +00:00
// deferred to let the loading image draw
if self.tick == 1 {
2020-08-19 11:21:40 +00:00
let stages = StageData::load_stage_table(ctx, &game_ctx.base_path)?;
game_ctx.stages = stages;
} else if self.tick == 2 {
state.init(game_ctx, ctx)?;
state.switch_to_stage(53, game_ctx, ctx)?;
2020-08-18 16:46:07 +00:00
}
self.tick += 1;
Ok(())
}
2020-08-19 11:21:40 +00:00
fn draw(&self, state: &GameState, game_ctx: &mut GameContext, ctx: &mut Context) -> GameResult {
let loading = game_ctx.texture_set.tex_map.get_mut("Loading");
2020-08-18 16:46:07 +00:00
if loading.is_some() {
let img = loading.unwrap();
2020-08-19 11:21:40 +00:00
img.add(((game_ctx.canvas_size.0 - img.width() as f32) / 2.0).floor(),
((game_ctx.canvas_size.1 - img.height() as f32) / 2.0).floor());
2020-08-18 16:46:07 +00:00
img.draw(ctx)?;
}
Ok(())
}
}