diff --git a/pong/src/collision.rs b/pong/src/collision.rs index 75c3b8a..9d40872 100644 --- a/pong/src/collision.rs +++ b/pong/src/collision.rs @@ -69,7 +69,7 @@ pub mod collision { let rest = &objs[i..]; for other in rest.iter().map(|o| o.borrow()) { if !self.config.matches_any_group(obj.obj_type(), other.obj_type()) { - self.logger.log(&*format!("objs {} and {} do not match any group: {:?}", obj.obj_type(), other.obj_type(), self.config.groups)); + // self.logger.log(&*format!("objs {} and {} do not match any group: {:?}", obj.obj_type(), other.obj_type(), self.config.groups)); continue; } let has_collision = obj.bounding_box().overlaps(&other.bounding_box()); diff --git a/pong/src/game_field.rs b/pong/src/game_field.rs index 25b2f74..1116eea 100644 --- a/pong/src/game_field.rs +++ b/pong/src/game_field.rs @@ -135,11 +135,11 @@ impl Field { let input = input_opt.unwrap(); match input.input { InputType::UP => { - let updated_vel_y = (obj_mut.vel().y + 1.).min(5.); + let updated_vel_y = (obj_mut.vel().y + 1.).min(1.); obj_mut.vel_mut().y = updated_vel_y; } InputType::DOWN => { - let updated_vel_y = (obj_mut.vel().y - 1.).max(-5.); + let updated_vel_y = (obj_mut.vel().y - 1.).max(-1.); obj_mut.vel_mut().y = updated_vel_y; } }; diff --git a/src/lib.rs b/src/lib.rs index 8fe42bf..fe58a5c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -114,6 +114,14 @@ impl FieldWrapper { self.field.height } + pub fn pause(&mut self) { + self.paused = true + } + + pub fn resume(&mut self) { + self.paused = false + } + pub fn tick(&mut self, inputs_js: &JsValue) { let input_dtos: Vec = inputs_js.into_serde().unwrap(); let inputs = input_dtos diff --git a/www/index.html b/www/index.html index 710baeb..e3b46e5 100644 --- a/www/index.html +++ b/www/index.html @@ -20,6 +20,17 @@ +
+ + + +
diff --git a/www/index.js b/www/index.js index aae03c0..0ca4a7f 100644 --- a/www/index.js +++ b/www/index.js @@ -14,31 +14,54 @@ canvas.width = width const ctx = canvas.getContext('2d'); +let paused = false; let keysDown = new Set(); - -console.log(field.get_state()) +let actions = []; const renderLoop = () => { - let actions = getInputActions(); - field.tick(actions); - - render(); + actions = getInputActions(); + if (paused) { + requestAnimationFrame(renderLoop); + return; + } + tick(); requestAnimationFrame(renderLoop); } +const tick = () => { + field.tick(actions); + render(); +} + const render = () => { ctx.clearRect(0, 0, canvas.width, canvas.height); - // drawField(); drawObjects(); } -const drawField = () => { - ctx.beginPath(); +window.WASM_PONG = {} +window.WASM_PONG.width = width +window.WASM_PONG.height = height - ctx.strokeStyle = GRID_COLOR; - ctx.rect(1, 1, field.width - 2, field.height - 2); +window.WASM_PONG.pauseGame = () => { + paused = true; + document.getElementById("pause-btn").disabled = true; + document.getElementById("resume-btn").disabled = false; + document.getElementById("tick-btn").disabled = false; +} - ctx.stroke(); +window.WASM_PONG.resumeGame = () => { + paused = false; + document.getElementById("pause-btn").disabled = false; + document.getElementById("resume-btn").disabled = true; + document.getElementById("tick-btn").disabled = true; +} + + +window.WASM_PONG.oneTick = () => { + if (!paused) { + return; + } + tick() } const drawObjects = () => {