diff --git a/src/engine/event.rs b/src/engine/event.rs index 5b31547..b8221e3 100644 --- a/src/engine/event.rs +++ b/src/engine/event.rs @@ -10,6 +10,7 @@ use wasm_bindgen::JsCast; use crate::Result; /// The state of the keyboard. +#[derive(Clone)] pub struct Keyboard { /// The inner keyboard. inner: Rc>, @@ -75,6 +76,7 @@ impl Keyboard { } /// The state of the keyboard. +#[derive(Clone)] pub struct InnerKeyboard { /// Holds the state of the keys. keys: HashMap, diff --git a/src/engine/mod.rs b/src/engine/mod.rs index 7685e10..761b5d9 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -13,15 +13,16 @@ pub mod vector; use std::cell::RefCell; use std::rc::Rc; +use std::time::Duration; 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::scene::{Scene, State}; use crate::engine::texture::TextureManager; -use crate::{error_js, Result}; +use crate::{error_js, log, Result}; macro_rules! unwrap { ($t: expr) => {{ @@ -33,6 +34,7 @@ macro_rules! unwrap { } /// Our game engine. +#[derive(Clone)] pub struct Engine { /// The inner engine. /// @@ -64,14 +66,13 @@ impl Engine { .unwrap() .dyn_into::()?; - let inner = InnerEngine::new(&document)?; + let map = Map::from_str(include_str!("../../static/levels/level1.lvl")).unwrap(); + let scene = Scene::from_map(map); + let inner = InnerEngine::new(&document, scene)?; 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, @@ -79,17 +80,6 @@ impl Engine { }) } - /// Clones the engine. - /// - /// It is made to be efficient, cloning only Rcs. - pub fn clone(&self) -> Engine { - Engine { - inner: self.inner.clone(), - document: self.document.clone(), - context: self.context.clone(), - } - } - /// Starts the engine. pub fn start(&self) -> Result<()> { let clone = self.clone(); @@ -140,8 +130,16 @@ impl Engine { let mut inner = self.inner.borrow_mut(); // Manage events - while let Some(_event) = inner.keyboard.pop() { - // Nothing to do here yet + while let Some(event) = inner.keyboard.pop() { + inner.scene.manage_event(&event); + } + + // Manage the physics + let duration = Duration::from_millis(60); + + let keyboard = inner.keyboard.clone(); + if inner.scene.update(&duration, &keyboard) == State::Finished { + // running = false; } // Perform update @@ -193,6 +191,9 @@ pub struct InnerEngine { /// The y position of the drawing. pub y: f64, + /// The scene of the engine. + pub scene: Scene, + /// The keyboard. pub keyboard: Keyboard, @@ -202,10 +203,12 @@ pub struct InnerEngine { impl InnerEngine { /// Initializes the engine. - pub fn new(document: &web_sys::Document) -> Result { + pub fn new(document: &web_sys::Document, scene: Scene) -> Result { + log!("sup"); Ok(InnerEngine { x: 0.0, y: 0.0, + scene, keyboard: Keyboard::new(document)?, textures: TextureManager::new()?, })