mirror of
https://github.com/thilo-behnke/wasm-pong.git
synced 2026-07-12 11:09:38 +00:00
175 lines
4.5 KiB
Rust
175 lines
4.5 KiB
Rust
mod utils;
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
use serde::{Serialize, Deserialize};
|
|
use serde_json::json;
|
|
|
|
extern crate serde_json;
|
|
extern crate web_sys;
|
|
|
|
// A macro to provide `println!(..)`-style syntax for `console.log` logging.
|
|
macro_rules! log {
|
|
( $( $t:tt )* ) => {
|
|
web_sys::console::log_1(&format!( $( $t )* ).into());
|
|
}
|
|
}
|
|
|
|
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
|
|
// allocator.
|
|
#[cfg(feature = "wee_alloc")]
|
|
#[global_allocator]
|
|
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
|
|
|
|
#[wasm_bindgen]
|
|
pub struct Field {
|
|
pub width: u16,
|
|
pub height: u16,
|
|
players: Vec<Player>,
|
|
balls: Vec<Ball>,
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
#[repr(u8)]
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum Shape {
|
|
Rect = 0, Circle = 1
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
#[repr(packed)]
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
|
|
pub struct GameObject {
|
|
pub id: u16,
|
|
pub x: u16,
|
|
pub y: u16,
|
|
pub shape: u16,
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub struct Player {
|
|
pub obj: GameObject,
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub struct Ball {
|
|
pub obj: GameObject,
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub enum InputType {
|
|
UP,
|
|
DOWN,
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub struct Input {
|
|
pub input: InputType,
|
|
pub obj_id: u16,
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
impl Field {
|
|
pub fn new() -> Field {
|
|
let width = 800;
|
|
let height = 600;
|
|
|
|
Field {
|
|
width,
|
|
height,
|
|
players: vec![
|
|
Player {
|
|
obj: GameObject { id: 0, x: 0 + width / 20, y: height / 2, shape: (width / 25) << 8 | (height / 5) },
|
|
},
|
|
Player {
|
|
obj: GameObject { id: 1, x: width - width / 20, y: height / 2, shape: (width / 25) << 8 | (height / 5)},
|
|
}
|
|
],
|
|
balls: vec![
|
|
Ball {
|
|
obj: GameObject {id: 2, x: width / 2, y: height / 2, shape: (width / 50) << 8}
|
|
}
|
|
],
|
|
}
|
|
}
|
|
|
|
pub fn tick(&mut self, inputs_js: &JsValue) {
|
|
let inputs: Vec<Input> = inputs_js.into_serde().unwrap();
|
|
self.tick_inner(inputs);
|
|
}
|
|
|
|
pub fn objects(&self) -> *const GameObject {
|
|
let mut objs = vec![];
|
|
objs.append(
|
|
&mut self
|
|
.balls
|
|
.iter()
|
|
.map(|ball| ball.obj)
|
|
.collect::<Vec<GameObject>>(),
|
|
);
|
|
objs.append(
|
|
&mut self
|
|
.players
|
|
.iter()
|
|
.map(|player| player.obj)
|
|
.collect::<Vec<GameObject>>(),
|
|
);
|
|
objs.as_ptr()
|
|
}
|
|
|
|
pub fn get_state(&self) -> String {
|
|
let json = json!(GameObject {shape: 0, x: 10, y: 10, id: 1});
|
|
serde_json::to_string(&json).unwrap()
|
|
}
|
|
}
|
|
|
|
impl Field {
|
|
pub fn mock(width: u16, height: u16, players: Vec<Player>, balls: Vec<Ball>) -> Field {
|
|
Field {
|
|
width, height, players, balls
|
|
}
|
|
}
|
|
|
|
pub fn tick_inner(&mut self, inputs: Vec<Input>) {
|
|
for input in inputs.iter() {
|
|
let obj_opt = self.players.iter_mut().find(|p| p.obj.id == input.obj_id);
|
|
if let None = obj_opt {
|
|
log!("Could not find player with id {} with players: {:?}", input.obj_id, self.players);
|
|
continue;
|
|
}
|
|
let player = obj_opt.unwrap();
|
|
let mut player_obj = &mut player.obj;
|
|
let half_box = (player_obj.shape & 255) / 2;
|
|
match input.input {
|
|
InputType::UP => {
|
|
let shape_vert_bound = player_obj.y + half_box;
|
|
let out_of_bounds = shape_vert_bound + 5 > self.height;
|
|
if out_of_bounds {
|
|
player_obj.y = self.height - half_box
|
|
} else {
|
|
player_obj.y = player_obj.y + 5
|
|
}
|
|
},
|
|
InputType::DOWN => {
|
|
let shape_vert_bound = player_obj.y - half_box;
|
|
let next_iter = shape_vert_bound.checked_sub(5);
|
|
if let Some(res) = next_iter {
|
|
player_obj.y = res + half_box;
|
|
} else {
|
|
player_obj.y = half_box;
|
|
}
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
pub fn players(&self) -> &Vec<Player> {
|
|
&self.players
|
|
}
|
|
}
|
|
|
|
|