planet-wars/frontend/src/lib.rs

72 lines
1.4 KiB
Rust
Raw Normal View History

2019-09-13 23:18:11 +02:00
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
2019-09-13 22:54:21 +02:00
mod utils;
2019-09-13 23:18:11 +02:00
mod types;
2019-09-13 22:54:21 +02:00
use wasm_bindgen::prelude::*;
// 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;
2019-09-14 11:13:17 +02:00
#[wasm_bindgen]
pub struct Location {
x: f64,
y: f64,
angle: f64,
}
2019-09-14 10:48:20 +02:00
#[wasm_bindgen]
pub struct Game {
states : Vec<types::State>,
2019-09-14 11:13:17 +02:00
2019-09-14 10:48:20 +02:00
/* put extra shit here */
2019-09-14 11:13:17 +02:00
current_turn: Vec<Location>,
2019-09-13 23:18:11 +02:00
}
2019-09-13 22:54:21 +02:00
#[wasm_bindgen]
2019-09-14 10:48:20 +02:00
impl Game {
pub fn new(file: &str) -> Self {
utils::set_panic_hook();
// First line is fucked but we just filter out things that cannot parse
let states = file.split("\n").filter_map(|line|
serde_json::from_str(line).ok()
).collect();
Self {
2019-09-14 11:13:17 +02:00
states,
current_turn: Vec::new()
2019-09-14 10:48:20 +02:00
}
}
pub fn turn_count(&self) -> usize {
self.states.len()
}
2019-09-14 11:13:17 +02:00
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()
}
2019-09-13 22:54:21 +02:00
}
2019-09-14 10:48:20 +02:00
2019-09-13 22:54:21 +02:00
#[wasm_bindgen]
2019-09-14 10:48:20 +02:00
extern {
fn alert(s: &str);
2019-09-13 22:54:21 +02:00
}