From 573ba69d810914c153a578747414b3d631e61bbc Mon Sep 17 00:00:00 2001 From: tom barrett Date: Thu, 12 Apr 2018 04:33:23 -0500 Subject: completely restructured code and fixed navigation bug --- src/client/engines.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/client/engines.rs (limited to 'src/client/engines.rs') diff --git a/src/client/engines.rs b/src/client/engines.rs new file mode 100644 index 0000000..b6f5f93 --- /dev/null +++ b/src/client/engines.rs @@ -0,0 +1,49 @@ +extern crate termion; +extern crate serde_json; + +use std::thread::sleep; +use std::time::Duration; +use std::net::TcpStream; +use self::termion::async_stdin; +use std::io::{BufRead, BufReader}; +use std::io::{Read, Write, stdout}; +use self::termion::raw::IntoRawMode; + +pub fn client_engines(mut stream : TcpStream, mut buff_r : BufReader) { + let stdout = stdout(); + let mut stdout = stdout.lock().into_raw_mode().unwrap(); + let mut stdin = async_stdin().bytes(); + + loop { + let mut recv = String::new(); + buff_r.read_line(&mut recv).unwrap(); + let has_target = serde_json::from_str(&recv.replace("\n", "")).unwrap(); + + 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(); + let mut send = String::new(); + send.push(c as char); + if send.as_bytes() == b"q" { + break; + } + send.push_str("\n"); + stream.write(send.as_bytes()).unwrap(); + } + None => () + } + + stdout.flush().unwrap(); + sleep(Duration::from_millis(100)); + } +} -- cgit v1.2.3