show fail popup when output is wrong

This commit is contained in:
Crispy 2024-12-09 21:40:24 +01:00
parent 14b87e6971
commit 70c745117f

View file

@ -56,8 +56,7 @@ pub struct Editor {
time_since_step: f32,
exit_state: ExitState,
exit_menu: bool,
complete_popup: Popup,
// fail_popup: Popup,
popup: EndPopup,
score: Option<Score>,
blueprints: Vec<Blueprint>,
selected_blueprint: usize,
@ -69,9 +68,10 @@ pub struct Editor {
}
#[derive(Debug, PartialEq)]
enum Popup {
Start,
Visible,
enum EndPopup {
None,
Success,
Failure,
Dismissed,
}
@ -135,8 +135,7 @@ impl Editor {
level,
exit_state: ExitState::Dont,
exit_menu: false,
complete_popup: Popup::Start,
// fail_popup: Popup::Start,
popup: EndPopup::None,
score: solution.score,
blueprints: get_blueprints(),
selected_blueprint: usize::MAX,
@ -190,21 +189,25 @@ impl Editor {
fn step(&mut self) {
self.machine.step();
if self.complete_popup == Popup::Visible {
self.complete_popup = Popup::Dismissed;
if self.popup != EndPopup::None {
self.popup = EndPopup::Dismissed;
}
if !self.level.outputs().is_empty()
&& self.level.outputs() == self.machine.output()
&& self.complete_popup == Popup::Start
&& self.popup == EndPopup::None
{
self.complete_popup = Popup::Visible;
println!("completed in {:?}", self.start_time.elapsed());
self.exit_state = ExitState::Save;
self.sim_state = SimState::Stepping;
self.score = Some(Score {
cycles: self.machine.step_count(),
tiles: self.source_board.count_tiles(),
});
if self.level.outputs() == self.machine.output() {
self.popup = EndPopup::Success;
println!("completed in {:?}", self.start_time.elapsed());
self.exit_state = ExitState::Save;
self.sim_state = SimState::Stepping;
self.score = Some(Score {
cycles: self.machine.step_count(),
tiles: self.source_board.count_tiles(),
});
} else if !self.level.outputs().starts_with(self.machine.output()) {
self.popup = EndPopup::Failure;
self.sim_state = SimState::Stepping;
}
}
}
@ -332,7 +335,7 @@ impl Editor {
pub fn update(&mut self, rl: &RaylibHandle) {
if rl.is_key_pressed(KeyboardKey::KEY_ESCAPE) {
self.sim_state = SimState::Editing;
self.complete_popup = Popup::Start;
self.popup = EndPopup::None;
}
if self.sim_state == SimState::Running {
self.time_since_step += rl.get_frame_time();
@ -373,7 +376,7 @@ impl Editor {
}
SimState::Running => {
self.sim_state = SimState::Editing;
self.complete_popup = Popup::Start;
self.popup = EndPopup::None;
}
SimState::Stepping => self.sim_state = SimState::Running,
}
@ -510,7 +513,7 @@ impl Editor {
}
}
if self.complete_popup == Popup::Visible {
if self.popup == EndPopup::Success {
let width = 320;
let height = 165;
let x = d.get_screen_width() / 2 - width / 2;
@ -524,7 +527,7 @@ impl Editor {
draw_usize(d, textures, score.tiles, x + 210, y + 70, 5, 2);
}
if simple_button(d, x + 10, y + 110, 140, 45) {
self.complete_popup = Popup::Dismissed;
self.popup = EndPopup::Dismissed;
}
d.draw_text("continue\nediting", x + 15, y + 115, 20, Color::WHITE);
@ -539,6 +542,20 @@ impl Editor {
Color::WHITE,
);
}
if self.popup == EndPopup::Failure {
let width = 320;
let height = 165;
let x = d.get_screen_width() / 2 - width / 2;
let y = d.get_screen_height() / 2 - height / 2;
d.draw_rectangle(x, y, width, height, BG_DARK);
d.draw_text("Level Failed!", x + 45, y + 10, 30, Color::RED);
d.draw_text("incorrect output", x + 45, y + 45, 20, Color::WHITE);
if simple_button(d, x + 10, y + 110, 300, 25) {
self.popup = EndPopup::Dismissed;
}
d.draw_text("ok :(", x + 15, y + 115, 20, Color::WHITE);
}
}
fn draw_top_bar(&mut self, d: &mut RaylibDrawHandle, textures: &Textures) {
@ -590,7 +607,7 @@ impl Editor {
draw_scaled_texture(d, textures.get("pause"), 260, 4, 2.);
if simple_button(d, 296, 4, 32, 32) {
self.sim_state = SimState::Editing;
self.complete_popup = Popup::Start;
self.popup = EndPopup::None;
}
draw_scaled_texture(d, textures.get("stop"), 296, 4, 2.);
}
@ -601,7 +618,7 @@ impl Editor {
draw_scaled_texture(d, textures.get("play"), 260, 4, 2.);
if simple_button(d, 296, 4, 32, 32) {
self.sim_state = SimState::Editing;
self.complete_popup = Popup::Start;
self.popup = EndPopup::None;
}
draw_scaled_texture(d, textures.get("stop"), 296, 4, 2.);
}