Working
This commit is contained in:
parent
0b36cebbd2
commit
abe9b4ca38
@ -10,6 +10,7 @@ use wasm_bindgen::JsCast;
|
|||||||
use crate::Result;
|
use crate::Result;
|
||||||
|
|
||||||
/// The state of the keyboard.
|
/// The state of the keyboard.
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct Keyboard {
|
pub struct Keyboard {
|
||||||
/// The inner keyboard.
|
/// The inner keyboard.
|
||||||
inner: Rc<RefCell<InnerKeyboard>>,
|
inner: Rc<RefCell<InnerKeyboard>>,
|
||||||
@ -75,6 +76,7 @@ impl Keyboard {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// The state of the keyboard.
|
/// The state of the keyboard.
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct InnerKeyboard {
|
pub struct InnerKeyboard {
|
||||||
/// Holds the state of the keys.
|
/// Holds the state of the keys.
|
||||||
keys: HashMap<Key, bool>,
|
keys: HashMap<Key, bool>,
|
||||||
|
@ -13,15 +13,16 @@ pub mod vector;
|
|||||||
|
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
use wasm_bindgen::JsCast;
|
use wasm_bindgen::JsCast;
|
||||||
|
|
||||||
use crate::engine::event::{Key, Keyboard};
|
use crate::engine::event::{Key, Keyboard};
|
||||||
use crate::engine::map::Map;
|
use crate::engine::map::Map;
|
||||||
use crate::engine::scene::Scene;
|
use crate::engine::scene::{Scene, State};
|
||||||
use crate::engine::texture::TextureManager;
|
use crate::engine::texture::TextureManager;
|
||||||
use crate::{error_js, Result};
|
use crate::{error_js, log, Result};
|
||||||
|
|
||||||
macro_rules! unwrap {
|
macro_rules! unwrap {
|
||||||
($t: expr) => {{
|
($t: expr) => {{
|
||||||
@ -33,6 +34,7 @@ macro_rules! unwrap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Our game engine.
|
/// Our game engine.
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct Engine {
|
pub struct Engine {
|
||||||
/// The inner engine.
|
/// The inner engine.
|
||||||
///
|
///
|
||||||
@ -64,14 +66,13 @@ impl Engine {
|
|||||||
.unwrap()
|
.unwrap()
|
||||||
.dyn_into::<web_sys::CanvasRenderingContext2d>()?;
|
.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 document = Rc::new(document);
|
||||||
let context = Rc::new(context);
|
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 {
|
Ok(Engine {
|
||||||
inner: Rc::new(RefCell::new(inner)),
|
inner: Rc::new(RefCell::new(inner)),
|
||||||
document,
|
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.
|
/// Starts the engine.
|
||||||
pub fn start(&self) -> Result<()> {
|
pub fn start(&self) -> Result<()> {
|
||||||
let clone = self.clone();
|
let clone = self.clone();
|
||||||
@ -140,8 +130,16 @@ impl Engine {
|
|||||||
let mut inner = self.inner.borrow_mut();
|
let mut inner = self.inner.borrow_mut();
|
||||||
|
|
||||||
// Manage events
|
// Manage events
|
||||||
while let Some(_event) = inner.keyboard.pop() {
|
while let Some(event) = inner.keyboard.pop() {
|
||||||
// Nothing to do here yet
|
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
|
// Perform update
|
||||||
@ -193,6 +191,9 @@ pub struct InnerEngine {
|
|||||||
/// The y position of the drawing.
|
/// The y position of the drawing.
|
||||||
pub y: f64,
|
pub y: f64,
|
||||||
|
|
||||||
|
/// The scene of the engine.
|
||||||
|
pub scene: Scene,
|
||||||
|
|
||||||
/// The keyboard.
|
/// The keyboard.
|
||||||
pub keyboard: Keyboard,
|
pub keyboard: Keyboard,
|
||||||
|
|
||||||
@ -202,10 +203,12 @@ pub struct InnerEngine {
|
|||||||
|
|
||||||
impl InnerEngine {
|
impl InnerEngine {
|
||||||
/// Initializes the engine.
|
/// 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 {
|
Ok(InnerEngine {
|
||||||
x: 0.0,
|
x: 0.0,
|
||||||
y: 0.0,
|
y: 0.0,
|
||||||
|
scene,
|
||||||
keyboard: Keyboard::new(document)?,
|
keyboard: Keyboard::new(document)?,
|
||||||
textures: TextureManager::new()?,
|
textures: TextureManager::new()?,
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user