A 2d game engine for WebAssembly. This is a project in development. It works now, but it can dramatically change.
```rust use crate::{ canvas::Canvas, gameloop::GameLoop, gamestate::GameState, image::{imageid::ImageId, imagerepository::ImageRepository, Image}, key_event::{KeyEvent, KeyboardEvent}, point::{Dot, Point}, sprite::Sprite, }; use std::{marker::PhantomData, rc::Rc};
// TestGameState struct TestGameState { data1: GameObject, data2: GameObject, }
impl GameState
impl TestGameState { fn new() -> Self { Self { data1: GameObject::new(ImageId(0), GamePoint::new(32, 32)), data2: GameObject::new(ImageId(1), GamePoint::new(32, 32)), } } }
// GameObject
struct GameObject {
image_id: ImageId,
size: GamePoint
impl Sprite
fn size(&self) -> &GamePoint<Dot> {
&self.size
}
}
impl GameObject {
fn new(imageid: ImageId, size: GamePoint
// GamePoint
struct GamePoint
impl
fn x(&self) -> &i64 {
&self.x
}
fn y(&self) -> &i64 {
&self.y
}
}
fn main() { let testgamestate = TestGameState::new(); let canvas = Canvas::new("main-canvas", GamePoint::new(480, 480), "game-container");
// image_repository factory
let image_repository = {
let new_html_image_element_rc =
Rc::new(Image::<GamePoint<Dot>>::create_new_html_image_element(&[], "gif").unwrap());
let image_repository = ImageRepository::new();
image_repository
.save(Image::new(
ImageId(0),
new_html_image_element_rc.clone(),
GamePoint::new(64, 32),
GamePoint::new(32, 32),
))
.unwrap();
image_repository
.save(Image::new(
ImageId(1),
new_html_image_element_rc.clone(),
GamePoint::new(64, 64),
GamePoint::new(32, 32),
))
.unwrap();
image_repository
};
GameLoop::run(test_game_state, image_repository, canvas.unwrap()).unwrap();
} ```