Die when fall off, restart, fix new tab glitch

This commit is contained in:
Thomas Forgione 2022-08-01 13:27:43 +02:00
parent 7e9bad3286
commit 2858e3d5ec
2 changed files with 21 additions and 15 deletions

View File

@ -141,19 +141,27 @@ impl Engine {
pub fn update(&self) -> Result<()> {
let mut inner = self.inner.borrow_mut();
// Manage events
while let Some(event) = inner.keyboard.pop() {
inner.scene.manage_event(&event);
}
// Manage the physics
let now = now(&self.performance);
let duration = unwrap!(now.duration_since(inner.after_loop).ok());
inner.after_loop = now;
let keyboard = inner.keyboard.clone();
if inner.scene.update(now, duration, &keyboard) == State::Finished {
// running = false;
if (self.document.has_focus()?) {
// Manage events
while let Some(event) = inner.keyboard.pop() {
inner.scene.manage_event(&event);
}
let keyboard = inner.keyboard.clone();
if inner.scene.update(now, duration, &keyboard) == State::Finished {
let map = Map::from_str(include_str!("../../static/levels/level1.lvl")).unwrap();
let mut scene = Scene::from_map(map);
let character = Character::with_controls(Controls::default_keyboard());
scene.add(character);
inner.scene = scene;
}
}
Ok(())

View File

@ -86,7 +86,7 @@ impl Scene {
/// Updates the whole scene.
pub fn update(&mut self, now: SystemTime, duration: Duration, keyboard: &Keyboard) -> State {
let mut state = State::Finished;
let mut state = State::Running;
for c in &mut self.characters {
// Don't need to update if the character is dead
@ -109,15 +109,13 @@ impl Scene {
c.ground_collision();
}
// c.die();
if (damage) {
c.die();
state = State::Finished;
}
} else {
c.fall_off();
}
// If a character is alive and still have controls, the game should continue
if c.controls().is_some() {
state = State::Running;
}
}
}