2019-04-14 23:28:29 +00:00
|
|
|
mod client_init;
|
2019-04-27 20:55:30 +00:00
|
|
|
mod start_singleplayer;
|
2019-03-04 07:28:16 +00:00
|
|
|
mod ui;
|
2019-01-02 22:08:13 +00:00
|
|
|
|
2019-03-17 17:52:54 +00:00
|
|
|
use super::char_selection::CharSelectionState;
|
2019-06-06 14:48:41 +00:00
|
|
|
use crate::{window::Event, Direction, GlobalState, PlayState, PlayStateResult};
|
2019-04-14 23:28:29 +00:00
|
|
|
use client_init::{ClientInit, Error as InitError};
|
|
|
|
use common::{clock::Clock, comp};
|
2019-06-06 14:48:41 +00:00
|
|
|
use log::warn;
|
2019-04-29 20:37:19 +00:00
|
|
|
use start_singleplayer::StartSingleplayerState;
|
2019-03-15 04:55:52 +00:00
|
|
|
use std::time::Duration;
|
2019-03-17 17:52:54 +00:00
|
|
|
use ui::{Event as MainMenuEvent, MainMenuUi};
|
2019-03-15 04:55:52 +00:00
|
|
|
use vek::*;
|
2019-01-02 21:25:01 +00:00
|
|
|
|
2019-03-04 07:28:16 +00:00
|
|
|
pub struct MainMenuState {
|
|
|
|
main_menu_ui: MainMenuUi,
|
2019-01-30 12:11:34 +00:00
|
|
|
}
|
2019-01-02 21:25:01 +00:00
|
|
|
|
2019-03-04 07:28:16 +00:00
|
|
|
impl MainMenuState {
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Create a new `MainMenuState`.
|
2019-04-18 17:40:29 +00:00
|
|
|
pub fn new(global_state: &mut GlobalState) -> Self {
|
2019-01-30 12:11:34 +00:00
|
|
|
Self {
|
2019-04-18 17:40:29 +00:00
|
|
|
main_menu_ui: MainMenuUi::new(global_state),
|
2019-01-30 12:11:34 +00:00
|
|
|
}
|
2019-01-02 21:25:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-27 20:55:30 +00:00
|
|
|
const DEFAULT_PORT: u16 = 59003;
|
|
|
|
|
2019-03-15 04:55:52 +00:00
|
|
|
// Background colour
|
|
|
|
const BG_COLOR: Rgba<f32> = Rgba {
|
|
|
|
r: 0.0,
|
|
|
|
g: 0.3,
|
|
|
|
b: 1.0,
|
|
|
|
a: 1.0,
|
|
|
|
};
|
2019-01-11 20:14:37 +00:00
|
|
|
|
2019-03-04 07:28:16 +00:00
|
|
|
impl PlayState for MainMenuState {
|
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-03-04 07:28:16 +00:00
|
|
|
let mut clock = Clock::new();
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Used for client creation.
|
2019-04-14 23:28:29 +00:00
|
|
|
let mut client_init: Option<ClientInit> = None;
|
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
loop {
|
2019-05-17 09:22:32 +00:00
|
|
|
// Handle window events.
|
2019-01-07 21:10:31 +00:00
|
|
|
for event in global_state.window.fetch_events() {
|
|
|
|
match event {
|
2019-01-11 23:18:34 +00:00
|
|
|
Event::Close => return PlayStateResult::Shutdown,
|
2019-05-17 09:22:32 +00:00
|
|
|
// Pass events to ui.
|
2019-03-22 03:55:42 +00:00
|
|
|
Event::Ui(event) => {
|
|
|
|
self.main_menu_ui.handle_event(event);
|
2019-02-16 03:01:42 +00:00
|
|
|
}
|
2019-05-17 09:22:32 +00:00
|
|
|
// Ignore all other events.
|
2019-03-15 04:55:52 +00:00
|
|
|
_ => {}
|
2019-01-07 21:10:31 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-02 21:25:01 +00:00
|
|
|
|
2019-02-12 04:14:55 +00:00
|
|
|
global_state.window.renderer_mut().clear(BG_COLOR);
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Poll client creation.
|
2019-04-15 20:51:32 +00:00
|
|
|
match client_init.as_ref().and_then(|init| init.poll()) {
|
2019-04-14 23:28:29 +00:00
|
|
|
Some(Ok(client)) => {
|
|
|
|
self.main_menu_ui.connected();
|
|
|
|
return PlayStateResult::Push(Box::new(CharSelectionState::new(
|
|
|
|
&mut global_state.window,
|
|
|
|
std::rc::Rc::new(std::cell::RefCell::new(client)),
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
Some(Err(err)) => {
|
|
|
|
client_init = None;
|
2019-04-15 20:51:32 +00:00
|
|
|
self.main_menu_ui.login_error(
|
|
|
|
match err {
|
|
|
|
InitError::BadAddress(_) | InitError::NoAddress => "Server not found",
|
|
|
|
InitError::ConnectionFailed(_) => "Connection failed",
|
2019-05-07 11:09:50 +00:00
|
|
|
InitError::ClientCrashed => "Client crashed",
|
2019-04-15 20:51:32 +00:00
|
|
|
}
|
|
|
|
.to_string(),
|
|
|
|
);
|
|
|
|
}
|
2019-04-14 23:28:29 +00:00
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
|
2019-05-18 20:10:02 +00:00
|
|
|
// Maintain global_state
|
|
|
|
global_state.maintain();
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Maintain the UI.
|
2019-04-29 20:37:19 +00:00
|
|
|
for event in self.main_menu_ui.maintain(global_state) {
|
2019-04-04 14:45:57 +00:00
|
|
|
match event {
|
2019-04-14 23:28:29 +00:00
|
|
|
MainMenuEvent::LoginAttempt {
|
|
|
|
username,
|
|
|
|
server_address,
|
|
|
|
} => {
|
2019-04-18 17:40:29 +00:00
|
|
|
let mut net_settings = &mut global_state.settings.networking;
|
|
|
|
net_settings.username = username.clone();
|
|
|
|
if !net_settings.servers.contains(&server_address) {
|
|
|
|
net_settings.servers.push(server_address.clone());
|
|
|
|
}
|
2019-06-06 14:48:41 +00:00
|
|
|
if let Err(err) = global_state.settings.save_to_file() {
|
|
|
|
warn!("Failed to save settings: {:?}", err);
|
|
|
|
}
|
2019-06-29 15:04:06 +00:00
|
|
|
|
|
|
|
let player = comp::Player::new(
|
|
|
|
username.clone(),
|
|
|
|
Some(global_state.settings.graphics.view_distance),
|
|
|
|
);
|
|
|
|
|
|
|
|
if player.is_valid() {
|
|
|
|
// Don't try to connect if there is already a connection in progress.
|
|
|
|
client_init = client_init.or(Some(ClientInit::new(
|
|
|
|
(server_address, DEFAULT_PORT, false),
|
|
|
|
player,
|
|
|
|
false,
|
|
|
|
)));
|
|
|
|
} else {
|
|
|
|
self.main_menu_ui.login_error("Invalid username".to_string());
|
|
|
|
}
|
2019-04-29 20:37:19 +00:00
|
|
|
}
|
2019-04-18 13:53:03 +00:00
|
|
|
MainMenuEvent::StartSingleplayer => {
|
2019-04-27 20:55:30 +00:00
|
|
|
return PlayStateResult::Push(Box::new(StartSingleplayerState::new()));
|
2019-04-29 20:37:19 +00:00
|
|
|
}
|
2019-06-10 20:44:03 +00:00
|
|
|
MainMenuEvent::Settings => {} // TODO
|
2019-04-04 14:45:57 +00:00
|
|
|
MainMenuEvent::Quit => return PlayStateResult::Shutdown,
|
2019-05-26 20:42:45 +00:00
|
|
|
MainMenuEvent::DisclaimerClosed => {
|
|
|
|
global_state.settings.show_disclaimer = false
|
|
|
|
}
|
2019-04-04 14:45:57 +00:00
|
|
|
}
|
2019-03-04 07:28:16 +00:00
|
|
|
}
|
2019-01-30 12:11:34 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Draw the UI to the screen.
|
2019-03-04 07:28:16 +00:00
|
|
|
self.main_menu_ui.render(global_state.window.renderer_mut());
|
2019-02-11 06:12:46 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Finish the frame.
|
2019-02-12 04:14:55 +00:00
|
|
|
global_state.window.renderer_mut().flush();
|
2019-03-15 04:55:52 +00:00
|
|
|
global_state
|
|
|
|
.window
|
2019-03-04 07:28:16 +00:00
|
|
|
.swap_buffers()
|
2019-05-17 09:22:32 +00:00
|
|
|
.expect("Failed to swap window buffers!");
|
2019-03-04 07:28:16 +00:00
|
|
|
|
2019-05-18 19:28:12 +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-01-02 21:25:01 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-11 23:18:34 +00:00
|
|
|
|
2019-03-15 04:55:52 +00:00
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"Title"
|
|
|
|
}
|
2019-01-02 21:25:01 +00:00
|
|
|
}
|