add turn updates with arrow keys etc

This commit is contained in:
ajuvercr 2019-09-21 10:32:57 +02:00
parent bcd0665c17
commit a744a17695
2 changed files with 106 additions and 93 deletions

View file

@ -4,7 +4,7 @@ extern crate serde_derive;
extern crate serde_json; extern crate serde_json;
extern crate octoon_math; extern crate octoon_math;
use octoon_math::{Mat3, Vec3, One}; use octoon_math::{Mat3, Vec3};
mod utils; mod utils;
mod types; mod types;
@ -75,7 +75,6 @@ impl Circle {
let cos = alpha.cos(); let cos = alpha.cos();
let sin = alpha.sin(); let sin = alpha.sin();
// Mat3::rotate_z(alpha) *
Mat3::new( Mat3::new(
0.3, 0.0, 0.0, 0.3, 0.0, 0.0,
0.0, 0.4, 0.0, 0.0, 0.4, 0.0,
@ -84,51 +83,51 @@ impl Circle {
} }
} }
struct Line { // struct Line {
x1: f32, // x1: f32,
y1: f32, // y1: f32,
x2: f32, // x2: f32,
y2: f32, // y2: f32,
a: f32, // a: f32,
d: usize, // d: usize,
} // }
impl Line { // impl Line {
pub fn new(p1: &types::Planet, p2: &types::Planet) -> Self { // pub fn new(p1: &types::Planet, p2: &types::Planet) -> Self {
let dx = p1.x - p2.x; // let dx = p1.x - p2.x;
let dy = p1.y - p2.y; // let dy = p1.y - p2.y;
let a = dy.atan2(dx); // let a = dy.atan2(dx);
// let a = (dy / dx).atan(); // // let a = (dy / dx).atan();
let d = (dx * dx + dy * dy).sqrt().ceil() as usize + 1; // let d = (dx * dx + dy * dy).sqrt().ceil() as usize + 1;
Self { // Self {
x1: p1.x, // x1: p1.x,
x2: p2.x, // x2: p2.x,
y1: p1.y, // y1: p1.y,
y2: p2.y, // y2: p2.y,
d, a, // d, a,
} // }
} // }
pub fn get_for_remaining(&self, remaining: usize) -> (Mat3<f32>, Mat3<f32>) { // pub fn get_for_remaining(&self, remaining: usize) -> (Mat3<f32>, Mat3<f32>) {
( // (
self.get_remaining(remaining), // self.get_remaining(remaining),
self.get_remaining((remaining + 1).min(self.d - 1)), // self.get_remaining((remaining + 1).min(self.d - 1)),
) // )
} // }
fn get_remaining(&self, remaining: usize) -> Mat3<f32> { // fn get_remaining(&self, remaining: usize) -> Mat3<f32> {
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 - 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 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 - remaining) as f32 * self.y2) / self.d as f32;
// let y = self.y1 + (remaining as f32 / self.d as f32) * (self.y2 - self.y1); // // let y = self.y1 + (remaining as f32 / self.d as f32) * (self.y2 - self.y1);
Mat3::new( // Mat3::new(
0.3, 0.0, 0.0, // 0.3, 0.0, 0.0,
0.0, 0.3, 0.0, // 0.0, 0.3, 0.0,
x, y, 0.3, // x, y, 0.3,
) * Mat3::rotate_z(self.a) // ) * Mat3::rotate_z(self.a)
} // }
} // }
#[wasm_bindgen] #[wasm_bindgen]
@ -154,13 +153,9 @@ impl Game {
utils::set_panic_hook(); utils::set_panic_hook();
// First line is fucked but we just filter out things that cannot parse // First line is fucked but we just filter out things that cannot parse
let mut states: Vec<types::State> = file.split("\n").filter_map(|line| let states: Vec<types::State> = file.split("\n").filter_map(|line|
serde_json::from_str(line).ok() serde_json::from_str(line).ok()
).collect(); ).collect();
states.push(types::State {
planets: states.last().unwrap().planets.clone(),
expeditions: Vec::new()
});
let mut planet_map = HashMap::new(); let mut planet_map = HashMap::new();
@ -204,11 +199,13 @@ impl Game {
self.states.len() 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.turn = turn.min(self.states.len() -1);
self.update_planet_colours(); self.update_planet_colours();
self.update_ship_locations() self.update_ship_locations();
self.turn
} }
fn update_planet_colours(&mut self) { fn update_planet_colours(&mut self) {
@ -242,12 +239,6 @@ impl Game {
}).collect(); }).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 { pub fn get_max_ships(&self) -> usize {
self.states.iter().map(|s| s.expeditions.len()).max().unwrap() self.states.iter().map(|s| s.expeditions.len()).max().unwrap()
} }

View file

@ -125,19 +125,7 @@ class GameInstance {
} }
} }
render(time: number) { _update_state() {
COUNTER.frame(time);
if (!this.playing) {
this.last_time = time;
return;
}
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); const colours = f32v(this.game.get_planet_colors(), this.planet_count * 6);
for(let i=0; i < this.planet_count; i++){ for(let i=0; i < this.planet_count; i++){
const u = new Uniform3f(colours[i*6], colours[i*6 + 1], colours[i*6 + 2]); const u = new Uniform3f(colours[i*6], colours[i*6 + 1], colours[i*6 + 2]);
@ -174,6 +162,19 @@ class GameInstance {
} }
} }
render(time: number) {
COUNTER.frame(time);
if (!this.playing) {
this.last_time = time;
return;
}
if (time > this.last_time + 500) {
this.last_time = time;
this.updateTurn(this.frame + 1);
}
GL.bindFramebuffer(GL.FRAMEBUFFER, null); GL.bindFramebuffer(GL.FRAMEBUFFER, null);
GL.viewport(0, 0, GL.canvas.width, GL.canvas.height); GL.viewport(0, 0, GL.canvas.width, GL.canvas.height);
GL.clear(GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT); GL.clear(GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT);
@ -188,6 +189,17 @@ class GameInstance {
this.renderer.render(GL); 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) { handleKey(event: KeyboardEvent) {
console.log(event.keyCode); console.log(event.keyCode);
console.log(event.key); console.log(event.key);
@ -199,7 +211,17 @@ class GameInstance {
} else { } else {
this.playing = true; 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);
} }
} }
} }