Stop singleplayer thread whenever a session is left

Former-commit-id: 16fcf35cbc856bebc12944781c2ac1a4daab5550
This commit is contained in:
Louis Pearson 2019-04-17 09:22:26 -06:00
parent 76adbec3bb
commit a768971c6c
3 changed files with 21 additions and 7 deletions

View File

@ -45,7 +45,10 @@ impl PlayState for CharSelectionState {
// Handle window events // Handle window events
for event in global_state.window.fetch_events() { for event in global_state.window.fetch_events() {
match event { match event {
Event::Close => return PlayStateResult::Shutdown, Event::Close => {
global_state.singleplayer = None;
return PlayStateResult::Shutdown;
},
// Pass events to ui // Pass events to ui
Event::Ui(event) => { Event::Ui(event) => {
self.char_selection_ui.handle_event(event); self.char_selection_ui.handle_event(event);
@ -60,7 +63,10 @@ impl PlayState for CharSelectionState {
// Maintain the UI // Maintain the UI
for event in self.char_selection_ui.maintain(global_state.window.renderer_mut()) { for event in self.char_selection_ui.maintain(global_state.window.renderer_mut()) {
match event { match event {
ui::Event::Logout => return PlayStateResult::Pop, ui::Event::Logout => {
global_state.singleplayer = None;
return PlayStateResult::Pop;
},
ui::Event::Play => return PlayStateResult::Push( ui::Event::Play => return PlayStateResult::Push(
Box::new(SessionState::new(&mut global_state.window, self.client.clone())) Box::new(SessionState::new(&mut global_state.window, self.client.clone()))
), ),

View File

@ -125,7 +125,10 @@ impl PlayState for SessionState {
continue; continue;
} }
let _handled = match event { let _handled = match event {
Event::Close => return PlayStateResult::Shutdown, Event::Close => {
global_state.singleplayer = None;
return PlayStateResult::Shutdown;
},
// Toggle cursor grabbing // Toggle cursor grabbing
Event::KeyDown(Key::ToggleCursor) => { Event::KeyDown(Key::ToggleCursor) => {
global_state global_state
@ -166,7 +169,10 @@ impl PlayState for SessionState {
self.client.borrow_mut().send_chat(msg); self.client.borrow_mut().send_chat(msg);
}, },
HudEvent::Logout => return PlayStateResult::Pop, HudEvent::Logout => return PlayStateResult::Pop,
HudEvent::Quit => return PlayStateResult::Shutdown, HudEvent::Quit => {
global_state.singleplayer = None;
return PlayStateResult::Shutdown;
},
} }
} }

View File

@ -7,7 +7,7 @@ use std::{
thread::JoinHandle thread::JoinHandle
}; };
use std::sync::mpsc::{ use std::sync::mpsc::{
channel, Receiver, Sender channel, Receiver, Sender, TryRecvError,
}; };
const TPS: u64 = 30; const TPS: u64 = 30;
@ -16,6 +16,8 @@ enum Msg {
Stop, Stop,
} }
/// Used to start and stop the background thread running the server
/// when in singleplayer mode.
pub struct Singleplayer { pub struct Singleplayer {
server_thread: JoinHandle<()>, server_thread: JoinHandle<()>,
sender: Sender<Msg>, sender: Sender<Msg>,
@ -68,8 +70,8 @@ fn run_server(rec: Receiver<Msg>) {
match rec.try_recv() { match rec.try_recv() {
Ok(msg) => break, Ok(msg) => break,
Err(err) => match err { Err(err) => match err {
Empty => (), TryRecvError::Empty => (),
Disconnected => break, TryRecvError::Disconnected => break,
}, },
} }