Working
This commit is contained in:
parent
0b36cebbd2
commit
abe9b4ca38
@ -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<RefCell<InnerKeyboard>>,
|
||||
@ -75,6 +76,7 @@ impl Keyboard {
|
||||
}
|
||||
|
||||
/// The state of the keyboard.
|
||||
#[derive(Clone)]
|
||||
pub struct InnerKeyboard {
|
||||
/// Holds the state of the keys.
|
||||
keys: HashMap<Key, bool>,
|
||||
|
@ -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::<web_sys::CanvasRenderingContext2d>()?;
|
||||
|
||||
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<InnerEngine> {
|
||||
pub fn new(document: &web_sys::Document, scene: Scene) -> Result<InnerEngine> {
|
||||
log!("sup");
|
||||
Ok(InnerEngine {
|
||||
x: 0.0,
|
||||
y: 0.0,
|
||||
scene,
|
||||
keyboard: Keyboard::new(document)?,
|
||||
textures: TextureManager::new()?,
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user