diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/astroid.rs | 6 | ||||
-rw-r--r-- | src/bin/client.rs | 2 | ||||
-rw-r--r-- | src/engines.rs | 62 | ||||
-rw-r--r-- | src/mass.rs | 1 | ||||
-rw-r--r-- | src/ship.rs | 11 |
5 files changed, 78 insertions, 4 deletions
diff --git a/src/astroid.rs b/src/astroid.rs index 25029ca..ffdeabf 100644 --- a/src/astroid.rs +++ b/src/astroid.rs @@ -22,7 +22,7 @@ impl Astroid { .take(8) .collect(); let mut pr = Range::new(-50.0, 50.0); - let mut vr = Range::new(-1.0, 1.0); + let mut vr = Range::new(-0.5, 0.5); let mut rng = rand::thread_rng(); let position = (pr.sample(&mut rng), pr.sample(&mut rng), pr.sample(&mut rng)); let velocity = (vr.sample(&mut rng), vr.sample(&mut rng), vr.sample(&mut rng)); @@ -58,6 +58,10 @@ impl Mass for Astroid { self.position.2 += self.velocity.2; } + fn recv_velocity(&self) -> (f64, f64, f64) { + self.velocity + } + fn give_acceleration(&mut self, acceleration : (f64, f64, f64)) { self.velocity.0 += acceleration.0; self.velocity.1 += acceleration.1; diff --git a/src/bin/client.rs b/src/bin/client.rs index 839c0be..558f914 100644 --- a/src/bin/client.rs +++ b/src/bin/client.rs @@ -42,7 +42,7 @@ fn main() { let module = from_primitive(choice); match module { Module::Dashboard => client_dashboard(buff_r), - Module::Engines => client_engines(stream), + Module::Engines => client_engines(stream, buff_r), Module::Navigation => client_navigation(name, stream, buff_r), } } diff --git a/src/engines.rs b/src/engines.rs index e951856..548b44b 100644 --- a/src/engines.rs +++ b/src/engines.rs @@ -4,18 +4,38 @@ use termion::async_stdin; use std::thread::sleep; use std::io::{Read, Write, stdout}; use std::time::Duration; -use std::io::BufRead; +use std::io::{BufRead, BufReader}; + +extern crate termion; use ship::Ship; use mass::Mass; use connection::Connection; -pub fn client_engines(mut stream : TcpStream) { +pub fn client_engines(mut stream : TcpStream, mut buff_r : BufReader<TcpStream>) { let stdout = stdout(); let mut stdout = stdout.lock().into_raw_mode().unwrap(); let mut stdin = async_stdin().bytes(); loop { + let mut data = String::new(); + buff_r.read_line(&mut data).unwrap(); + + let has_target = match data.as_bytes() { + b"true\n" => true, + _ => false + }; + + write!(stdout, "{}{}use numpad to freely move\n", termion::clear::All, termion::cursor::Goto(1, 1)).unwrap(); + write!(stdout, "{}+ : speedup", termion::cursor::Goto(1, 2)).unwrap(); + write!(stdout, "{}- : slowdown", termion::cursor::Goto(1, 3)).unwrap(); + write!(stdout, "{}q : quit", termion::cursor::Goto(1, 4)).unwrap(); + + if has_target { + write!(stdout, "{}c : mimic targets velocity vector", termion::cursor::Goto(1,5)).unwrap(); + write!(stdout, "{}t : accelerate torwards target", termion::cursor::Goto(1,6)).unwrap(); + } + match stdin.next() { Some(c) => { let c = c.unwrap(); @@ -37,7 +57,19 @@ pub fn client_engines(mut stream : TcpStream) { impl Connection { pub fn server_engines(&mut self, masses : &mut Vec<Box<Mass>>) -> bool { + let m = masses.to_vec(); let ship = masses[self.index].downcast_mut::<Ship>().unwrap(); + + let mut send = String::new(); + match ship.recv_target().is_some() { + true => send.push_str("true\n"), + false => send.push_str("false\n"), + } + match self.stream.write(send.as_bytes()) { + Ok(_result) => (), + Err(_error) => return false, + } + let mut acceleration = (0.0, 0.0, 0.0); let mut data = String::new(); match self.buff_r.read_line(&mut data) { @@ -48,7 +80,32 @@ impl Connection { b"2\n" => acceleration.1 -= 0.1, b"4\n" => acceleration.2 += 0.1, b"6\n" => acceleration.2 -= 0.1, + b"+\n" => ship.speedup(), b"-\n" => ship.slow(), + b"c\n" => { + match ship.recv_target() { + Some(index) => { + let d_v = m[index].recv_velocity(); + let m_v = ship.recv_velocity(); + acceleration = (d_v.0 - m_v.0, + d_v.1 - m_v.1, + d_v.2 - m_v.2); + }, + None => (), + } + }, + b"t\n" => { + match ship.recv_target() { + Some(index) => { + let d_p = m[index].position(); + let m_p = ship.position(); + acceleration = ((d_p.0 - m_p.0) * 0.01, + (d_p.1 - m_p.1) * 0.01, + (d_p.2 - m_p.2) * 0.01); + }, + None => (), + } + }, _ => { if result == 0 { return false @@ -58,6 +115,7 @@ impl Connection { Err(_error) => (), } ship.give_acceleration(acceleration); + true } } diff --git a/src/mass.rs b/src/mass.rs index 22862b6..800ef58 100644 --- a/src/mass.rs +++ b/src/mass.rs @@ -6,6 +6,7 @@ pub trait Mass : Any { fn serialize(&self) -> String; fn process(&mut self); fn give_acceleration(&mut self, acceleration : (f64, f64, f64)); + fn recv_velocity(&self) -> (f64, f64, f64); fn box_clone(&self) -> Box<Mass>; } diff --git a/src/ship.rs b/src/ship.rs index 7eb3ba2..c33dc6c 100644 --- a/src/ship.rs +++ b/src/ship.rs @@ -46,6 +46,13 @@ impl Ship { } } + pub fn speedup(&mut self) { + self.velocity.0 *= 1.05; + self.velocity.1 *= 1.05; + self.velocity.2 *= 1.05; + + } + pub fn range(&self) -> f64 { self.r } @@ -82,6 +89,10 @@ impl Mass for Ship { Box::new((*self).clone()) } + fn recv_velocity(&self) -> (f64, f64, f64) { + self.velocity + } + fn give_acceleration(&mut self, acceleration : (f64, f64, f64)) { self.velocity.0 += acceleration.0; self.velocity.1 += acceleration.1; |