mirror of
https://github.com/thilo-behnke/wasm-pong.git
synced 2026-07-11 02:29:25 +00:00
open file does not work in wasm
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
|
||||
pub mod event {
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::Write;
|
||||
|
||||
pub struct Event {
|
||||
pub topic: String,
|
||||
pub key: String,
|
||||
@@ -7,19 +10,27 @@ pub mod event {
|
||||
}
|
||||
|
||||
pub trait EventWriterImpl {
|
||||
fn write(&self, event: Event) -> std::io::Result<()>;
|
||||
fn write(&self, event: Event) -> Result<(), ()>;
|
||||
}
|
||||
|
||||
pub struct FileEventWriterImpl {}
|
||||
impl EventWriterImpl for FileEventWriterImpl {
|
||||
fn write(&self, event: Event) -> std::io::Result<()> {
|
||||
todo!()
|
||||
fn write(&self, event: Event) -> Result<(), ()> {
|
||||
let options = OpenOptions::new().read(true).create(true).write(true).open("events.log");
|
||||
if let Err(_) = options {
|
||||
return Err(());
|
||||
}
|
||||
let mut file = options.unwrap();
|
||||
match file.write(event.msg.as_bytes()) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(e) => Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct NoopEventWriterImpl {}
|
||||
impl EventWriterImpl for NoopEventWriterImpl {
|
||||
fn write(&self, event: Event) -> std::io::Result<()> {
|
||||
fn write(&self, event: Event) -> Result<(), ()> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
@@ -48,7 +59,7 @@ pub mod event {
|
||||
}
|
||||
// TODO: Kafka
|
||||
|
||||
pub fn write(&self, event: Event) -> std::io::Result<()> {
|
||||
pub fn write(&self, event: Event) -> Result<(), ()> {
|
||||
self.writer_impl.write(event)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ use crate::game_object::game_object::{DefaultGameObject, GameObject};
|
||||
use crate::geom::geom::Vector;
|
||||
use crate::geom::shape::{Shape, ShapeType};
|
||||
use crate::pong::pong_collisions::{handle_ball_bounds_collision, handle_player_ball_collision, handle_player_bound_collision};
|
||||
use crate::pong::pong_events::{PongEventWriter, DefaultPongEventWriter, NoopPongEventWriter};
|
||||
use crate::pong::pong_events::{PongEventWriter, DefaultPongEventWriter, NoopPongEventWriter, PongEventType, GameObjUpdate};
|
||||
use crate::utils::utils::{DefaultLoggerFactory, Logger, LoggerFactory, NoopLogger};
|
||||
use std::borrow::{Borrow, BorrowMut};
|
||||
use std::cell::{Cell, Ref, RefCell, RefMut};
|
||||
@@ -169,6 +169,14 @@ impl Field {
|
||||
let obj_b = objs.iter().find(|o| RefCell::borrow(o).id() == collision.1).unwrap().clone();
|
||||
collision_handler.handle(obj_a, obj_b);
|
||||
}
|
||||
|
||||
{
|
||||
for obj in self.objs.iter().filter(|o| RefCell::borrow(o).is_dirty()) {
|
||||
let mut obj = RefCell::borrow_mut(obj);
|
||||
self.event_writer.write(PongEventType::GameObjUpdate(GameObjUpdate{obj_id: &obj.id().to_string(), vel: obj.vel(), orientation: obj.orientation(), pos: obj.pos()}));
|
||||
obj.set_dirty(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_collisions(&self) -> Box<dyn CollisionRegistry> {
|
||||
|
||||
@@ -17,6 +17,8 @@ pub mod game_object {
|
||||
fn vel(&self) -> &Vector;
|
||||
fn vel_mut(&mut self) -> &mut Vector;
|
||||
fn is_static(&self) -> bool;
|
||||
fn is_dirty(&self) -> bool;
|
||||
fn set_dirty(&mut self, is_dirty: bool);
|
||||
}
|
||||
|
||||
// #[derive(Clone, Debug, PartialEq)]
|
||||
@@ -26,6 +28,7 @@ pub mod game_object {
|
||||
pub obj_type: String,
|
||||
geom: Box<dyn GeomComp>,
|
||||
physics: Box<dyn PhysicsComp>,
|
||||
dirty: bool
|
||||
}
|
||||
|
||||
impl DefaultGameObject {
|
||||
@@ -40,6 +43,7 @@ pub mod game_object {
|
||||
obj_type,
|
||||
geom,
|
||||
physics,
|
||||
dirty: false
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -74,18 +78,20 @@ pub mod game_object {
|
||||
}
|
||||
|
||||
fn update_pos(&mut self) {
|
||||
// Keep last orientation if vel is now zero.
|
||||
if self.vel() == &Vector::zero() {
|
||||
return;
|
||||
}
|
||||
let vel = self.vel().clone();
|
||||
let center = self.geom.center_mut();
|
||||
center.add(&vel);
|
||||
// Keep last orientation if vel is now zero.
|
||||
if vel == Vector::zero() {
|
||||
return;
|
||||
}
|
||||
let mut updated_orientation = vel.clone();
|
||||
updated_orientation.normalize();
|
||||
let orientation = self.geom.orientation_mut();
|
||||
orientation.x = updated_orientation.x;
|
||||
orientation.y = updated_orientation.y;
|
||||
|
||||
self.dirty = true;
|
||||
}
|
||||
|
||||
fn bounding_box(&self) -> BoundingBox {
|
||||
@@ -103,6 +109,14 @@ pub mod game_object {
|
||||
fn is_static(&self) -> bool {
|
||||
self.physics.is_static()
|
||||
}
|
||||
|
||||
fn is_dirty(&self) -> bool {
|
||||
return self.dirty
|
||||
}
|
||||
|
||||
fn set_dirty(&mut self, is_dirty: bool) {
|
||||
self.dirty = is_dirty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,8 @@ pub mod pong_collisions {
|
||||
b_to_a.sub(&player.pos());
|
||||
b_to_a.normalize();
|
||||
ball.pos_mut().add(&b_to_a);
|
||||
|
||||
ball.set_dirty(true);
|
||||
}
|
||||
|
||||
pub fn handle_ball_bounds_collision(
|
||||
@@ -34,6 +36,8 @@ pub mod pong_collisions {
|
||||
let mut ball = RefCell::borrow_mut(&ball);
|
||||
let bound = RefCell::borrow(&bound);
|
||||
ball.vel_mut().reflect(&bound.orientation());
|
||||
|
||||
ball.set_dirty(true);
|
||||
}
|
||||
|
||||
pub fn handle_player_bound_collision(
|
||||
@@ -54,6 +58,8 @@ pub mod pong_collisions {
|
||||
new_pos.add(&perpendicular);
|
||||
let player_pos = player.pos_mut();
|
||||
player_pos.y = new_pos.y;
|
||||
|
||||
player.set_dirty(true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,20 +70,20 @@ pub mod pong_events {
|
||||
use crate::geom::geom::Vector;
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub enum PongEventType {
|
||||
GameObjUpdate(GameObjUpdate)
|
||||
pub enum PongEventType<'a> {
|
||||
GameObjUpdate(GameObjUpdate<'a>)
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct GameObjUpdate {
|
||||
pub obj_id: String,
|
||||
pub pos: Vector,
|
||||
pub vel: Vector,
|
||||
pub orientation: Vector
|
||||
pub struct GameObjUpdate<'a> {
|
||||
pub obj_id: &'a str,
|
||||
pub pos: &'a Vector,
|
||||
pub vel: &'a Vector,
|
||||
pub orientation: &'a Vector
|
||||
}
|
||||
|
||||
pub trait PongEventWriter {
|
||||
fn write(&self, event: PongEventType) -> std::io::Result<()>;
|
||||
fn write(&self, event: PongEventType) -> Result<(), ()>;
|
||||
}
|
||||
|
||||
pub struct DefaultPongEventWriter {
|
||||
@@ -85,12 +91,12 @@ pub mod pong_events {
|
||||
}
|
||||
|
||||
impl PongEventWriter for DefaultPongEventWriter {
|
||||
fn write(&self, event: PongEventType) -> std::io::Result<()> {
|
||||
fn write(&self, event: PongEventType) -> Result<(), ()> {
|
||||
let out_event = match event {
|
||||
PongEventType::GameObjUpdate(ref update) => {
|
||||
Event {
|
||||
topic: String::from("obj_update"),
|
||||
key: update.obj_id.clone(),
|
||||
key: update.obj_id.clone().to_string(),
|
||||
msg: serde_json::to_string(&event).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ mod utils;
|
||||
use std::cell::RefCell;
|
||||
use pong::collision::collision::{Collision, CollisionDetector};
|
||||
use pong::game_field::{Field, Input, InputType};
|
||||
use pong::game_object::game_object::GameObject;
|
||||
use pong::game_object::game_object::{DefaultGameObject, GameObject};
|
||||
use pong::geom::geom::Vector;
|
||||
use pong::geom::shape::ShapeType;
|
||||
use pong::utils::utils::{DefaultLoggerFactory, Logger};
|
||||
@@ -12,6 +12,7 @@ use serde_json::json;
|
||||
use std::cmp::{max, min};
|
||||
use std::rc::Rc;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use pong::pong::pong_events::DefaultPongEventWriter;
|
||||
|
||||
extern crate serde_json;
|
||||
extern crate web_sys;
|
||||
@@ -111,7 +112,7 @@ pub struct FieldWrapper {
|
||||
#[wasm_bindgen]
|
||||
impl FieldWrapper {
|
||||
pub fn new() -> FieldWrapper {
|
||||
let field = Field::new(DefaultLoggerFactory::new(Box::new(WasmLogger::root())));
|
||||
let field = Field::new(DefaultLoggerFactory::new(Box::new(WasmLogger::root())), DefaultPongEventWriter::new());
|
||||
FieldWrapper { field }
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user