veloren/voxygen/src/menu/char_selection/mod.rs

215 lines
8.0 KiB
Rust
Raw Normal View History

mod ui;
use crate::{
i18n::{i18n_asset_key, VoxygenLocalization},
scene::simple::{self as scene, Scene},
session::SessionState,
window::Event as WinEvent,
Direction, GlobalState, PlayState, PlayStateResult,
};
use client::{self, Client};
use common::{assets, clock::Clock, comp, msg::ClientState, state::DeltaTime};
use specs::WorldExt;
use std::{cell::RefCell, rc::Rc, time::Duration};
use tracing::error;
use ui::CharSelectionUi;
pub struct CharSelectionState {
char_selection_ui: CharSelectionUi,
client: Rc<RefCell<Client>>,
scene: Scene,
}
impl CharSelectionState {
/// Create a new `CharSelectionState`.
2019-07-26 02:28:53 +00:00
pub fn new(global_state: &mut GlobalState, client: Rc<RefCell<Client>>) -> Self {
Self {
2019-07-26 02:28:53 +00:00
char_selection_ui: CharSelectionUi::new(global_state),
client,
scene: Scene::new(
global_state.window.renderer_mut(),
Some("fixture.selection_bg"),
),
}
}
}
impl PlayState for CharSelectionState {
fn play(&mut self, _: Direction, global_state: &mut GlobalState) -> PlayStateResult {
// Set up an fps clock.
let mut clock = Clock::start();
// Load the player's character list
self.client.borrow_mut().load_character_list();
let mut current_client_state = self.client.borrow().get_client_state();
while let ClientState::Pending | ClientState::Registered = current_client_state {
2019-09-18 16:46:12 +00:00
// Handle window events
for event in global_state.window.fetch_events(&mut global_state.settings) {
2019-09-18 16:46:12 +00:00
if self.char_selection_ui.handle_event(event.clone()) {
continue;
}
match event {
2019-09-18 16:46:12 +00:00
WinEvent::Close => {
return PlayStateResult::Shutdown;
},
// Pass all other events to the scene
event => {
self.scene.handle_input_event(event);
}, // TODO: Do something if the event wasn't handled?
}
}
2019-07-04 12:02:26 +00:00
global_state.window.renderer_mut().clear();
// Maintain the UI.
let events = self
.char_selection_ui
.maintain(global_state, &mut self.client.borrow_mut());
for event in events {
match event {
ui::Event::Logout => {
return PlayStateResult::Pop;
},
ui::Event::AddCharacter { alias, tool, body } => {
self.client.borrow_mut().create_character(alias, tool, body);
},
ui::Event::DeleteCharacter(character_id) => {
self.client.borrow_mut().delete_character(character_id);
},
ui::Event::Play => {
let char_data = self
.char_selection_ui
.get_character_list()
.expect("Character data is required to play");
if let Some(selected_character) =
char_data.get(self.char_selection_ui.selected_character)
{
if let Some(character_id) = selected_character.character.id {
self.client.borrow_mut().request_character(character_id);
}
}
2020-03-08 20:31:27 +00:00
return PlayStateResult::Switch(Box::new(SessionState::new(
2019-07-26 02:28:53 +00:00
global_state,
self.client.clone(),
)));
},
}
}
// Maintain global state.
2019-08-31 08:30:23 +00:00
global_state.maintain(clock.get_last_delta().as_secs_f32());
let humanoid_body = self
.char_selection_ui
.get_character_list()
.and_then(|data| {
if let Some(character) = data.get(self.char_selection_ui.selected_character) {
match character.body {
comp::Body::Humanoid(body) => Some(body),
_ => None,
}
} else {
None
}
});
let loadout = self.char_selection_ui.get_loadout();
// Maintain the scene.
{
let client = self.client.borrow();
let scene_data = scene::SceneData {
time: client.state().get_time(),
delta_time: client.state().ecs().read_resource::<DeltaTime>().0,
tick: client.get_tick(),
body: humanoid_body,
gamma: global_state.settings.graphics.gamma,
2020-04-23 22:59:34 +00:00
mouse_smoothing: global_state.settings.gameplay.smooth_pan_enable,
2020-04-26 01:44:56 +00:00
figure_lod_render_distance: global_state
.settings
.graphics
.figure_lod_render_distance
as f32,
};
self.scene.maintain(
global_state.window.renderer_mut(),
scene_data,
loadout.as_ref(),
);
}
// Render the scene.
self.scene.render(
global_state.window.renderer_mut(),
self.client.borrow().get_tick(),
humanoid_body,
2020-03-27 17:17:41 +00:00
loadout.as_ref(),
);
// Draw the UI to the screen.
self.char_selection_ui
.render(global_state.window.renderer_mut(), self.scene.globals());
// Tick the client (currently only to keep the connection alive).
let localized_strings = assets::load_expect::<VoxygenLocalization>(&i18n_asset_key(
&global_state.settings.language.selected_language,
));
2020-07-06 16:11:19 +00:00
match self.client.borrow_mut().tick(
2020-01-10 00:33:38 +00:00
comp::ControllerInputs::default(),
clock.get_last_delta(),
|_| {},
) {
2020-07-06 16:11:19 +00:00
Ok(events) => {
for event in events {
match event {
client::Event::SetViewDistance(vd) => {
global_state.settings.graphics.view_distance = vd;
global_state.settings.save_to_file_warn();
},
client::Event::Disconnect => {
global_state.info_message = Some(
localized_strings
.get("main.login.server_shut_down")
.to_owned(),
);
return PlayStateResult::Pop;
},
_ => {},
}
}
},
Err(err) => {
global_state.info_message =
Some(localized_strings.get("common.connection_lost").to_owned());
error!(?err, "[char_selection] Failed to tick the client");
return PlayStateResult::Pop;
},
}
self.client.borrow_mut().cleanup();
// 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.
2019-06-06 19:11:39 +00:00
clock.tick(Duration::from_millis(
1000 / (global_state.settings.graphics.max_fps as u64),
));
current_client_state = self.client.borrow().get_client_state();
}
PlayStateResult::Pop
}
fn name(&self) -> &'static str { "Title" }
}