2019-03-17 17:52:54 +00:00
|
|
|
mod ui;
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
session::SessionState,
|
2019-04-29 20:37:19 +00:00
|
|
|
window::{Event, Window},
|
2019-04-27 20:55:30 +00:00
|
|
|
Direction, GlobalState, PlayState, PlayStateResult,
|
2019-03-17 17:52:54 +00:00
|
|
|
};
|
2019-04-04 14:45:57 +00:00
|
|
|
use client::{self, Client};
|
2019-04-29 20:37:19 +00:00
|
|
|
use common::{clock::Clock, msg::ClientMsg};
|
2019-04-04 14:45:57 +00:00
|
|
|
use std::{cell::RefCell, rc::Rc, time::Duration};
|
2019-03-17 17:52:54 +00:00
|
|
|
use ui::CharSelectionUi;
|
|
|
|
use vek::*;
|
|
|
|
|
|
|
|
const FPS: u64 = 60;
|
|
|
|
|
|
|
|
pub struct CharSelectionState {
|
|
|
|
char_selection_ui: CharSelectionUi,
|
2019-04-04 14:45:57 +00:00
|
|
|
client: Rc<RefCell<Client>>,
|
2019-03-17 17:52:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CharSelectionState {
|
|
|
|
/// Create a new `CharSelectionState`
|
2019-04-04 14:45:57 +00:00
|
|
|
pub fn new(window: &mut Window, client: Rc<RefCell<Client>>) -> Self {
|
2019-03-17 17:52:54 +00:00
|
|
|
Self {
|
|
|
|
char_selection_ui: CharSelectionUi::new(window),
|
2019-04-04 14:45:57 +00:00
|
|
|
client,
|
2019-03-17 17:52:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The background colour
|
|
|
|
const BG_COLOR: Rgba<f32> = Rgba {
|
|
|
|
r: 0.0,
|
|
|
|
g: 0.3,
|
|
|
|
b: 1.0,
|
|
|
|
a: 1.0,
|
|
|
|
};
|
|
|
|
|
|
|
|
impl PlayState for CharSelectionState {
|
2019-04-27 20:55:30 +00:00
|
|
|
fn play(&mut self, _: Direction, global_state: &mut GlobalState) -> PlayStateResult {
|
2019-03-17 17:52:54 +00:00
|
|
|
// Set up an fps clock
|
|
|
|
let mut clock = Clock::new();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
// Handle window events
|
|
|
|
for event in global_state.window.fetch_events() {
|
|
|
|
match event {
|
2019-04-18 13:53:03 +00:00
|
|
|
Event::Close => {
|
|
|
|
return PlayStateResult::Shutdown;
|
2019-04-29 20:37:19 +00:00
|
|
|
}
|
2019-03-17 17:52:54 +00:00
|
|
|
// Pass events to ui
|
2019-03-22 03:55:42 +00:00
|
|
|
Event::Ui(event) => {
|
|
|
|
self.char_selection_ui.handle_event(event);
|
2019-03-17 17:52:54 +00:00
|
|
|
}
|
|
|
|
// Ignore all other events
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
global_state.window.renderer_mut().clear(BG_COLOR);
|
|
|
|
|
|
|
|
// Maintain the UI
|
2019-04-29 20:37:19 +00:00
|
|
|
for event in self
|
|
|
|
.char_selection_ui
|
|
|
|
.maintain(global_state.window.renderer_mut())
|
|
|
|
{
|
2019-03-22 06:29:09 +00:00
|
|
|
match event {
|
2019-04-18 13:53:03 +00:00
|
|
|
ui::Event::Logout => {
|
|
|
|
return PlayStateResult::Pop;
|
2019-04-29 20:37:19 +00:00
|
|
|
}
|
2019-04-19 19:32:47 +00:00
|
|
|
ui::Event::Play => {
|
2019-04-29 20:37:19 +00:00
|
|
|
self.client
|
|
|
|
.borrow_mut()
|
|
|
|
.postbox
|
|
|
|
.send_message(ClientMsg::Character(self.char_selection_ui.character));
|
|
|
|
return PlayStateResult::Switch(Box::new(SessionState::new(
|
|
|
|
&mut global_state.window,
|
|
|
|
self.client.clone(),
|
2019-05-01 22:15:43 +00:00
|
|
|
global_state.settings.clone(),
|
2019-04-29 20:37:19 +00:00
|
|
|
)));
|
2019-04-20 19:33:47 +00:00
|
|
|
}
|
2019-03-22 06:29:09 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-17 17:52:54 +00:00
|
|
|
|
|
|
|
// Draw the UI to the screen
|
2019-04-29 20:37:19 +00:00
|
|
|
self.char_selection_ui
|
|
|
|
.render(global_state.window.renderer_mut());
|
2019-03-17 17:52:54 +00:00
|
|
|
|
2019-04-04 14:45:57 +00:00
|
|
|
// Tick the client (currently only to keep the connection alive)
|
2019-04-29 20:37:19 +00:00
|
|
|
self.client
|
|
|
|
.borrow_mut()
|
|
|
|
.tick(client::Input::default(), clock.get_last_delta())
|
2019-04-04 14:45:57 +00:00
|
|
|
.expect("Failed to tick the client");
|
|
|
|
self.client.borrow_mut().cleanup();
|
|
|
|
|
2019-03-17 17:52:54 +00:00
|
|
|
// Finish the frame
|
|
|
|
global_state.window.renderer_mut().flush();
|
|
|
|
global_state
|
|
|
|
.window
|
|
|
|
.swap_buffers()
|
|
|
|
.expect("Failed to swap window buffers");
|
|
|
|
|
|
|
|
// Wait for the next tick
|
|
|
|
clock.tick(Duration::from_millis(1000 / FPS));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"Title"
|
|
|
|
}
|
|
|
|
}
|