Working on cleaning
This commit is contained in:
parent
e384751b18
commit
96f3b3f33c
@ -1,76 +1 @@
|
|||||||
//! A module that makes it easier to deal with images.
|
//! A module that makes it easier to deal with images.
|
||||||
|
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
use wasm_bindgen::prelude::*;
|
|
||||||
use wasm_bindgen::JsCast;
|
|
||||||
|
|
||||||
use web_sys::HtmlImageElement;
|
|
||||||
|
|
||||||
use crate::{log, Result};
|
|
||||||
|
|
||||||
/// A wrapper to make it easier to manage HTML images.
|
|
||||||
pub struct Image {
|
|
||||||
/// The inner image.
|
|
||||||
pub inner: Rc<RefCell<InnerImage>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The content of the image.
|
|
||||||
pub struct InnerImage {
|
|
||||||
/// The inner HtmlImageElement.
|
|
||||||
pub inner: HtmlImageElement,
|
|
||||||
|
|
||||||
/// Whether the image has been loaded or not.
|
|
||||||
pub loaded: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Image {
|
|
||||||
/// Loads a new image from its src.
|
|
||||||
pub fn new(path: &str) -> Result<Image> {
|
|
||||||
let image = HtmlImageElement::new()?;
|
|
||||||
image.set_src(path);
|
|
||||||
|
|
||||||
let image = Image {
|
|
||||||
inner: Rc::new(RefCell::new(InnerImage {
|
|
||||||
inner: image,
|
|
||||||
loaded: false,
|
|
||||||
})),
|
|
||||||
};
|
|
||||||
|
|
||||||
let clone = image.inner.clone();
|
|
||||||
let path_clone = path.to_string();
|
|
||||||
|
|
||||||
let cb = Closure::<dyn FnMut(_)>::new(move |_event: web_sys::Event| {
|
|
||||||
log!("{} loaded", path_clone);
|
|
||||||
(*clone).borrow_mut().loaded = true;
|
|
||||||
});
|
|
||||||
|
|
||||||
image
|
|
||||||
.inner
|
|
||||||
.borrow()
|
|
||||||
.inner
|
|
||||||
.set_onload(Some(cb.as_ref().unchecked_ref()));
|
|
||||||
|
|
||||||
cb.forget();
|
|
||||||
|
|
||||||
Ok(image)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns whether the image is loaded.
|
|
||||||
pub fn is_loaded(&self) -> bool {
|
|
||||||
self.inner.borrow().loaded
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Renders the image on a context.
|
|
||||||
pub fn render(
|
|
||||||
&self,
|
|
||||||
x: f64,
|
|
||||||
y: f64,
|
|
||||||
context: &web_sys::CanvasRenderingContext2d,
|
|
||||||
) -> Result<()> {
|
|
||||||
context.draw_image_with_html_image_element(&self.inner.borrow().inner, x, y)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -409,8 +409,8 @@ impl Map {
|
|||||||
/// Returns the new correct position.
|
/// Returns the new correct position.
|
||||||
pub fn collides_bbox(
|
pub fn collides_bbox(
|
||||||
&self,
|
&self,
|
||||||
old: FloatRect,
|
old: Bbox,
|
||||||
new: FloatRect,
|
new: Bbox,
|
||||||
) -> Option<(CollisionAxis, Vector2<f32>, Damage)> {
|
) -> Option<(CollisionAxis, Vector2<f32>, Damage)> {
|
||||||
let mut damage = Damage::None;
|
let mut damage = Damage::None;
|
||||||
|
|
||||||
@ -432,7 +432,7 @@ impl Map {
|
|||||||
let tile_left = col as f32 * SPRITE_SIZE_F32;
|
let tile_left = col as f32 * SPRITE_SIZE_F32;
|
||||||
let tile_top = row as f32 * SPRITE_SIZE_F32;
|
let tile_top = row as f32 * SPRITE_SIZE_F32;
|
||||||
|
|
||||||
let tile = FloatRect::new(tile_left, tile_top, SPRITE_SIZE_F32, SPRITE_SIZE_F32);
|
let tile = Bbox::new(tile_left, tile_top, SPRITE_SIZE_F32, SPRITE_SIZE_F32);
|
||||||
|
|
||||||
if !overlap(new, tile) {
|
if !overlap(new, tile) {
|
||||||
continue;
|
continue;
|
||||||
@ -518,9 +518,9 @@ impl Map {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Checks if two boxes overlap.
|
/// Checks if two boxes overlap.
|
||||||
pub fn overlap(box1: FloatRect, box2: FloatRect) -> bool {
|
pub fn overlap(box1: Bbox, box2: Bbox) -> bool {
|
||||||
box2.left < box1.left + box1.width
|
box2.position.x < box1.position.x + box1.size.x
|
||||||
&& box2.left + box2.width > box1.left
|
&& box2.position.x + box2.size.x > box1.position.x
|
||||||
&& box2.top < box1.top + box1.height
|
&& box2.position.y < box1.position.y + box1.size.y
|
||||||
&& box2.top + box2.height > box1.top
|
&& box2.position.y + box2.size.y > box1.position.y
|
||||||
}
|
}
|
||||||
|
@ -3,12 +3,11 @@
|
|||||||
pub mod bbox;
|
pub mod bbox;
|
||||||
pub mod character;
|
pub mod character;
|
||||||
pub mod controls;
|
pub mod controls;
|
||||||
pub mod controls;
|
|
||||||
pub mod event;
|
pub mod event;
|
||||||
pub mod image;
|
|
||||||
pub mod math;
|
pub mod math;
|
||||||
pub mod physics;
|
pub mod physics;
|
||||||
pub mod scene;
|
pub mod scene;
|
||||||
|
pub mod texture;
|
||||||
pub mod vector;
|
pub mod vector;
|
||||||
|
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
@ -18,7 +17,7 @@ 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::image::Image;
|
use crate::engine::texture::TextureManager;
|
||||||
use crate::{error_js, Result};
|
use crate::{error_js, Result};
|
||||||
|
|
||||||
macro_rules! unwrap {
|
macro_rules! unwrap {
|
||||||
@ -206,18 +205,3 @@ impl InnerEngine {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Our texture manager.
|
|
||||||
///
|
|
||||||
/// It holds all our resources.
|
|
||||||
pub struct TextureManager {
|
|
||||||
rusty: Image,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TextureManager {
|
|
||||||
/// Creates and start the loading of all our textures.
|
|
||||||
fn new() -> Result<TextureManager> {
|
|
||||||
let rusty = Image::new("static/textures/rusty.png")?;
|
|
||||||
Ok(TextureManager { rusty })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -4,7 +4,7 @@ use std::time::Duration;
|
|||||||
|
|
||||||
use crate::engine::character::Character;
|
use crate::engine::character::Character;
|
||||||
use crate::engine::map::Map;
|
use crate::engine::map::Map;
|
||||||
use crate::engine::texture::SPRITE_SIZE_F32;
|
use crate::engine::texture::SPRITE_SIZE;
|
||||||
|
|
||||||
/// Contains everything needed to play.
|
/// Contains everything needed to play.
|
||||||
pub struct Scene {
|
pub struct Scene {
|
||||||
@ -37,8 +37,8 @@ impl Scene {
|
|||||||
/// Adds a character to the scene.
|
/// Adds a character to the scene.
|
||||||
pub fn add(&mut self, character: Character) {
|
pub fn add(&mut self, character: Character) {
|
||||||
let mut character = character;
|
let mut character = character;
|
||||||
character.position.x = self.map.entrance().1 as f32 * SPRITE_SIZE_F32;
|
character.position.x = self.map.entrance().1 * SPRITE_SIZE;
|
||||||
character.position.y = self.map.entrance().0 as f32 * SPRITE_SIZE_F32;
|
character.position.y = self.map.entrance().0 * SPRITE_SIZE;
|
||||||
|
|
||||||
self.characters.push(character);
|
self.characters.push(character);
|
||||||
}
|
}
|
||||||
|
95
src/engine/texture.rs
Normal file
95
src/engine/texture.rs
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
//! This module holds everything related to textures.
|
||||||
|
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
use wasm_bindgen::JsCast;
|
||||||
|
|
||||||
|
use web_sys::HtmlImageElement;
|
||||||
|
|
||||||
|
use crate::{log, Result};
|
||||||
|
|
||||||
|
/// The size of sprites.
|
||||||
|
pub const SPRITE_SIZE: f64 = 32.0;
|
||||||
|
|
||||||
|
/// A wrapper to make it easier to manage HTML images.
|
||||||
|
pub struct Image {
|
||||||
|
/// The inner image.
|
||||||
|
pub inner: Rc<RefCell<InnerImage>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The content of the image.
|
||||||
|
pub struct InnerImage {
|
||||||
|
/// The inner HtmlImageElement.
|
||||||
|
pub inner: HtmlImageElement,
|
||||||
|
|
||||||
|
/// Whether the image has been loaded or not.
|
||||||
|
pub loaded: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Image {
|
||||||
|
/// Loads a new image from its src.
|
||||||
|
pub fn new(path: &str) -> Result<Image> {
|
||||||
|
let image = HtmlImageElement::new()?;
|
||||||
|
image.set_src(path);
|
||||||
|
|
||||||
|
let image = Image {
|
||||||
|
inner: Rc::new(RefCell::new(InnerImage {
|
||||||
|
inner: image,
|
||||||
|
loaded: false,
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
|
||||||
|
let clone = image.inner.clone();
|
||||||
|
let path_clone = path.to_string();
|
||||||
|
|
||||||
|
let cb = Closure::<dyn FnMut(_)>::new(move |_event: web_sys::Event| {
|
||||||
|
log!("{} loaded", path_clone);
|
||||||
|
(*clone).borrow_mut().loaded = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
image
|
||||||
|
.inner
|
||||||
|
.borrow()
|
||||||
|
.inner
|
||||||
|
.set_onload(Some(cb.as_ref().unchecked_ref()));
|
||||||
|
|
||||||
|
cb.forget();
|
||||||
|
|
||||||
|
Ok(image)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns whether the image is loaded.
|
||||||
|
pub fn is_loaded(&self) -> bool {
|
||||||
|
self.inner.borrow().loaded
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Renders the image on a context.
|
||||||
|
pub fn render(
|
||||||
|
&self,
|
||||||
|
x: f64,
|
||||||
|
y: f64,
|
||||||
|
context: &web_sys::CanvasRenderingContext2d,
|
||||||
|
) -> Result<()> {
|
||||||
|
context.draw_image_with_html_image_element(&self.inner.borrow().inner, x, y)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Our texture manager.
|
||||||
|
///
|
||||||
|
/// It holds all our resources.
|
||||||
|
pub struct TextureManager {
|
||||||
|
/// The main character sprite.
|
||||||
|
pub rusty: Image,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TextureManager {
|
||||||
|
/// Creates and start the loading of all our textures.
|
||||||
|
pub fn new() -> Result<TextureManager> {
|
||||||
|
let rusty = Image::new("static/textures/rusty.png")?;
|
||||||
|
Ok(TextureManager { rusty })
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user