diff --git a/frontend/src/lib.rs b/frontend/src/lib.rs index 3a5b665..431e9cb 100644 --- a/frontend/src/lib.rs +++ b/frontend/src/lib.rs @@ -4,7 +4,7 @@ extern crate serde_derive; extern crate serde_json; extern crate octoon_math; -use octoon_math::{Mat3, Vec3, One}; +use octoon_math::{Mat3, Vec3}; mod utils; mod types; @@ -75,7 +75,6 @@ impl Circle { let cos = alpha.cos(); let sin = alpha.sin(); - // Mat3::rotate_z(alpha) * Mat3::new( 0.3, 0.0, 0.0, 0.0, 0.4, 0.0, @@ -84,51 +83,51 @@ impl Circle { } } -struct Line { - x1: f32, - y1: f32, - x2: f32, - y2: f32, - a: f32, - d: usize, -} -impl Line { - pub fn new(p1: &types::Planet, p2: &types::Planet) -> Self { - let dx = p1.x - p2.x; - let dy = p1.y - p2.y; - let a = dy.atan2(dx); - // let a = (dy / dx).atan(); - let d = (dx * dx + dy * dy).sqrt().ceil() as usize + 1; +// struct Line { +// x1: f32, +// y1: f32, +// x2: f32, +// y2: f32, +// a: f32, +// d: usize, +// } +// impl Line { +// pub fn new(p1: &types::Planet, p2: &types::Planet) -> Self { +// let dx = p1.x - p2.x; +// let dy = p1.y - p2.y; +// let a = dy.atan2(dx); +// // let a = (dy / dx).atan(); +// let d = (dx * dx + dy * dy).sqrt().ceil() as usize + 1; - Self { - x1: p1.x, - x2: p2.x, - y1: p1.y, - y2: p2.y, - d, a, - } - } +// Self { +// x1: p1.x, +// x2: p2.x, +// y1: p1.y, +// y2: p2.y, +// d, a, +// } +// } - pub fn get_for_remaining(&self, remaining: usize) -> (Mat3, Mat3) { - ( - self.get_remaining(remaining), - self.get_remaining((remaining + 1).min(self.d - 1)), - ) - } +// pub fn get_for_remaining(&self, remaining: usize) -> (Mat3, Mat3) { +// ( +// self.get_remaining(remaining), +// self.get_remaining((remaining + 1).min(self.d - 1)), +// ) +// } - fn get_remaining(&self, remaining: usize) -> Mat3 { - let x = (self.x1 * remaining as f32 + (self.d - remaining) as f32 * self.x2) / self.d as f32; - // let x = self.x1 + (remaining as f32 / self.d as f32) * (self.x2 - self.x1); - let y = (self.y1 * remaining as f32 + (self.d - remaining) as f32 * self.y2) / self.d as f32; +// fn get_remaining(&self, remaining: usize) -> Mat3 { +// let x = (self.x1 * remaining as f32 + (self.d - remaining) as f32 * self.x2) / self.d as f32; +// // let x = self.x1 + (remaining as f32 / self.d as f32) * (self.x2 - self.x1); +// let y = (self.y1 * remaining as f32 + (self.d - remaining) as f32 * self.y2) / self.d as f32; - // let y = self.y1 + (remaining as f32 / self.d as f32) * (self.y2 - self.y1); - Mat3::new( - 0.3, 0.0, 0.0, - 0.0, 0.3, 0.0, - x, y, 0.3, - ) * Mat3::rotate_z(self.a) - } -} +// // let y = self.y1 + (remaining as f32 / self.d as f32) * (self.y2 - self.y1); +// Mat3::new( +// 0.3, 0.0, 0.0, +// 0.0, 0.3, 0.0, +// x, y, 0.3, +// ) * Mat3::rotate_z(self.a) +// } +// } #[wasm_bindgen] @@ -154,13 +153,9 @@ impl Game { utils::set_panic_hook(); // First line is fucked but we just filter out things that cannot parse - let mut states: Vec = file.split("\n").filter_map(|line| + let states: Vec = file.split("\n").filter_map(|line| serde_json::from_str(line).ok() ).collect(); - states.push(types::State { - planets: states.last().unwrap().planets.clone(), - expeditions: Vec::new() - }); let mut planet_map = HashMap::new(); @@ -204,11 +199,13 @@ impl Game { self.states.len() } - pub fn update_turn(&mut self, turn: usize) { + pub fn update_turn(&mut self, turn: usize) -> usize { self.turn = turn.min(self.states.len() -1); self.update_planet_colours(); - self.update_ship_locations() + self.update_ship_locations(); + + self.turn } fn update_planet_colours(&mut self) { @@ -242,12 +239,6 @@ impl Game { }).collect(); } - // pub fn add_location(&mut self, x: f32, y: f32, angle: f32) { - // self.current_turn.push( - // Location { x, y, angle} - // ); - // } - pub fn get_max_ships(&self) -> usize { self.states.iter().map(|s| s.expeditions.len()).max().unwrap() } diff --git a/frontend/www/index.ts b/frontend/www/index.ts index cb210ef..a37c692 100644 --- a/frontend/www/index.ts +++ b/frontend/www/index.ts @@ -125,6 +125,43 @@ class GameInstance { } } + _update_state() { + const colours = f32v(this.game.get_planet_colors(), this.planet_count * 6); + for(let i=0; i < this.planet_count; i++){ + const u = new Uniform3f(colours[i*6], colours[i*6 + 1], colours[i*6 + 2]); + this.renderer.updateUniform(i, (us) => us["u_color"] = u); + const u2 = new Uniform3f(colours[i*6 + 3], colours[i*6 + 4], colours[i*6 + 5]); + this.renderer.updateUniform(i, (us) => us["u_color_next"] = u2); + } + + const ships = f32v(this.game.get_ship_locations(), this.game.get_ship_count() * 9 * 2); + const ship_colours = f32v(this.game.get_ship_colours(), this.game.get_ship_count() * 3); + + for (let i=0; i < this.game.get_max_ships(); i++) { + const index = this.ship_indices[i]; + if (i < this.game.get_ship_count()) { + + this.renderer.enableRendershit(index); + + const u = new Uniform3f(ship_colours[i*3], ship_colours[i*3 + 1], ship_colours[i*3 + 2]); + // const t1 = new UniformMatrix3fv(new Float32Array(ships, i * 18, 9)); + // const t2 = new UniformMatrix3fv(new Float32Array(ships, i * 18 + 9, 9)); + + const t1 = new UniformMatrix3fv(ships.slice(i * 18, i * 18 + 9)); + const t2 = new UniformMatrix3fv(ships.slice(i * 18 + 9, i * 18 + 18)); + + this.renderer.updateUniform(index, (us) => { + us["u_color"] = u; + us["u_color_next"] = u; + us["u_trans"] = t1; + us["u_trans_next"] = t2; + }); + } else { + this.renderer.disableRenderShift(index); + } + } + } + render(time: number) { COUNTER.frame(time); @@ -135,43 +172,7 @@ class GameInstance { if (time > this.last_time + 500) { this.last_time = time; - this.frame ++; - this.game.update_turn(this.frame); - - const colours = f32v(this.game.get_planet_colors(), this.planet_count * 6); - for(let i=0; i < this.planet_count; i++){ - const u = new Uniform3f(colours[i*6], colours[i*6 + 1], colours[i*6 + 2]); - this.renderer.updateUniform(i, (us) => us["u_color"] = u); - const u2 = new Uniform3f(colours[i*6 + 3], colours[i*6 + 4], colours[i*6 + 5]); - this.renderer.updateUniform(i, (us) => us["u_color_next"] = u2); - } - - const ships = f32v(this.game.get_ship_locations(), this.game.get_ship_count() * 9 * 2); - const ship_colours = f32v(this.game.get_ship_colours(), this.game.get_ship_count() * 3); - - for (let i=0; i < this.game.get_max_ships(); i++) { - const index = this.ship_indices[i]; - if (i < this.game.get_ship_count()) { - - this.renderer.enableRendershit(index); - - const u = new Uniform3f(ship_colours[i*3], ship_colours[i*3 + 1], ship_colours[i*3 + 2]); - // const t1 = new UniformMatrix3fv(new Float32Array(ships, i * 18, 9)); - // const t2 = new UniformMatrix3fv(new Float32Array(ships, i * 18 + 9, 9)); - - const t1 = new UniformMatrix3fv(ships.slice(i * 18, i * 18 + 9)); - const t2 = new UniformMatrix3fv(ships.slice(i * 18 + 9, i * 18 + 18)); - - this.renderer.updateUniform(index, (us) => { - us["u_color"] = u; - us["u_color_next"] = u; - us["u_trans"] = t1; - us["u_trans_next"] = t2; - }); - } else { - this.renderer.disableRenderShift(index); - } - } + this.updateTurn(this.frame + 1); } GL.bindFramebuffer(GL.FRAMEBUFFER, null); @@ -188,6 +189,17 @@ class GameInstance { this.renderer.render(GL); } + updateTurn(turn: number) { + this.frame = Math.max(0, turn); + const new_frame = this.game.update_turn(this.frame); + if (new_frame < this.frame) { + this.frame = new_frame; + this.playing = false; + } else { + this._update_state(); + } + } + handleKey(event: KeyboardEvent) { console.log(event.keyCode); console.log(event.key); @@ -199,7 +211,17 @@ class GameInstance { } else { this.playing = true; } + } + // Arrow left + if (event.keyCode == 37) { + // This feels more natural than -1 what it should be, I think + this.updateTurn(this.frame - 2); + } + + // Arrow right + if (event.keyCode == 39) { + this.updateTurn(this.frame + 1); } } }