More cleaning

This commit is contained in:
Thomas Forgione 2022-08-02 14:35:44 +02:00
parent 27bcd3a5d2
commit f528fdc1b9
2 changed files with 14 additions and 21 deletions

View File

@ -40,17 +40,12 @@ macro_rules! unwrap {
/// Our game engine. /// Our game engine.
#[derive(Clone)] #[derive(Clone)]
pub struct Engine { pub struct Game(Rc<RefCell<Engine>>);
/// The inner engine.
///
/// We need Rc<RefCell> in order to deal with events.
pub inner: Rc<RefCell<InnerEngine>>,
}
impl Engine { impl Game {
/// Creates a new engine. /// Creates a new game.
pub fn new() -> Result<Engine> { pub fn new() -> Result<Game> {
let inner = Rc::new(RefCell::new(InnerEngine::new()?)); Ok(Game(Rc::new(RefCell::new(Engine::new()?))))
// let clone = inner.clone(); // let clone = inner.clone();
// let cb = Closure::<dyn FnMut(_)>::new(move |event: web_sys::GamepadEvent| { // let cb = Closure::<dyn FnMut(_)>::new(move |event: web_sys::GamepadEvent| {
@ -62,11 +57,9 @@ impl Engine {
// .add_event_listener_with_callback("gamepadconnected", cb.as_ref().unchecked_ref())?; // .add_event_listener_with_callback("gamepadconnected", cb.as_ref().unchecked_ref())?;
// cb.forget(); // cb.forget();
Ok(Engine { inner })
} }
/// Starts the engine. /// Starts the game.
pub fn start(&self) -> Result<()> { pub fn start(&self) -> Result<()> {
let clone = self.clone(); let clone = self.clone();
let cb = Closure::<dyn FnMut(_)>::new(move |_event: web_sys::Event| { let cb = Closure::<dyn FnMut(_)>::new(move |_event: web_sys::Event| {
@ -75,7 +68,7 @@ impl Engine {
} }
}); });
let inner = self.inner.borrow_mut(); let inner = self.0.borrow_mut();
inner inner
.window .window
.request_animation_frame(cb.as_ref().unchecked_ref())?; .request_animation_frame(cb.as_ref().unchecked_ref())?;
@ -87,7 +80,7 @@ impl Engine {
/// Launches a loop of the engine, and schedules the next one. /// Launches a loop of the engine, and schedules the next one.
pub fn run(&self) -> Result<()> { pub fn run(&self) -> Result<()> {
let mut inner = self.inner.borrow_mut(); let mut inner = self.0.borrow_mut();
// Perform update // Perform update
inner.update()?; inner.update()?;
@ -114,7 +107,7 @@ impl Engine {
} }
/// The data contained in our engine. /// The data contained in our engine.
pub struct InnerEngine { pub struct Engine {
/// The scene of the engine. /// The scene of the engine.
pub scene: Scene, pub scene: Scene,
@ -142,9 +135,9 @@ pub struct InnerEngine {
pub performance: web_sys::Performance, pub performance: web_sys::Performance,
} }
impl InnerEngine { impl Engine {
/// Initializes the engine. /// Initializes the engine.
pub fn new() -> Result<InnerEngine> { pub fn new() -> Result<Engine> {
let window = unwrap!(web_sys::window()); let window = unwrap!(web_sys::window());
let document = unwrap!(window.document()); let document = unwrap!(window.document());
let performance = unwrap!(window.performance()); let performance = unwrap!(window.performance());
@ -163,7 +156,7 @@ impl InnerEngine {
let character = Character::new(); let character = Character::new();
scene.add(character); scene.add(character);
Ok(InnerEngine { Ok(Engine {
scene, scene,
after_loop: now(&performance), after_loop: now(&performance),
textures: TextureManager::new()?, textures: TextureManager::new()?,

View File

@ -61,8 +61,8 @@ pub(crate) use log;
pub(crate) use log_js; pub(crate) use log_js;
pub fn run() -> Result<()> { pub fn run() -> Result<()> {
let engine = engine::Engine::new()?; let game = engine::Game::new()?;
engine.start()?; game.start()?;
Ok(()) Ok(())
} }