2019-09-13 23:18:11 +02:00
|
|
|
extern crate serde;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
|
|
|
extern crate serde_json;
|
2019-09-17 13:46:20 +02:00
|
|
|
extern crate octoon_math;
|
|
|
|
|
|
|
|
use octoon_math::{Mat3, Vec3};
|
2019-09-13 23:18:11 +02:00
|
|
|
|
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 10:48:20 +02:00
|
|
|
#[wasm_bindgen]
|
|
|
|
pub struct Game {
|
2019-09-17 13:46:20 +02:00
|
|
|
states: Vec<types::State>,
|
|
|
|
turn: usize,
|
2019-09-14 11:13:17 +02:00
|
|
|
|
2019-09-14 10:48:20 +02:00
|
|
|
/* put extra shit here */
|
2019-09-18 12:29:56 +02:00
|
|
|
planets: Vec<Vec3<f32>>,
|
|
|
|
view_box: Vec<f32>,
|
2019-09-17 13:46:20 +02:00
|
|
|
|
2019-09-18 12:29:56 +02:00
|
|
|
ship_locations: Vec<Mat3<f32>>,
|
|
|
|
current_planet_colours: Vec<Vec3<f32>>,
|
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
|
2019-09-17 13:46:20 +02:00
|
|
|
let states: Vec<types::State> = file.split("\n").filter_map(|line|
|
2019-09-14 10:48:20 +02:00
|
|
|
serde_json::from_str(line).ok()
|
|
|
|
).collect();
|
|
|
|
|
|
|
|
Self {
|
2019-09-18 12:29:56 +02:00
|
|
|
planets: utils::get_planets(&states[0].planets, 2.0),
|
2019-09-17 13:46:20 +02:00
|
|
|
view_box: utils::caclulate_viewbox(&states[0].planets),
|
|
|
|
turn: 0,
|
2019-09-14 11:13:17 +02:00
|
|
|
states,
|
2019-09-17 13:46:20 +02:00
|
|
|
ship_locations: Vec::new(),
|
|
|
|
current_planet_colours: Vec::new(),
|
2019-09-14 10:48:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 12:29:56 +02:00
|
|
|
pub fn get_viewbox(&self) -> *const f32 {
|
2019-09-17 13:46:20 +02:00
|
|
|
self.view_box.as_ptr()
|
|
|
|
}
|
|
|
|
|
2019-09-18 12:29:56 +02:00
|
|
|
pub fn get_planets(&self) -> *const Vec3<f32> {
|
2019-09-17 13:46:20 +02:00
|
|
|
self.planets.as_ptr()
|
|
|
|
}
|
|
|
|
|
2019-09-18 12:29:56 +02:00
|
|
|
pub fn get_planet_colors(&self) -> *const Vec3<f32> {
|
2019-09-17 13:46:20 +02:00
|
|
|
self.current_planet_colours.as_ptr()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_planet_count(&self) -> usize {
|
|
|
|
self.planets.len()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-09-17 13:46:20 +02:00
|
|
|
pub fn update_turn(&mut self, turn: usize) {
|
2019-09-18 12:29:56 +02:00
|
|
|
self.turn = turn.min(self.states.len() -1);
|
2019-09-17 13:46:20 +02:00
|
|
|
|
|
|
|
self.update_planet_colours();
|
|
|
|
self.update_ship_locations()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_planet_colours(&mut self) {
|
|
|
|
self.current_planet_colours = self.states[self.turn].planets
|
|
|
|
.iter()
|
|
|
|
.map(|p| utils::COLORS[p.owner.unwrap_or(0) as usize % utils::COLORS.len()].into())
|
|
|
|
.collect();
|
2019-09-14 11:13:17 +02:00
|
|
|
}
|
|
|
|
|
2019-09-17 13:46:20 +02:00
|
|
|
fn update_ship_locations(&mut self) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-09-18 12:29:56 +02:00
|
|
|
// pub fn add_location(&mut self, x: f32, y: f32, angle: f32) {
|
2019-09-17 13:46:20 +02:00
|
|
|
// self.current_turn.push(
|
|
|
|
// Location { x, y, angle}
|
|
|
|
// );
|
|
|
|
// }
|
|
|
|
|
2019-09-14 11:13:17 +02:00
|
|
|
pub fn location_count(&self) -> usize {
|
2019-09-17 13:46:20 +02:00
|
|
|
self.ship_locations.len()
|
2019-09-14 11:13:17 +02:00
|
|
|
}
|
|
|
|
|
2019-09-18 12:29:56 +02:00
|
|
|
pub fn get_ship_locations(&self) -> *const Mat3<f32> {
|
2019-09-17 13:46:20 +02:00
|
|
|
self.ship_locations.as_ptr()
|
2019-09-14 11:13:17 +02:00
|
|
|
}
|
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
|
|
|
}
|