2019-05-12 13:02:47 +00:00
|
|
|
mod scene;
|
2019-05-12 19:57:39 +00:00
|
|
|
mod ui;
|
2019-03-17 17:52:54 +00:00
|
|
|
|
|
|
|
use crate::{
|
2019-07-26 02:28:53 +00:00
|
|
|
session::SessionState, window::Event, 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-06-06 14:48:41 +00:00
|
|
|
use common::{clock::Clock, comp, msg::ClientState};
|
|
|
|
use log::error;
|
2019-05-12 19:57:39 +00:00
|
|
|
use scene::Scene;
|
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;
|
|
|
|
|
|
|
|
pub struct CharSelectionState {
|
|
|
|
char_selection_ui: CharSelectionUi,
|
2019-04-04 14:45:57 +00:00
|
|
|
client: Rc<RefCell<Client>>,
|
2019-05-12 13:02:47 +00:00
|
|
|
scene: Scene,
|
2019-03-17 17:52:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CharSelectionState {
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Create a new `CharSelectionState`.
|
2019-07-26 02:28:53 +00:00
|
|
|
pub fn new(global_state: &mut GlobalState, client: Rc<RefCell<Client>>) -> Self {
|
2019-03-17 17:52:54 +00:00
|
|
|
Self {
|
2019-07-26 02:28:53 +00:00
|
|
|
char_selection_ui: CharSelectionUi::new(global_state),
|
2019-04-04 14:45:57 +00:00
|
|
|
client,
|
2019-07-26 02:28:53 +00:00
|
|
|
scene: Scene::new(global_state.window.renderer_mut()),
|
2019-03-17 17:52:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PlayState for CharSelectionState {
|
2019-04-27 20:55:30 +00:00
|
|
|
fn play(&mut self, _: Direction, global_state: &mut GlobalState) -> PlayStateResult {
|
2019-05-17 09:22:32 +00:00
|
|
|
// Set up an fps clock.
|
2019-07-01 20:42:43 +00:00
|
|
|
let mut clock = Clock::start();
|
2019-03-17 17:52:54 +00:00
|
|
|
|
2019-05-24 19:10:18 +00:00
|
|
|
let mut current_client_state = self.client.borrow().get_client_state();
|
|
|
|
while let ClientState::Pending | ClientState::Registered = current_client_state {
|
2019-05-17 09:22:32 +00:00
|
|
|
// Handle window events.
|
2019-03-17 17:52:54 +00:00
|
|
|
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-05-17 09:22:32 +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
|
|
|
}
|
2019-08-25 00:42:43 +00:00
|
|
|
// Pass all other events to the scene
|
|
|
|
event => {
|
|
|
|
self.scene.handle_input_event(event);
|
|
|
|
} // TODO: Do something if the event wasn't handled?
|
2019-03-17 17:52:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-04 12:02:26 +00:00
|
|
|
global_state.window.renderer_mut().clear();
|
2019-03-17 17:52:54 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Maintain the UI.
|
2019-07-21 18:13:17 +00:00
|
|
|
let events = self
|
2019-04-29 20:37:19 +00:00
|
|
|
.char_selection_ui
|
2019-07-21 18:13:17 +00:00
|
|
|
.maintain(global_state.window.renderer_mut(), &self.client.borrow());
|
|
|
|
for event in events {
|
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-05-17 20:47:58 +00:00
|
|
|
self.client.borrow_mut().request_character(
|
|
|
|
self.char_selection_ui.character_name.clone(),
|
|
|
|
comp::Body::Humanoid(self.char_selection_ui.character_body),
|
2019-08-27 19:56:46 +00:00
|
|
|
self.char_selection_ui.character_tool,
|
2019-05-17 20:47:58 +00:00
|
|
|
);
|
2019-05-19 17:22:35 +00:00
|
|
|
return PlayStateResult::Push(Box::new(SessionState::new(
|
2019-07-26 02:28:53 +00:00
|
|
|
global_state,
|
2019-04-29 20:37:19 +00:00
|
|
|
self.client.clone(),
|
|
|
|
)));
|
2019-04-20 19:33:47 +00:00
|
|
|
}
|
2019-03-22 06:29:09 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-17 17:52:54 +00:00
|
|
|
|
2019-05-31 01:33:54 +00:00
|
|
|
// Maintain global state.
|
2019-08-31 08:30:23 +00:00
|
|
|
global_state.maintain(clock.get_last_delta().as_secs_f32());
|
2019-05-18 20:10:02 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Maintain the scene.
|
2019-06-28 08:24:13 +00:00
|
|
|
self.scene.maintain(
|
|
|
|
global_state.window.renderer_mut(),
|
|
|
|
&self.client.borrow(),
|
|
|
|
self.char_selection_ui.character_body,
|
|
|
|
);
|
2019-05-12 13:02:47 +00:00
|
|
|
|
2019-05-31 01:33:54 +00:00
|
|
|
// Render the scene.
|
2019-05-18 19:03:13 +00:00
|
|
|
self.scene.render(
|
|
|
|
global_state.window.renderer_mut(),
|
|
|
|
&self.client.borrow(),
|
|
|
|
self.char_selection_ui.character_body,
|
2019-08-27 19:56:46 +00:00
|
|
|
&comp::Equipment {
|
|
|
|
main: if let Some(kind) = self.char_selection_ui.character_tool {
|
|
|
|
Some(comp::Item::Tool {
|
|
|
|
kind: kind,
|
|
|
|
power: 10,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
|
|
|
alt: None,
|
|
|
|
},
|
2019-05-18 19:03:13 +00:00
|
|
|
);
|
2019-05-12 13:02:47 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Draw the UI to the screen.
|
2019-04-29 20:37:19 +00:00
|
|
|
self.char_selection_ui
|
2019-05-14 06:43:07 +00:00
|
|
|
.render(global_state.window.renderer_mut(), self.scene.globals());
|
2019-03-17 17:52:54 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Tick the client (currently only to keep the connection alive).
|
2019-05-26 09:21:51 +00:00
|
|
|
if let Err(err) = self
|
|
|
|
.client
|
2019-04-29 20:37:19 +00:00
|
|
|
.borrow_mut()
|
2019-06-09 14:20:20 +00:00
|
|
|
.tick(comp::Controller::default(), clock.get_last_delta())
|
2019-05-26 09:21:51 +00:00
|
|
|
{
|
2019-06-06 14:48:41 +00:00
|
|
|
error!("Failed to tick the scene: {:?}", err);
|
2019-05-26 09:21:51 +00:00
|
|
|
return PlayStateResult::Pop;
|
|
|
|
}
|
2019-04-04 14:45:57 +00:00
|
|
|
self.client.borrow_mut().cleanup();
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Finish the frame.
|
2019-03-17 17:52:54 +00:00
|
|
|
global_state.window.renderer_mut().flush();
|
|
|
|
global_state
|
|
|
|
.window
|
|
|
|
.swap_buffers()
|
|
|
|
.expect("Failed to swap window buffers");
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Wait for the next tick.
|
2019-06-06 19:11:39 +00:00
|
|
|
clock.tick(Duration::from_millis(
|
|
|
|
1000 / (global_state.settings.graphics.max_fps as u64),
|
|
|
|
));
|
2019-05-24 19:10:18 +00:00
|
|
|
|
|
|
|
current_client_state = self.client.borrow().get_client_state();
|
2019-03-17 17:52:54 +00:00
|
|
|
}
|
2019-05-19 18:22:45 +00:00
|
|
|
|
|
|
|
PlayStateResult::Pop
|
2019-03-17 17:52:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"Title"
|
|
|
|
}
|
|
|
|
}
|