diff --git a/frontend/Cargo.toml b/frontend/Cargo.toml index 012014a..a15e96b 100644 --- a/frontend/Cargo.toml +++ b/frontend/Cargo.toml @@ -28,6 +28,7 @@ wee_alloc = { version = "0.4.2", optional = true } serde = "1.0.100" serde_derive = "1.0.100" serde_json = "1.0" +octoon-math = "0.1.7" [dev-dependencies] wasm-bindgen-test = "0.2" diff --git a/frontend/src/lib.rs b/frontend/src/lib.rs index 17a6752..2ee5504 100644 --- a/frontend/src/lib.rs +++ b/frontend/src/lib.rs @@ -2,6 +2,9 @@ extern crate serde; #[macro_use] extern crate serde_derive; extern crate serde_json; +extern crate octoon_math; + +use octoon_math::{Mat3, Vec3}; mod utils; mod types; @@ -14,19 +17,17 @@ 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, + states: Vec, + turn: usize, /* put extra shit here */ - current_turn: Vec, + planets: Vec>, + view_box: Vec, + + ship_locations: Vec>, + current_planet_colours: Vec>, } #[wasm_bindgen] @@ -35,32 +36,70 @@ impl Game { 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| + let states: Vec = file.split("\n").filter_map(|line| serde_json::from_str(line).ok() ).collect(); Self { + planets: utils::get_planets(&states[0].planets, 10.0), + view_box: utils::caclulate_viewbox(&states[0].planets), + turn: 0, states, - current_turn: Vec::new() + ship_locations: Vec::new(), + current_planet_colours: Vec::new(), } } + pub fn get_viewbox(&self) -> *const f64 { + self.view_box.as_ptr() + } + + pub fn get_planets(&self) -> *const Vec3 { + self.planets.as_ptr() + } + + pub fn get_planet_colors(&self) -> *const Vec3 { + self.current_planet_colours.as_ptr() + } + + pub fn get_planet_count(&self) -> usize { + self.planets.len() + } + 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 update_turn(&mut self, turn: usize) { + self.turn = turn.min(self.states.len()); + + 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(); + } + + fn update_ship_locations(&mut self) { + + } + + // 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() + self.ship_locations.len() } - pub fn locations(&self) -> *const Location { - self.current_turn.as_ptr() + pub fn get_ship_locations(&self) -> *const Mat3 { + self.ship_locations.as_ptr() } } diff --git a/frontend/src/utils.rs b/frontend/src/utils.rs index b1d7929..c76463a 100644 --- a/frontend/src/utils.rs +++ b/frontend/src/utils.rs @@ -8,3 +8,44 @@ pub fn set_panic_hook() { #[cfg(feature = "console_error_panic_hook")] console_error_panic_hook::set_once(); } + +use octoon_math::{Vec3}; + +/// this is total extra, so it the planet viewbox is like 100px wide, it will now be in total 110 pixels wide +static VIEWBOX_SCALE: f64 = 0.1; + +pub static COLORS: [[f64; 3]; 10] = [ + [1.0, 0.0, 0.0], + [1.0, 0.0, 0.0], + [1.0, 0.0, 0.0], + [1.0, 0.0, 0.0], + [1.0, 0.0, 0.0], + [1.0, 0.0, 0.0], + [1.0, 0.0, 0.0], + [1.0, 0.0, 0.0], + [1.0, 0.0, 0.0], + [1.0, 0.0, 0.0], +]; + +use super::types; + +pub fn caclulate_viewbox(planets: &Vec) -> Vec { + let mut iter = planets.iter(); + + let init = match iter.next() { + Some(p) => (p.x, p.y, p.x, p.y), + None => return vec![0.0, 0.0, 0.0, 0.0], + }; + let (min_x, min_y, max_x, max_y) = planets + .iter() + .fold(init, |(min_x, min_y, max_x, max_y), p| (min_x.min(p.x), min_y.min(p.y), max_x.max(p.x), max_y.max(p.y))); + + let (width, height) = (max_x - min_x, max_y - min_y); + let (dx, dy) = (VIEWBOX_SCALE * width, VIEWBOX_SCALE * height); + + vec![min_x - dx/2.0, min_y - dy/2.0, width + dx, height + dy] +} + +pub fn get_planets(planets: &Vec, r: f64) -> Vec> { + planets.iter().map(|p| Vec3::new(p.x, p.y, r)).collect() +}