From a289936913302c624a12816193a045bbfa04bd49 Mon Sep 17 00:00:00 2001 From: ajuvercr Date: Sat, 14 Sep 2019 11:13:17 +0200 Subject: [PATCH] start the testing --- frontend/src/lib.rs | 26 +++++++++++++++++++++++++- frontend/www/index.js | 31 +++++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/frontend/src/lib.rs b/frontend/src/lib.rs index 6ff877b..17a6752 100644 --- a/frontend/src/lib.rs +++ b/frontend/src/lib.rs @@ -14,10 +14,19 @@ use wasm_bindgen::prelude::*; #[global_allocator] static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; +#[wasm_bindgen] +pub struct Location { + x: f64, + y: f64, + angle: f64, +} + #[wasm_bindgen] pub struct Game { states : Vec, + /* put extra shit here */ + current_turn: Vec, } #[wasm_bindgen] @@ -31,13 +40,28 @@ impl Game { ).collect(); Self { - states + states, + current_turn: Vec::new() } } pub fn turn_count(&self) -> usize { self.states.len() } + + pub fn add_location(&mut self, x: f64, y: f64, angle: f64) { + self.current_turn.push( + Location { x, y, angle} + ); + } + + pub fn location_count(&self) -> usize { + self.current_turn.len() + } + + pub fn locations(&self) -> *const Location { + self.current_turn.as_ptr() + } } diff --git a/frontend/www/index.js b/frontend/www/index.js index 90bc2db..a7c06fa 100644 --- a/frontend/www/index.js +++ b/frontend/www/index.js @@ -1,16 +1,31 @@ import { Game } from "planetwars"; +import { memory } from "planetwars/plantwars_bg" const URL = window.location.origin+window.location.pathname; const LOCATION = URL.substring(0, URL.lastIndexOf("/") + 1); const game_location = LOCATION + "static/game.json"; -fetch(game_location) - .then((r) => r.text()) - .then((response) => { - console.log(response); - let game = Game.new(response); - console.log(game.turn_count()); - }).catch(console.error); +// fetch(game_location) +// .then((r) => r.text()) +// .then((response) => { +// console.log(response); +// let game = Game.new(response); +// console.log(game.turn_count()); +// }).catch(console.error); - \ No newline at end of file + +const g = Game.new(""); + +const p1 = g.locations(); +const s1 = g.location_count(); +console.log(p1, s1); +const a1 = new Float64Array(memory.buffer, p1, s1 * 3); +console.log(a1); + +g.add_location(0.5, 1.2, 3.14); + +const p2 = g.locations(); +const s2 = g.location_count(); +const a2 = new Float64Array(memory.buffer, p2, s2 * 3); +console.log(a2);