This commit is contained in:
Thomas Forgione 2022-07-30 14:55:40 +02:00
parent bc90055639
commit 0b36cebbd2
5 changed files with 47 additions and 28 deletions

View File

@ -3,8 +3,8 @@
use std::time::{Duration, Instant};
use crate::engine::bbox::Bbox;
use crate::engine::controls::Controls;
use crate::engine::event::Keyboard;
use crate::engine::controls::{Action, Controls};
use crate::engine::event::{Event, Keyboard};
use crate::engine::math::{clamp, duration_as_f64};
use crate::engine::physics;
use crate::engine::scene::Updatable;
@ -188,26 +188,26 @@ impl Updatable for Character {
self.position += self.speed * duration;
}
// fn manage_event(&mut self, event: &Event) {
// let action = if let Some(ref controls) = self.controls {
// controls.convert(event)
// } else {
// None
// };
fn manage_event(&mut self, event: &Event) {
let action = if let Some(ref controls) = self.controls {
controls.convert(event)
} else {
None
};
// match action {
// Some(Action::Jump(true)) => {
// self.jump();
// self.can_jump = false;
// }
match action {
Some(Action::Jump(true)) => {
self.jump();
self.can_jump = false;
}
// Some(Action::Jump(false)) => {
// self.can_jump = true;
// }
Some(Action::Jump(false)) => {
self.can_jump = true;
}
// _ => (),
// }
// }
_ => (),
}
}
}
// impl Drawable for Character {

View File

@ -18,6 +18,8 @@ use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use crate::engine::event::{Key, Keyboard};
use crate::engine::map::Map;
use crate::engine::scene::Scene;
use crate::engine::texture::TextureManager;
use crate::{error_js, Result};
@ -67,6 +69,9 @@ impl Engine {
let document = Rc::new(document);
let context = Rc::new(context);
let map = Map::from_str(include_str!("../../static/levels/level1.lvl")).unwrap();
let scene = Scene::from_map(map);
Ok(Engine {
inner: Rc::new(RefCell::new(inner)),
document,

View File

@ -3,7 +3,7 @@
use std::time::Duration;
use crate::engine::character::Character;
use crate::engine::event::Keyboard;
use crate::engine::event::{Event, Keyboard};
use crate::engine::map::Map;
use crate::engine::texture::SPRITE_SIZE;
@ -123,12 +123,12 @@ impl Scene {
state
}
// /// Transfers an event to the elements contained in the scene that should receive events.
// pub fn manage_event(&mut self, event: &Event) {
// for c in &mut self.characters {
// c.manage_event(event);
// }
// }
/// Transfers an event to the elements contained in the scene that should receive events.
pub fn manage_event(&mut self, event: &Event) {
for c in &mut self.characters {
c.manage_event(event);
}
}
/// Returns a reference to the characters of the scene.
pub fn characters(&self) -> &Vec<Character> {
@ -146,6 +146,6 @@ pub trait Updatable {
/// Updates the thing depending on the duration since last frame.
fn update(&mut self, duration: &Duration, keyboard: &Keyboard);
// /// Called when an event arrives.
// fn manage_event(&mut self, event: &Event);
/// Called when an event arrives.
fn manage_event(&mut self, event: &Event);
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 583 KiB

14
static/levels/level1.lvl Normal file
View File

@ -0,0 +1,14 @@
13 24
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1