1
0
Fork 0
mirror of https://github.com/doukutsu-rs/doukutsu-rs synced 2024-11-16 19:02:47 +00:00
doukutsu-rs/src/live_debugger.rs

113 lines
4.2 KiB
Rust
Raw Normal View History

2020-08-19 13:11:34 +00:00
use imgui::{Condition, im_str, ImStr, ImString, Window};
use itertools::Itertools;
2020-08-19 11:21:40 +00:00
2020-08-20 18:31:47 +00:00
use crate::ggez::{Context, GameResult};
2020-08-19 13:11:34 +00:00
use crate::scene::game_scene::GameScene;
use crate::SharedGameState;
2020-08-19 11:21:40 +00:00
pub struct LiveDebugger {
selected_item: i32,
2020-08-20 18:31:47 +00:00
map_selector_visible: bool,
2020-08-23 02:16:31 +00:00
hacks_visible: bool,
2020-08-19 13:11:34 +00:00
stages: Vec<ImString>,
error: Option<ImString>,
2020-08-19 11:21:40 +00:00
}
impl LiveDebugger {
pub fn new() -> Self {
Self {
2020-08-19 13:11:34 +00:00
selected_item: -1,
2020-08-20 18:31:47 +00:00
map_selector_visible: false,
2020-08-23 02:16:31 +00:00
hacks_visible: false,
2020-08-19 13:11:34 +00:00
stages: vec![],
error: None,
2020-08-19 11:21:40 +00:00
}
}
2020-08-19 13:11:34 +00:00
pub fn run_ingame(&mut self, game_scene: &mut GameScene, state: &mut SharedGameState, ctx: &mut Context, ui: &mut imgui::Ui) -> GameResult {
2020-08-23 02:16:31 +00:00
Window::new(im_str!("Debugger"))
2020-08-20 18:31:47 +00:00
.position([5.0, 5.0], Condition::FirstUseEver)
2020-08-23 02:16:31 +00:00
.size([300.0, 120.0], Condition::FirstUseEver)
2020-08-19 11:21:40 +00:00
.build(ui, || {
ui.text(format!(
"Player position: ({:.1},{:.1})",
2020-08-20 18:31:47 +00:00
game_scene.player.x as f32 / 512.0,
game_scene.player.y as f32 / 512.0,
));
ui.text(format!(
"Player velocity: ({:.1},{:.1})",
game_scene.player.vel_x as f32 / 512.0,
game_scene.player.vel_y as f32 / 512.0,
));
ui.text(format!(
"Booster fuel: ({})", game_scene.player.booster_fuel
2020-08-19 11:21:40 +00:00
));
2020-08-20 18:31:47 +00:00
if ui.button(im_str!("Map Selector"), [0.0, 0.0]) {
self.map_selector_visible = true;
}
2020-08-23 02:16:31 +00:00
if ui.button(im_str!("Hacks"), [0.0, 0.0]) {
self.hacks_visible = true;
}
2020-08-20 18:31:47 +00:00
});
2020-08-19 13:11:34 +00:00
if self.error.is_some() {
Window::new(im_str!("Error!"))
.resizable(false)
.collapsible(false)
2020-08-19 19:11:32 +00:00
.position([((state.screen_size.0 - 300.0) / 2.0).floor(), ((state.screen_size.1 - 100.0) / 2.0).floor()], Condition::Appearing)
.size([300.0, 100.0], Condition::Appearing)
2020-08-19 13:11:34 +00:00
.build(ui, || {
ui.push_item_width(-1.0);
ui.text(self.error.as_ref().unwrap());
if ui.button(im_str!("OK"), [0.0, 0.0]) {
self.error = None;
}
});
}
2020-08-19 11:21:40 +00:00
2020-08-20 18:31:47 +00:00
if self.map_selector_visible {
Window::new(im_str!("Map selector"))
.resizable(false)
.position([5.0, 35.0], Condition::FirstUseEver)
.size([240.0, 280.0], Condition::FirstUseEver)
.build(ui, || {
if self.stages.is_empty() {
for s in state.stages.iter() {
self.stages.push(ImString::new(s.name.to_owned()));
}
2020-08-19 13:11:34 +00:00
2020-08-20 18:31:47 +00:00
self.selected_item = match state.stages.iter().find_position(|s| s.name == game_scene.stage.data.name) {
Some((pos, _)) => { pos as i32 }
_ => { -1 }
};
}
let stages: Vec<&ImStr> = self.stages.iter().map(|e| e.as_ref()).collect();
2020-08-19 13:11:34 +00:00
2020-08-20 18:31:47 +00:00
ui.push_item_width(-1.0);
ui.list_box(im_str!(""), &mut self.selected_item, &stages, 10);
2020-08-19 13:11:34 +00:00
2020-08-20 18:31:47 +00:00
if ui.button(im_str!("Load"), [0.0, 0.0]) {
match GameScene::new(state, ctx, self.selected_item as usize) {
Ok(mut scene) => {
scene.player.x = (scene.stage.map.width / 2 * 16 * 0x200) as isize;
scene.player.y = (scene.stage.map.height / 2 * 16 * 0x200) as isize;
state.next_scene = Some(Box::new(scene));
}
Err(e) => {
log::error!("Error loading map: {:?}", e);
2020-08-20 18:31:47 +00:00
self.error = Some(ImString::new(e.to_string()));
}
2020-08-19 13:11:34 +00:00
}
}
2020-08-20 18:31:47 +00:00
});
}
2020-08-19 11:21:40 +00:00
Ok(())
}
}