diff options
author | tom barrett <spalf0@gmail.com> | 2018-03-12 08:15:51 -0500 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2018-03-12 08:15:51 -0500 |
commit | 74e1c462f6dfa76243917ef358195f057c236b5d (patch) | |
tree | 1bc65b13369cdb642629a01b8c6d72b017cfb7d5 /src | |
parent | 160d7b6e50df6d818f6882c033398d49193bd804 (diff) |
-better implementation of server navigation and added range checking of targets
Diffstat (limited to 'src')
-rw-r--r-- | src/navigation.rs | 43 | ||||
-rw-r--r-- | src/ship.rs | 4 |
2 files changed, 29 insertions, 18 deletions
diff --git a/src/navigation.rs b/src/navigation.rs index b615eaa..67f53cb 100644 --- a/src/navigation.rs +++ b/src/navigation.rs @@ -79,23 +79,30 @@ pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : Buf impl Connection { pub fn server_navigation(&mut self, masses : &mut Vec<Box<Mass>>) -> bool { - let position = masses[self.index].position(); - let range = masses[self.index].downcast_ref::<Ship>().unwrap().range(); + let m = masses.to_vec(); + let ship = masses[self.index].downcast_mut::<Ship>().unwrap(); - { - let within_range : Vec<&Box<Mass>> = masses.iter().filter(|mass| - distance(position, mass.position()) < range) - .collect(); - let mut send = String::new(); - for mass in within_range { - send.push_str(&mass.serialize()); - send.push_str(";"); - } - send.push_str("\n"); - match self.stream.write(send.as_bytes()) { - Ok(_result) => (), - Err(_error) => return false, + match ship.recv_target() { + Some(index) => { + if distance(m[index].position(), ship.position()) > ship.range() { + ship.give_target(None); + } } + None => (), + } + + let within_range : Vec<&Box<Mass>> = m.iter().filter(|mass| + distance(ship.position(), mass.position()) < ship.range()) + .collect(); + let mut send = String::new(); + for mass in within_range { + send.push_str(&mass.serialize()); + send.push_str(";"); + } + send.push_str("\n"); + match self.stream.write(send.as_bytes()) { + Ok(_result) => (), + Err(_error) => return false, } let mut string_mass = String::new(); @@ -105,9 +112,9 @@ impl Connection { } if string_mass.len() > 0 { let target = build_mass(&string_mass); - let t = masses.iter().position(|mass| - mass.name() == target.name()); - masses[self.index].downcast_mut::<Ship>().unwrap().give_target(t); + let t = m.iter().position(|mass| + mass.name() == target.name()); + ship.give_target(t); } true } diff --git a/src/ship.rs b/src/ship.rs index b4b32e7..7eb3ba2 100644 --- a/src/ship.rs +++ b/src/ship.rs @@ -53,6 +53,10 @@ impl Ship { pub fn give_target(&mut self, target : Option<usize>) { self.target = target; } + + pub fn recv_target(&self) -> Option<usize> { + self.target + } } impl Mass for Ship { |