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.
#[derive(Clone)]
pub struct Engine {
/// The inner engine.
///
/// We need Rc<RefCell> in order to deal with events.
pub inner: Rc<RefCell<InnerEngine>>,
}
pub struct Game(Rc<RefCell<Engine>>);
impl Engine {
/// Creates a new engine.
pub fn new() -> Result<Engine> {
let inner = Rc::new(RefCell::new(InnerEngine::new()?));
impl Game {
/// Creates a new game.
pub fn new() -> Result<Game> {
Ok(Game(Rc::new(RefCell::new(Engine::new()?))))
// let clone = inner.clone();
// 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())?;
// cb.forget();
Ok(Engine { inner })
}
/// Starts the engine.
/// Starts the game.
pub fn start(&self) -> Result<()> {
let clone = self.clone();
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
.window
.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.
pub fn run(&self) -> Result<()> {
let mut inner = self.inner.borrow_mut();
let mut inner = self.0.borrow_mut();
// Perform update
inner.update()?;
@ -114,7 +107,7 @@ impl Engine {
}
/// The data contained in our engine.
pub struct InnerEngine {
pub struct Engine {
/// The scene of the engine.
pub scene: Scene,
@ -142,9 +135,9 @@ pub struct InnerEngine {
pub performance: web_sys::Performance,
}
impl InnerEngine {
impl Engine {
/// Initializes the engine.
pub fn new() -> Result<InnerEngine> {
pub fn new() -> Result<Engine> {
let window = unwrap!(web_sys::window());
let document = unwrap!(window.document());
let performance = unwrap!(window.performance());
@ -163,7 +156,7 @@ impl InnerEngine {
let character = Character::new();
scene.add(character);
Ok(InnerEngine {
Ok(Engine {
scene,
after_loop: now(&performance),
textures: TextureManager::new()?,

View File

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