2019-01-11 23:18:34 +00:00
|
|
|
use crate::{
|
2020-06-28 15:21:12 +00:00
|
|
|
audio::sfx::{SfxEvent, SfxEventItem},
|
2020-01-10 00:33:38 +00:00
|
|
|
ecs::MyEntity,
|
2020-05-18 22:40:28 +00:00
|
|
|
hud::{DebugInfo, Event as HudEvent, Hud, HudInfo, PressBehavior},
|
2020-02-01 20:39:39 +00:00
|
|
|
i18n::{i18n_asset_key, VoxygenLocalization},
|
2019-03-02 03:48:30 +00:00
|
|
|
key_state::KeyState,
|
2020-03-08 20:31:27 +00:00
|
|
|
menu::char_selection::CharSelectionState,
|
2020-02-29 03:59:11 +00:00
|
|
|
scene::{camera, Scene, SceneData},
|
2020-03-31 18:12:49 +00:00
|
|
|
settings::AudioOutput,
|
2020-03-10 21:00:13 +00:00
|
|
|
window::{AnalogGameInput, Event, GameInput},
|
2019-04-29 20:37:19 +00:00
|
|
|
Direction, Error, GlobalState, PlayState, PlayStateResult,
|
2019-01-11 23:18:34 +00:00
|
|
|
};
|
2020-06-02 02:42:26 +00:00
|
|
|
use client::{self, Client};
|
2019-08-06 21:51:13 +00:00
|
|
|
use common::{
|
2020-07-01 19:05:44 +00:00
|
|
|
assets::{load_expect, load_watched, watch},
|
2019-08-14 21:28:37 +00:00
|
|
|
clock::Clock,
|
|
|
|
comp,
|
2020-06-28 15:21:12 +00:00
|
|
|
comp::{ChatMsg, ChatType, InventoryUpdateEvent, Pos, Vel, MAX_PICKUP_RANGE_SQR},
|
|
|
|
event::EventBus,
|
2020-06-02 02:42:26 +00:00
|
|
|
msg::ClientState,
|
2019-08-14 21:28:37 +00:00
|
|
|
terrain::{Block, BlockKind},
|
2020-03-28 01:31:22 +00:00
|
|
|
util::Dir,
|
2019-08-14 21:28:37 +00:00
|
|
|
vol::ReadVol,
|
2019-08-06 21:51:13 +00:00
|
|
|
};
|
2019-11-30 06:41:20 +00:00
|
|
|
use specs::{Join, WorldExt};
|
2020-06-05 20:53:15 +00:00
|
|
|
use std::{cell::RefCell, rc::Rc, time::Duration};
|
2020-06-21 10:22:26 +00:00
|
|
|
use tracing::{error, info};
|
2019-05-09 17:58:16 +00:00
|
|
|
use vek::*;
|
2019-01-11 23:18:34 +00:00
|
|
|
|
2020-03-08 08:06:22 +00:00
|
|
|
/// The action to perform after a tick
|
|
|
|
enum TickAction {
|
|
|
|
// Continue executing
|
|
|
|
Continue,
|
|
|
|
// Disconnected (i.e. go to main menu)
|
|
|
|
Disconnect,
|
|
|
|
}
|
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
pub struct SessionState {
|
|
|
|
scene: Scene,
|
2019-04-04 14:45:57 +00:00
|
|
|
client: Rc<RefCell<Client>>,
|
2019-03-15 04:55:52 +00:00
|
|
|
hud: Hud,
|
2019-06-09 14:20:20 +00:00
|
|
|
key_state: KeyState,
|
2019-10-15 04:06:14 +00:00
|
|
|
inputs: comp::ControllerInputs,
|
2019-07-04 19:59:57 +00:00
|
|
|
selected_block: Block,
|
2020-07-01 19:05:44 +00:00
|
|
|
voxygen_i18n: std::sync::Arc<VoxygenLocalization>,
|
2019-01-11 23:18:34 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Represents an active game session (i.e., the one being played).
|
2019-01-11 23:18:34 +00:00
|
|
|
impl SessionState {
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Create a new `SessionState`.
|
2019-07-26 02:28:53 +00:00
|
|
|
pub fn new(global_state: &mut GlobalState, client: Rc<RefCell<Client>>) -> Self {
|
2020-02-01 20:39:39 +00:00
|
|
|
// Create a scene for this session. The scene handles visible elements of the
|
|
|
|
// game world.
|
2019-08-05 16:37:52 +00:00
|
|
|
let mut scene = Scene::new(global_state.window.renderer_mut());
|
2019-08-05 16:47:07 +00:00
|
|
|
scene
|
|
|
|
.camera_mut()
|
|
|
|
.set_fov_deg(global_state.settings.graphics.fov);
|
2019-10-16 11:39:41 +00:00
|
|
|
let hud = Hud::new(global_state, &client.borrow());
|
2020-01-10 00:33:38 +00:00
|
|
|
{
|
2020-07-07 00:01:39 +00:00
|
|
|
let mut client = client.borrow_mut();
|
|
|
|
let my_entity = client.entity();
|
2020-07-07 00:11:37 +00:00
|
|
|
client.state_mut().ecs_mut().insert(MyEntity(my_entity));
|
2020-01-10 00:33:38 +00:00
|
|
|
}
|
2020-07-01 19:05:44 +00:00
|
|
|
let voxygen_i18n = load_expect::<VoxygenLocalization>(&i18n_asset_key(
|
|
|
|
&global_state.settings.language.selected_language,
|
|
|
|
));
|
2019-04-04 14:45:57 +00:00
|
|
|
Self {
|
|
|
|
scene,
|
2019-01-15 15:13:11 +00:00
|
|
|
client,
|
2019-03-02 03:48:30 +00:00
|
|
|
key_state: KeyState::new(),
|
2019-10-15 04:06:14 +00:00
|
|
|
inputs: comp::ControllerInputs::default(),
|
2019-10-16 11:39:41 +00:00
|
|
|
hud,
|
2019-08-14 21:28:37 +00:00
|
|
|
selected_block: Block::new(BlockKind::Normal, Rgb::broadcast(255)),
|
2020-07-01 19:05:44 +00:00
|
|
|
voxygen_i18n,
|
2019-04-04 14:45:57 +00:00
|
|
|
}
|
2019-01-11 23:18:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 15:57:19 +00:00
|
|
|
impl SessionState {
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Tick the session (and the client attached to it).
|
2020-06-25 11:20:09 +00:00
|
|
|
fn tick(&mut self, dt: Duration, global_state: &mut GlobalState) -> Result<TickAction, Error> {
|
2020-03-24 07:38:16 +00:00
|
|
|
self.inputs.tick(dt);
|
|
|
|
|
2020-06-04 07:11:35 +00:00
|
|
|
let mut client = self.client.borrow_mut();
|
|
|
|
for event in client.tick(self.inputs.clone(), dt, crate::ecs::sys::add_local_systems)? {
|
2020-07-01 19:05:44 +00:00
|
|
|
self.voxygen_i18n = load_expect::<VoxygenLocalization>(&i18n_asset_key(
|
|
|
|
&global_state.settings.language.selected_language,
|
|
|
|
));
|
2019-03-17 05:26:51 +00:00
|
|
|
match event {
|
2020-06-02 02:42:26 +00:00
|
|
|
client::Event::Chat(m) => {
|
|
|
|
self.hud.new_message(m);
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-06-28 15:21:12 +00:00
|
|
|
client::Event::InventoryUpdated(inv_event) => {
|
|
|
|
let sfx_event = SfxEvent::from(&inv_event);
|
|
|
|
client
|
|
|
|
.state()
|
|
|
|
.ecs()
|
|
|
|
.read_resource::<EventBus<SfxEventItem>>()
|
|
|
|
.emit_now(SfxEventItem::at_player_position(sfx_event));
|
|
|
|
|
|
|
|
match inv_event {
|
|
|
|
InventoryUpdateEvent::CollectFailed => {
|
|
|
|
self.hud.new_message(ChatMsg {
|
2020-07-01 19:05:44 +00:00
|
|
|
message: self.voxygen_i18n.get("hud.chat.loot_fail").to_string(),
|
2020-06-28 15:21:12 +00:00
|
|
|
chat_type: ChatType::CommandError,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
InventoryUpdateEvent::Collected(item) => {
|
|
|
|
self.hud.new_message(ChatMsg {
|
2020-07-01 19:05:44 +00:00
|
|
|
message: self
|
|
|
|
.voxygen_i18n
|
|
|
|
.get("hud.chat.loot_msg")
|
|
|
|
.replace("{item}", item.name().to_string().as_str()),
|
|
|
|
chat_type: ChatType::Loot,
|
2020-06-28 15:21:12 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
};
|
|
|
|
},
|
2020-03-08 08:06:22 +00:00
|
|
|
client::Event::Disconnect => return Ok(TickAction::Disconnect),
|
2019-10-17 04:09:01 +00:00
|
|
|
client::Event::DisconnectionNotification(time) => {
|
2019-10-22 03:46:12 +00:00
|
|
|
let message = match time {
|
2020-07-01 19:05:44 +00:00
|
|
|
0 => String::from(self.voxygen_i18n.get("hud.chat.goodbye")),
|
|
|
|
_ => self
|
|
|
|
.voxygen_i18n
|
|
|
|
.get("hud.chat.connection_lost")
|
|
|
|
.replace("{time}", time.to_string().as_str()),
|
2019-10-22 03:46:12 +00:00
|
|
|
};
|
|
|
|
|
2020-06-28 15:21:12 +00:00
|
|
|
self.hud.new_message(ChatMsg {
|
|
|
|
chat_type: ChatType::CommandError,
|
2019-10-22 03:46:12 +00:00
|
|
|
message,
|
2019-10-17 04:09:01 +00:00
|
|
|
});
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-06-02 02:42:26 +00:00
|
|
|
client::Event::Notification(n) => {
|
|
|
|
self.hud.new_notification(n);
|
2020-05-14 16:56:10 +00:00
|
|
|
},
|
2020-06-25 11:20:09 +00:00
|
|
|
client::Event::SetViewDistance(vd) => {
|
|
|
|
global_state.settings.graphics.view_distance = vd;
|
|
|
|
global_state.settings.save_to_file_warn();
|
|
|
|
},
|
2019-03-17 05:26:51 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-02 01:05:18 +00:00
|
|
|
|
2020-03-07 22:14:21 +00:00
|
|
|
Ok(TickAction::Continue)
|
2019-01-14 15:47:57 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Clean up the session (and the client attached to it) after a tick.
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn cleanup(&mut self) { self.client.borrow_mut().cleanup(); }
|
2019-01-12 15:57:19 +00:00
|
|
|
}
|
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
impl PlayState for SessionState {
|
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
|
|
|
// Trap the cursor.
|
2019-01-23 22:21:47 +00:00
|
|
|
global_state.window.grab_cursor(true);
|
2019-01-12 01:14:58 +00:00
|
|
|
|
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-05-25 12:23:56 +00:00
|
|
|
self.client.borrow_mut().clear_terrain();
|
2019-01-12 15:57:19 +00:00
|
|
|
|
2019-07-18 22:50:46 +00:00
|
|
|
// Send startup commands to the server
|
|
|
|
if global_state.settings.send_logon_commands {
|
|
|
|
for cmd in &global_state.settings.logon_commands {
|
|
|
|
self.client.borrow_mut().send_chat(cmd.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-17 23:43:18 +00:00
|
|
|
// Keep a watcher on the language
|
|
|
|
let mut localization_watcher = watch::ReloadIndicator::new();
|
|
|
|
let mut localized_strings = load_watched::<VoxygenLocalization>(
|
|
|
|
&i18n_asset_key(&global_state.settings.language.selected_language),
|
|
|
|
&mut localization_watcher,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2020-07-09 17:12:11 +00:00
|
|
|
let mut walk_forward_dir = self.scene.camera().forward_xy();
|
|
|
|
let mut walk_right_dir = self.scene.camera().right_xy();
|
|
|
|
let mut freefly_vel = Vec3::zero();
|
2020-03-26 21:22:21 +00:00
|
|
|
let mut free_look = false;
|
2020-06-06 18:09:01 +00:00
|
|
|
let mut auto_walk = false;
|
|
|
|
|
|
|
|
fn stop_auto_walk(auto_walk: &mut bool, key_state: &mut KeyState, hud: &mut Hud) {
|
|
|
|
*auto_walk = false;
|
|
|
|
hud.auto_walk(false);
|
|
|
|
key_state.auto_walk = false;
|
|
|
|
}
|
2020-03-26 21:22:21 +00:00
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
// Game loop
|
2019-05-24 19:10:18 +00:00
|
|
|
let mut current_client_state = self.client.borrow().get_client_state();
|
2019-12-31 08:10:51 +00:00
|
|
|
while let ClientState::Pending | ClientState::Character = current_client_state {
|
2019-08-24 17:58:28 +00:00
|
|
|
// Compute camera data
|
2020-02-29 03:59:11 +00:00
|
|
|
self.scene
|
|
|
|
.camera_mut()
|
|
|
|
.compute_dependents(&*self.client.borrow().state().terrain());
|
|
|
|
let camera::Dependents {
|
|
|
|
view_mat, cam_pos, ..
|
|
|
|
} = self.scene.camera().dependents();
|
2020-03-10 20:50:04 +00:00
|
|
|
|
|
|
|
// Choose a spot above the player's head for item distance checks
|
|
|
|
let player_pos = match self
|
|
|
|
.client
|
|
|
|
.borrow()
|
|
|
|
.state()
|
|
|
|
.read_storage::<comp::Pos>()
|
|
|
|
.get(self.client.borrow().entity())
|
|
|
|
{
|
|
|
|
Some(pos) => pos.0 + (Vec3::unit_z() * 2.0),
|
|
|
|
_ => cam_pos, // Should never happen, but a safe fallback
|
|
|
|
};
|
|
|
|
|
2020-05-19 13:26:53 +00:00
|
|
|
let (is_aiming, aim_dir_offset) = {
|
|
|
|
let client = self.client.borrow();
|
|
|
|
let is_aiming = client
|
|
|
|
.state()
|
|
|
|
.read_storage::<comp::CharacterState>()
|
|
|
|
.get(client.entity())
|
|
|
|
.map(|cs| cs.is_aimed())
|
|
|
|
.unwrap_or(false);
|
|
|
|
|
2020-05-19 13:41:08 +00:00
|
|
|
(
|
|
|
|
is_aiming,
|
|
|
|
if is_aiming {
|
|
|
|
Vec3::unit_z() * 0.025
|
|
|
|
} else {
|
|
|
|
Vec3::zero()
|
|
|
|
},
|
|
|
|
)
|
2020-05-19 13:26:53 +00:00
|
|
|
};
|
|
|
|
|
2019-08-24 17:58:28 +00:00
|
|
|
let cam_dir: Vec3<f32> = Vec3::from(view_mat.inverted() * -Vec4::unit_z());
|
|
|
|
|
2019-09-26 10:43:03 +00:00
|
|
|
// Check to see whether we're aiming at anything
|
|
|
|
let (build_pos, select_pos) = {
|
|
|
|
let client = self.client.borrow();
|
|
|
|
let terrain = client.state().terrain();
|
2020-03-10 20:50:04 +00:00
|
|
|
|
|
|
|
let cam_ray = terrain
|
2019-09-26 10:43:03 +00:00
|
|
|
.ray(cam_pos, cam_pos + cam_dir * 100.0)
|
2019-09-26 13:03:41 +00:00
|
|
|
.until(|block| block.is_tangible())
|
2019-09-26 10:43:03 +00:00
|
|
|
.cast();
|
2020-03-10 20:50:04 +00:00
|
|
|
|
|
|
|
let cam_dist = cam_ray.0;
|
|
|
|
|
2020-05-15 16:17:10 +00:00
|
|
|
match cam_ray.1 {
|
|
|
|
Ok(Some(_))
|
|
|
|
if player_pos.distance_squared(cam_pos + cam_dir * cam_dist)
|
|
|
|
<= MAX_PICKUP_RANGE_SQR =>
|
2020-03-10 20:50:04 +00:00
|
|
|
{
|
2020-05-15 16:17:10 +00:00
|
|
|
(
|
|
|
|
Some((cam_pos + cam_dir * (cam_dist - 0.01)).map(|e| e.floor() as i32)),
|
2020-07-06 21:41:21 +00:00
|
|
|
Some((cam_pos + cam_dir * (cam_dist + 0.01)).map(|e| e.floor() as i32)),
|
2020-05-15 16:17:10 +00:00
|
|
|
)
|
|
|
|
},
|
|
|
|
_ => (None, None),
|
2019-09-26 10:43:03 +00:00
|
|
|
}
|
|
|
|
};
|
2020-03-10 20:50:04 +00:00
|
|
|
|
2020-07-06 22:37:44 +00:00
|
|
|
let can_build = self
|
|
|
|
.client
|
2020-07-06 22:04:13 +00:00
|
|
|
.borrow()
|
|
|
|
.state()
|
|
|
|
.read_storage::<comp::CanBuild>()
|
|
|
|
.get(self.client.borrow().entity())
|
|
|
|
.is_some();
|
|
|
|
|
2020-01-30 02:49:27 +00:00
|
|
|
// Only highlight collectables
|
|
|
|
self.scene.set_select_pos(select_pos.filter(|sp| {
|
|
|
|
self.client
|
|
|
|
.borrow()
|
|
|
|
.state()
|
|
|
|
.terrain()
|
|
|
|
.get(*sp)
|
2020-07-06 22:04:13 +00:00
|
|
|
.map(|b| b.is_collectible() || can_build)
|
2020-01-30 02:49:27 +00:00
|
|
|
.unwrap_or(false)
|
|
|
|
}));
|
2019-09-26 10:43:03 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Handle window events.
|
2020-06-05 20:53:15 +00:00
|
|
|
for event in global_state.window.fetch_events(&mut global_state.settings) {
|
2019-05-17 09:22:32 +00:00
|
|
|
// Pass all events to the ui first.
|
2019-04-20 18:08:39 +00:00
|
|
|
if self.hud.handle_event(event.clone(), global_state) {
|
2019-03-22 03:55:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
2019-05-23 15:14:39 +00:00
|
|
|
|
|
|
|
match event {
|
2019-04-17 15:22:26 +00:00
|
|
|
Event::Close => {
|
|
|
|
return PlayStateResult::Shutdown;
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-08-30 22:13:45 +00:00
|
|
|
Event::InputUpdate(GameInput::Primary, state) => {
|
2020-07-06 22:04:13 +00:00
|
|
|
// If we can build, use LMB to break blocks, if not, use it to attack
|
2019-07-04 18:14:14 +00:00
|
|
|
let mut client = self.client.borrow_mut();
|
2020-07-06 22:04:13 +00:00
|
|
|
if state && can_build {
|
2020-05-15 16:17:10 +00:00
|
|
|
if let Some(select_pos) = select_pos {
|
|
|
|
client.remove_block(select_pos);
|
2019-07-03 17:55:56 +00:00
|
|
|
}
|
2019-07-03 20:28:13 +00:00
|
|
|
} else {
|
2019-11-29 15:20:35 +00:00
|
|
|
self.inputs.primary.set_state(state);
|
2019-07-03 17:55:56 +00:00
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-07-04 18:14:14 +00:00
|
|
|
|
2019-08-30 22:13:45 +00:00
|
|
|
Event::InputUpdate(GameInput::Secondary, state) => {
|
2019-11-29 15:20:35 +00:00
|
|
|
self.inputs.secondary.set_state(false); // To be changed later on
|
2019-09-25 21:17:43 +00:00
|
|
|
|
2019-08-24 16:38:59 +00:00
|
|
|
let mut client = self.client.borrow_mut();
|
2019-09-25 21:17:43 +00:00
|
|
|
|
2020-07-06 22:04:13 +00:00
|
|
|
if state && can_build {
|
2020-05-15 16:17:10 +00:00
|
|
|
if let Some(build_pos) = build_pos {
|
|
|
|
client.place_block(build_pos, self.selected_block);
|
2019-06-30 20:25:37 +00:00
|
|
|
}
|
2019-09-25 21:17:43 +00:00
|
|
|
} else {
|
2020-02-24 14:35:07 +00:00
|
|
|
self.inputs.secondary.set_state(state);
|
2019-06-30 20:25:37 +00:00
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-03-24 12:59:53 +00:00
|
|
|
|
2019-06-11 04:08:55 +00:00
|
|
|
Event::InputUpdate(GameInput::Roll, state) => {
|
2019-07-04 19:59:57 +00:00
|
|
|
let client = self.client.borrow();
|
2020-07-06 22:04:13 +00:00
|
|
|
if can_build {
|
2019-07-04 19:59:57 +00:00
|
|
|
if state {
|
2019-09-26 10:43:03 +00:00
|
|
|
if let Some(block) = select_pos
|
|
|
|
.and_then(|sp| client.state().terrain().get(sp).ok().copied())
|
2019-07-07 04:37:22 +00:00
|
|
|
{
|
2019-09-26 10:43:03 +00:00
|
|
|
self.selected_block = block;
|
2019-07-04 19:59:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2019-11-29 15:20:35 +00:00
|
|
|
self.inputs.roll.set_state(state);
|
2019-07-04 19:59:57 +00:00
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-03-24 07:38:16 +00:00
|
|
|
Event::InputUpdate(GameInput::Respawn, state)
|
|
|
|
if state != self.key_state.respawn =>
|
|
|
|
{
|
|
|
|
self.key_state.respawn = state;
|
|
|
|
if state {
|
|
|
|
self.client.borrow_mut().respawn();
|
|
|
|
}
|
|
|
|
}
|
2019-06-09 14:20:20 +00:00
|
|
|
Event::InputUpdate(GameInput::Jump, state) => {
|
2019-11-29 15:20:35 +00:00
|
|
|
self.inputs.jump.set_state(state);
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-07-04 19:22:55 +00:00
|
|
|
Event::InputUpdate(GameInput::Swim, state) => {
|
|
|
|
self.inputs.swim.set_state(state);
|
|
|
|
},
|
2020-03-24 07:38:16 +00:00
|
|
|
Event::InputUpdate(GameInput::Sit, state)
|
|
|
|
if state != self.key_state.toggle_sit =>
|
|
|
|
{
|
|
|
|
self.key_state.toggle_sit = state;
|
|
|
|
if state {
|
2020-06-06 18:09:01 +00:00
|
|
|
stop_auto_walk(&mut auto_walk, &mut self.key_state, &mut self.hud);
|
2020-03-24 07:38:16 +00:00
|
|
|
self.client.borrow_mut().toggle_sit();
|
|
|
|
}
|
|
|
|
}
|
2020-05-27 06:41:55 +00:00
|
|
|
Event::InputUpdate(GameInput::Dance, state)
|
|
|
|
if state != self.key_state.toggle_dance =>
|
|
|
|
{
|
|
|
|
self.key_state.toggle_dance = state;
|
|
|
|
if state {
|
2020-06-06 18:09:01 +00:00
|
|
|
stop_auto_walk(&mut auto_walk, &mut self.key_state, &mut self.hud);
|
2020-05-27 06:41:55 +00:00
|
|
|
self.client.borrow_mut().toggle_dance();
|
|
|
|
}
|
|
|
|
}
|
2020-06-06 18:09:01 +00:00
|
|
|
Event::InputUpdate(GameInput::MoveForward, state) => {
|
|
|
|
if state && global_state.settings.gameplay.stop_auto_walk_on_input {
|
|
|
|
stop_auto_walk(&mut auto_walk, &mut self.key_state, &mut self.hud);
|
|
|
|
}
|
|
|
|
self.key_state.up = state
|
|
|
|
},
|
|
|
|
Event::InputUpdate(GameInput::MoveBack, state) => {
|
|
|
|
if state && global_state.settings.gameplay.stop_auto_walk_on_input {
|
|
|
|
stop_auto_walk(&mut auto_walk, &mut self.key_state, &mut self.hud);
|
|
|
|
}
|
|
|
|
self.key_state.down = state
|
|
|
|
},
|
|
|
|
Event::InputUpdate(GameInput::MoveLeft, state) => {
|
|
|
|
if state && global_state.settings.gameplay.stop_auto_walk_on_input {
|
|
|
|
stop_auto_walk(&mut auto_walk, &mut self.key_state, &mut self.hud);
|
|
|
|
}
|
|
|
|
self.key_state.left = state
|
|
|
|
},
|
|
|
|
Event::InputUpdate(GameInput::MoveRight, state) => {
|
|
|
|
if state && global_state.settings.gameplay.stop_auto_walk_on_input {
|
|
|
|
stop_auto_walk(&mut auto_walk, &mut self.key_state, &mut self.hud);
|
|
|
|
}
|
|
|
|
self.key_state.right = state
|
|
|
|
},
|
2020-06-16 21:32:39 +00:00
|
|
|
Event::InputUpdate(GameInput::Glide, state)
|
|
|
|
if state != self.key_state.toggle_glide =>
|
|
|
|
{
|
|
|
|
self.key_state.toggle_glide = state;
|
|
|
|
if state {
|
|
|
|
self.client.borrow_mut().toggle_glide();
|
|
|
|
}
|
|
|
|
}
|
2019-11-29 15:20:35 +00:00
|
|
|
Event::InputUpdate(GameInput::Climb, state) => {
|
2020-03-24 07:38:16 +00:00
|
|
|
self.key_state.climb_up = state;
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-09-09 19:11:40 +00:00
|
|
|
Event::InputUpdate(GameInput::ClimbDown, state) => {
|
2020-03-24 07:38:16 +00:00
|
|
|
self.key_state.climb_down = state;
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-04-08 23:23:51 +00:00
|
|
|
/*Event::InputUpdate(GameInput::WallLeap, state) => {
|
2019-11-29 15:20:35 +00:00
|
|
|
self.inputs.wall_leap.set_state(state)
|
2020-04-08 23:23:51 +00:00
|
|
|
},*/
|
2020-03-24 07:38:16 +00:00
|
|
|
Event::InputUpdate(GameInput::ToggleWield, state)
|
|
|
|
if state != self.key_state.toggle_wield =>
|
|
|
|
{
|
|
|
|
self.key_state.toggle_wield = state;
|
|
|
|
if state {
|
|
|
|
self.client.borrow_mut().toggle_wield();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Event::InputUpdate(GameInput::SwapLoadout, state)
|
|
|
|
if state != self.key_state.swap_loadout =>
|
|
|
|
{
|
|
|
|
self.key_state.swap_loadout = state;
|
|
|
|
if state {
|
|
|
|
self.client.borrow_mut().swap_loadout();
|
|
|
|
}
|
|
|
|
}
|
2020-05-04 15:15:31 +00:00
|
|
|
Event::InputUpdate(GameInput::ToggleLantern, true) => {
|
|
|
|
self.client.borrow_mut().toggle_lantern();
|
|
|
|
},
|
2019-09-09 19:11:40 +00:00
|
|
|
Event::InputUpdate(GameInput::Mount, true) => {
|
2019-10-15 04:06:14 +00:00
|
|
|
let mut client = self.client.borrow_mut();
|
2019-09-09 19:11:40 +00:00
|
|
|
if client.is_mounted() {
|
2019-10-15 04:06:14 +00:00
|
|
|
client.unmount();
|
2019-09-09 19:11:40 +00:00
|
|
|
} else {
|
|
|
|
let player_pos = client
|
|
|
|
.state()
|
|
|
|
.read_storage::<comp::Pos>()
|
|
|
|
.get(client.entity())
|
|
|
|
.copied();
|
|
|
|
if let Some(player_pos) = player_pos {
|
|
|
|
// Find closest mountable entity
|
|
|
|
let closest_mountable = (
|
|
|
|
&client.state().ecs().entities(),
|
|
|
|
&client.state().ecs().read_storage::<comp::Pos>(),
|
|
|
|
&client.state().ecs().read_storage::<comp::MountState>(),
|
|
|
|
)
|
|
|
|
.join()
|
|
|
|
.filter(|(_, _, ms)| {
|
|
|
|
if let comp::MountState::Unmounted = ms {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.min_by_key(|(_, pos, _)| {
|
|
|
|
(player_pos.0.distance_squared(pos.0) * 1000.0) as i32
|
|
|
|
})
|
2019-10-15 04:06:14 +00:00
|
|
|
.map(|(uid, _, _)| uid);
|
2019-09-09 19:11:40 +00:00
|
|
|
|
2019-10-15 04:06:14 +00:00
|
|
|
if let Some(mountee_entity) = closest_mountable {
|
|
|
|
client.mount(mountee_entity);
|
2019-09-09 19:11:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-07-29 16:19:08 +00:00
|
|
|
Event::InputUpdate(GameInput::Interact, state) => {
|
|
|
|
let mut client = self.client.borrow_mut();
|
|
|
|
|
2020-05-23 14:11:42 +00:00
|
|
|
// Collect terrain sprites
|
|
|
|
if let Some(select_pos) = self.scene.select_pos() {
|
|
|
|
client.collect_block(select_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect lootable entities
|
2019-07-29 16:19:08 +00:00
|
|
|
let player_pos = client
|
|
|
|
.state()
|
|
|
|
.read_storage::<comp::Pos>()
|
|
|
|
.get(client.entity())
|
|
|
|
.copied();
|
|
|
|
|
|
|
|
if let (Some(player_pos), true) = (player_pos, state) {
|
|
|
|
let entity = (
|
|
|
|
&client.state().ecs().entities(),
|
|
|
|
&client.state().ecs().read_storage::<comp::Pos>(),
|
|
|
|
&client.state().ecs().read_storage::<comp::Item>(),
|
|
|
|
)
|
|
|
|
.join()
|
|
|
|
.filter(|(_, pos, _)| {
|
2020-03-12 02:54:23 +00:00
|
|
|
pos.0.distance_squared(player_pos.0) < MAX_PICKUP_RANGE_SQR
|
2019-07-29 16:19:08 +00:00
|
|
|
})
|
|
|
|
.min_by_key(|(_, pos, _)| {
|
|
|
|
(pos.0.distance_squared(player_pos.0) * 1000.0) as i32
|
|
|
|
})
|
|
|
|
.map(|(entity, _, _)| entity);
|
|
|
|
|
|
|
|
if let Some(entity) = entity {
|
|
|
|
client.pick_up(entity);
|
|
|
|
}
|
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-04-08 23:23:51 +00:00
|
|
|
/*Event::InputUpdate(GameInput::Charge, state) => {
|
2019-12-03 06:30:08 +00:00
|
|
|
self.inputs.charge.set_state(state);
|
2020-04-08 23:23:51 +00:00
|
|
|
},*/
|
2020-03-27 18:35:52 +00:00
|
|
|
Event::InputUpdate(GameInput::FreeLook, state) => {
|
|
|
|
match (global_state.settings.gameplay.free_look_behavior, state) {
|
|
|
|
(PressBehavior::Toggle, true) => {
|
|
|
|
free_look = !free_look;
|
|
|
|
self.hud.free_look(free_look);
|
|
|
|
},
|
|
|
|
(PressBehavior::Hold, state) => {
|
|
|
|
free_look = state;
|
|
|
|
self.hud.free_look(free_look);
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
};
|
2020-03-26 21:22:21 +00:00
|
|
|
},
|
2020-06-06 18:09:01 +00:00
|
|
|
Event::InputUpdate(GameInput::AutoWalk, state) => {
|
|
|
|
match (global_state.settings.gameplay.auto_walk_behavior, state) {
|
|
|
|
(PressBehavior::Toggle, true) => {
|
|
|
|
auto_walk = !auto_walk;
|
|
|
|
self.key_state.auto_walk = auto_walk;
|
|
|
|
self.hud.auto_walk(auto_walk);
|
|
|
|
},
|
|
|
|
(PressBehavior::Hold, state) => {
|
|
|
|
auto_walk = state;
|
|
|
|
self.key_state.auto_walk = auto_walk;
|
|
|
|
self.hud.auto_walk(auto_walk);
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
},
|
2020-07-09 17:12:11 +00:00
|
|
|
Event::InputUpdate(GameInput::CycleCamera, true) => {
|
2020-07-09 21:01:48 +00:00
|
|
|
// Prevent accessing camera modes which aren't available in multiplayer
|
|
|
|
// unless you are an admin. This is an easily bypassed clientside check.
|
|
|
|
// The server should do its own filtering of which entities are sent to
|
|
|
|
// clients to prevent abuse.
|
2020-07-09 17:12:11 +00:00
|
|
|
let camera = self.scene.camera_mut();
|
2020-07-09 21:01:48 +00:00
|
|
|
camera.next_mode(self.client.borrow().is_admin());
|
2020-07-09 17:12:11 +00:00
|
|
|
},
|
2020-03-10 21:00:13 +00:00
|
|
|
Event::AnalogGameInput(input) => match input {
|
|
|
|
AnalogGameInput::MovementX(v) => {
|
|
|
|
self.key_state.analog_matrix.x = v;
|
|
|
|
},
|
|
|
|
AnalogGameInput::MovementY(v) => {
|
|
|
|
self.key_state.analog_matrix.y = v;
|
|
|
|
},
|
|
|
|
other => {
|
|
|
|
self.scene.handle_input_event(Event::AnalogGameInput(other));
|
|
|
|
},
|
|
|
|
},
|
2020-06-02 02:42:26 +00:00
|
|
|
Event::ScreenshotMessage(screenshot_message) => {
|
|
|
|
self.hud.new_message(comp::ChatMsg {
|
2020-06-12 07:43:20 +00:00
|
|
|
chat_type: comp::ChatType::CommandInfo,
|
2020-06-02 02:42:26 +00:00
|
|
|
message: screenshot_message,
|
|
|
|
})
|
|
|
|
},
|
2019-05-25 14:39:27 +00:00
|
|
|
|
2019-01-12 13:56:34 +00:00
|
|
|
// Pass all other events to the scene
|
2019-04-02 01:05:18 +00:00
|
|
|
event => {
|
|
|
|
self.scene.handle_input_event(event);
|
2020-02-01 20:39:39 +00:00
|
|
|
}, // TODO: Do something if the event wasn't handled?
|
2019-05-23 15:14:39 +00:00
|
|
|
}
|
2019-01-11 23:18:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 21:22:21 +00:00
|
|
|
if !free_look {
|
2020-07-09 17:12:11 +00:00
|
|
|
walk_forward_dir = self.scene.camera().forward_xy();
|
|
|
|
walk_right_dir = self.scene.camera().right_xy();
|
2020-05-19 13:26:53 +00:00
|
|
|
self.inputs.look_dir = Dir::from_unnormalized(cam_dir + aim_dir_offset).unwrap();
|
2020-03-26 21:22:21 +00:00
|
|
|
}
|
2020-07-09 17:12:11 +00:00
|
|
|
|
|
|
|
// Get the current state of movement related inputs
|
|
|
|
let input_vec = self.key_state.dir_vec();
|
|
|
|
let (axis_right, axis_up) = (input_vec[0], input_vec[1]);
|
|
|
|
|
|
|
|
match self.scene.camera().get_mode() {
|
|
|
|
camera::CameraMode::FirstPerson | camera::CameraMode::ThirdPerson => {
|
|
|
|
// Move the player character based on their walking direction.
|
|
|
|
// This could be different from the camera direction if free look is enabled.
|
|
|
|
self.inputs.move_dir = walk_right_dir * axis_right + walk_forward_dir * axis_up;
|
|
|
|
freefly_vel = Vec3::zero();
|
|
|
|
},
|
|
|
|
|
|
|
|
camera::CameraMode::Freefly => {
|
|
|
|
// Move the camera freely in 3d space. Apply acceleration so that
|
|
|
|
// the movement feels more natural and controlled.
|
|
|
|
const FREEFLY_ACCEL: f32 = 120.0;
|
|
|
|
const FREEFLY_DAMPING: f32 = 80.0;
|
|
|
|
const FREEFLY_MAX_SPEED: f32 = 50.0;
|
|
|
|
|
|
|
|
let forward = self.scene.camera().forward();
|
|
|
|
let right = self.scene.camera().right();
|
|
|
|
let dir = right * axis_right + forward * axis_up;
|
|
|
|
|
|
|
|
let dt = clock.get_last_delta().as_secs_f32();
|
|
|
|
if freefly_vel.magnitude_squared() > 0.01 {
|
|
|
|
let new_vel =
|
|
|
|
freefly_vel - freefly_vel.normalized() * (FREEFLY_DAMPING * dt);
|
|
|
|
if freefly_vel.dot(new_vel) > 0.0 {
|
|
|
|
freefly_vel = new_vel;
|
|
|
|
} else {
|
|
|
|
freefly_vel = Vec3::zero();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if dir.magnitude_squared() > 0.01 {
|
|
|
|
freefly_vel += dir * (FREEFLY_ACCEL * dt);
|
|
|
|
if freefly_vel.magnitude() > FREEFLY_MAX_SPEED {
|
|
|
|
freefly_vel = freefly_vel.normalized() * FREEFLY_MAX_SPEED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let pos = self.scene.camera().get_focus_pos();
|
|
|
|
self.scene
|
|
|
|
.camera_mut()
|
|
|
|
.set_focus_pos(pos + freefly_vel * dt);
|
|
|
|
|
|
|
|
// Do not apply any movement to the player character
|
|
|
|
self.inputs.move_dir = Vec2::zero();
|
|
|
|
},
|
|
|
|
};
|
2019-06-09 14:20:20 +00:00
|
|
|
|
2020-03-24 07:38:16 +00:00
|
|
|
self.inputs.climb = self.key_state.climb();
|
|
|
|
|
2020-03-03 19:51:15 +00:00
|
|
|
// Runs if either in a multiplayer server or the singleplayer server is unpaused
|
|
|
|
if global_state.singleplayer.is_none()
|
|
|
|
|| !global_state.singleplayer.as_ref().unwrap().is_paused()
|
|
|
|
{
|
2020-03-01 22:18:22 +00:00
|
|
|
// Perform an in-game tick.
|
2020-06-25 11:20:09 +00:00
|
|
|
match self.tick(clock.get_avg_delta(), global_state) {
|
2020-03-07 22:14:21 +00:00
|
|
|
Ok(TickAction::Continue) => {}, // Do nothing
|
|
|
|
Ok(TickAction::Disconnect) => return PlayStateResult::Pop, // Go to main menu
|
|
|
|
Err(err) => {
|
|
|
|
global_state.info_message =
|
|
|
|
Some(localized_strings.get("common.connection_lost").to_owned());
|
|
|
|
error!("[session] Failed to tick the scene: {:?}", err);
|
2020-03-01 22:18:22 +00:00
|
|
|
|
2020-03-07 22:14:21 +00:00
|
|
|
return PlayStateResult::Pop;
|
|
|
|
},
|
2020-03-01 22:18:22 +00:00
|
|
|
}
|
2019-05-26 09:21:51 +00:00
|
|
|
}
|
2019-01-11 23:18:34 +00:00
|
|
|
|
2019-06-05 15:57:48 +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
|
|
|
|
2020-02-29 03:59:11 +00:00
|
|
|
// Recompute dependents just in case some input modified the camera
|
|
|
|
self.scene
|
|
|
|
.camera_mut()
|
|
|
|
.compute_dependents(&*self.client.borrow().state().terrain());
|
2019-06-05 15:57:48 +00:00
|
|
|
// Extract HUD events ensuring the client borrow gets dropped.
|
2020-01-17 23:43:18 +00:00
|
|
|
let mut hud_events = self.hud.maintain(
|
2019-05-14 06:43:07 +00:00
|
|
|
&self.client.borrow(),
|
2019-05-23 08:18:25 +00:00
|
|
|
global_state,
|
2019-05-23 09:30:46 +00:00
|
|
|
DebugInfo {
|
|
|
|
tps: clock.get_tps(),
|
2020-07-10 20:25:45 +00:00
|
|
|
ping_ms: self.client.borrow().get_ping_ms_rolling_avg(),
|
2019-05-28 18:23:24 +00:00
|
|
|
coordinates: self
|
|
|
|
.client
|
|
|
|
.borrow()
|
|
|
|
.state()
|
|
|
|
.ecs()
|
|
|
|
.read_storage::<Pos>()
|
|
|
|
.get(self.client.borrow().entity())
|
2019-05-28 19:40:50 +00:00
|
|
|
.cloned(),
|
2019-08-06 21:51:13 +00:00
|
|
|
velocity: self
|
|
|
|
.client
|
|
|
|
.borrow()
|
|
|
|
.state()
|
|
|
|
.ecs()
|
|
|
|
.read_storage::<Vel>()
|
|
|
|
.get(self.client.borrow().entity())
|
|
|
|
.cloned(),
|
2020-04-25 00:30:20 +00:00
|
|
|
ori: self
|
|
|
|
.client
|
|
|
|
.borrow()
|
|
|
|
.state()
|
|
|
|
.ecs()
|
|
|
|
.read_storage::<comp::Ori>()
|
|
|
|
.get(self.client.borrow().entity())
|
|
|
|
.cloned(),
|
2019-11-19 15:13:33 +00:00
|
|
|
num_chunks: self.scene.terrain().chunk_count() as u32,
|
|
|
|
num_visible_chunks: self.scene.terrain().visible_chunk_count() as u32,
|
|
|
|
num_figures: self.scene.figure_mgr().figure_count() as u32,
|
|
|
|
num_figures_visible: self.scene.figure_mgr().figure_count_visible() as u32,
|
2019-05-23 09:30:46 +00:00
|
|
|
},
|
2019-05-20 06:09:20 +00:00
|
|
|
&self.scene.camera(),
|
2019-12-30 12:16:35 +00:00
|
|
|
clock.get_last_delta(),
|
2020-05-18 22:40:28 +00:00
|
|
|
HudInfo {
|
2020-05-19 13:26:53 +00:00
|
|
|
is_aiming,
|
2020-05-18 23:35:58 +00:00
|
|
|
is_first_person: matches!(
|
|
|
|
self.scene.camera().get_mode(),
|
|
|
|
camera::CameraMode::FirstPerson
|
|
|
|
),
|
2020-05-18 22:40:28 +00:00
|
|
|
},
|
2019-05-23 09:30:46 +00:00
|
|
|
);
|
2019-06-05 15:57:48 +00:00
|
|
|
|
2020-01-17 23:43:18 +00:00
|
|
|
// Look for changes in the localization files
|
|
|
|
if localization_watcher.reloaded() {
|
|
|
|
hud_events.push(HudEvent::ChangeLanguage(localized_strings.metadata.clone()));
|
|
|
|
}
|
|
|
|
|
2019-05-23 09:30:46 +00:00
|
|
|
// Maintain the UI.
|
|
|
|
for event in hud_events {
|
2019-03-16 02:03:21 +00:00
|
|
|
match event {
|
2019-03-17 05:26:51 +00:00
|
|
|
HudEvent::SendMessage(msg) => {
|
|
|
|
// TODO: Handle result
|
2019-04-04 14:45:57 +00:00
|
|
|
self.client.borrow_mut().send_chat(msg);
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-06-02 02:17:36 +00:00
|
|
|
HudEvent::CharacterSelection => {
|
|
|
|
self.client.borrow_mut().request_remove_character()
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-06-02 02:17:36 +00:00
|
|
|
HudEvent::Logout => self.client.borrow_mut().request_logout(),
|
2019-04-17 15:22:26 +00:00
|
|
|
HudEvent::Quit => {
|
|
|
|
return PlayStateResult::Shutdown;
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-06-05 15:57:48 +00:00
|
|
|
HudEvent::AdjustMousePan(sensitivity) => {
|
2019-06-06 17:42:13 +00:00
|
|
|
global_state.window.pan_sensitivity = sensitivity;
|
|
|
|
global_state.settings.gameplay.pan_sensitivity = sensitivity;
|
2019-07-26 02:28:53 +00:00
|
|
|
global_state.settings.save_to_file_warn();
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-06-05 15:57:48 +00:00
|
|
|
HudEvent::AdjustMouseZoom(sensitivity) => {
|
2019-06-06 17:42:13 +00:00
|
|
|
global_state.window.zoom_sensitivity = sensitivity;
|
|
|
|
global_state.settings.gameplay.zoom_sensitivity = sensitivity;
|
2019-07-26 02:28:53 +00:00
|
|
|
global_state.settings.save_to_file_warn();
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-10-02 13:09:05 +00:00
|
|
|
HudEvent::ToggleZoomInvert(zoom_inverted) => {
|
|
|
|
global_state.window.zoom_inversion = zoom_inverted;
|
|
|
|
global_state.settings.gameplay.zoom_inversion = zoom_inverted;
|
|
|
|
global_state.settings.save_to_file_warn();
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-01-10 00:33:38 +00:00
|
|
|
HudEvent::Sct(sct) => {
|
|
|
|
global_state.settings.gameplay.sct = sct;
|
|
|
|
global_state.settings.save_to_file_warn();
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-01-10 00:33:38 +00:00
|
|
|
HudEvent::SctPlayerBatch(sct_player_batch) => {
|
|
|
|
global_state.settings.gameplay.sct_player_batch = sct_player_batch;
|
|
|
|
global_state.settings.save_to_file_warn();
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-01-10 00:33:38 +00:00
|
|
|
HudEvent::SctDamageBatch(sct_damage_batch) => {
|
|
|
|
global_state.settings.gameplay.sct_damage_batch = sct_damage_batch;
|
|
|
|
global_state.settings.save_to_file_warn();
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-05-25 01:29:47 +00:00
|
|
|
HudEvent::SpeechBubbleDarkMode(sbdm) => {
|
|
|
|
global_state.settings.gameplay.speech_bubble_dark_mode = sbdm;
|
|
|
|
global_state.settings.save_to_file_warn();
|
|
|
|
},
|
2020-06-14 03:13:01 +00:00
|
|
|
HudEvent::SpeechBubbleIcon(sbi) => {
|
|
|
|
global_state.settings.gameplay.speech_bubble_icon = sbi;
|
|
|
|
global_state.settings.save_to_file_warn();
|
|
|
|
},
|
2020-01-10 00:33:38 +00:00
|
|
|
HudEvent::ToggleDebug(toggle_debug) => {
|
|
|
|
global_state.settings.gameplay.toggle_debug = toggle_debug;
|
|
|
|
global_state.settings.save_to_file_warn();
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-12-06 22:49:17 +00:00
|
|
|
HudEvent::ToggleMouseYInvert(mouse_y_inverted) => {
|
|
|
|
global_state.window.mouse_y_inversion = mouse_y_inverted;
|
|
|
|
global_state.settings.gameplay.mouse_y_inversion = mouse_y_inverted;
|
|
|
|
global_state.settings.save_to_file_warn();
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-04-23 22:59:34 +00:00
|
|
|
HudEvent::ToggleSmoothPan(smooth_pan_enabled) => {
|
|
|
|
global_state.settings.gameplay.smooth_pan_enable = smooth_pan_enabled;
|
|
|
|
global_state.settings.save_to_file_warn();
|
|
|
|
},
|
2019-05-19 00:45:02 +00:00
|
|
|
HudEvent::AdjustViewDistance(view_distance) => {
|
2019-05-20 20:18:01 +00:00
|
|
|
self.client.borrow_mut().set_view_distance(view_distance);
|
|
|
|
|
|
|
|
global_state.settings.graphics.view_distance = view_distance;
|
2019-07-26 02:28:53 +00:00
|
|
|
global_state.settings.save_to_file_warn();
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-04-25 13:04:30 +00:00
|
|
|
HudEvent::AdjustSpriteRenderDistance(sprite_render_distance) => {
|
2020-04-25 13:37:08 +00:00
|
|
|
global_state.settings.graphics.sprite_render_distance =
|
|
|
|
sprite_render_distance;
|
2020-04-25 13:04:30 +00:00
|
|
|
global_state.settings.save_to_file_warn();
|
|
|
|
},
|
2020-04-26 01:44:56 +00:00
|
|
|
HudEvent::AdjustFigureLoDRenderDistance(figure_lod_render_distance) => {
|
|
|
|
global_state.settings.graphics.figure_lod_render_distance =
|
|
|
|
figure_lod_render_distance;
|
|
|
|
global_state.settings.save_to_file_warn();
|
|
|
|
},
|
2019-07-07 00:15:22 +00:00
|
|
|
HudEvent::CrosshairTransp(crosshair_transp) => {
|
|
|
|
global_state.settings.gameplay.crosshair_transp = crosshair_transp;
|
2019-07-26 02:28:53 +00:00
|
|
|
global_state.settings.save_to_file_warn();
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-10-23 19:40:45 +00:00
|
|
|
HudEvent::ChatTransp(chat_transp) => {
|
|
|
|
global_state.settings.gameplay.chat_transp = chat_transp;
|
|
|
|
global_state.settings.save_to_file_warn();
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-06-24 05:46:29 +00:00
|
|
|
HudEvent::ChatCharName(chat_char_name) => {
|
|
|
|
global_state.settings.gameplay.chat_character_name = chat_char_name;
|
|
|
|
global_state.settings.save_to_file_warn();
|
|
|
|
},
|
2019-07-23 01:02:57 +00:00
|
|
|
HudEvent::CrosshairType(crosshair_type) => {
|
|
|
|
global_state.settings.gameplay.crosshair_type = crosshair_type;
|
2019-07-26 02:28:53 +00:00
|
|
|
global_state.settings.save_to_file_warn();
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-10-23 19:40:45 +00:00
|
|
|
HudEvent::Intro(intro_show) => {
|
|
|
|
global_state.settings.gameplay.intro_show = intro_show;
|
|
|
|
global_state.settings.save_to_file_warn();
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-08-18 18:07:21 +00:00
|
|
|
HudEvent::ToggleXpBar(xp_bar) => {
|
|
|
|
global_state.settings.gameplay.xp_bar = xp_bar;
|
|
|
|
global_state.settings.save_to_file_warn();
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-08-18 18:07:21 +00:00
|
|
|
HudEvent::ToggleBarNumbers(bar_numbers) => {
|
|
|
|
global_state.settings.gameplay.bar_numbers = bar_numbers;
|
|
|
|
global_state.settings.save_to_file_warn();
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-08-18 18:07:21 +00:00
|
|
|
HudEvent::ToggleShortcutNumbers(shortcut_numbers) => {
|
|
|
|
global_state.settings.gameplay.shortcut_numbers = shortcut_numbers;
|
|
|
|
global_state.settings.save_to_file_warn();
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-07-26 02:28:53 +00:00
|
|
|
HudEvent::UiScale(scale_change) => {
|
|
|
|
global_state.settings.gameplay.ui_scale =
|
|
|
|
self.hud.scale_change(scale_change);
|
|
|
|
global_state.settings.save_to_file_warn();
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-09-24 16:18:09 +00:00
|
|
|
HudEvent::AdjustMusicVolume(music_volume) => {
|
|
|
|
global_state.audio.set_music_volume(music_volume);
|
2019-07-23 01:02:57 +00:00
|
|
|
|
2019-09-24 16:18:09 +00:00
|
|
|
global_state.settings.audio.music_volume = music_volume;
|
|
|
|
global_state.settings.save_to_file_warn();
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-09-24 16:18:09 +00:00
|
|
|
HudEvent::AdjustSfxVolume(sfx_volume) => {
|
|
|
|
global_state.audio.set_sfx_volume(sfx_volume);
|
2019-05-20 20:18:01 +00:00
|
|
|
|
2019-09-24 16:18:09 +00:00
|
|
|
global_state.settings.audio.sfx_volume = sfx_volume;
|
2019-07-26 02:28:53 +00:00
|
|
|
global_state.settings.save_to_file_warn();
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-05-20 17:40:35 +00:00
|
|
|
HudEvent::ChangeAudioDevice(name) => {
|
2019-08-31 06:37:09 +00:00
|
|
|
global_state.audio.set_device(name.clone());
|
2019-05-20 20:18:01 +00:00
|
|
|
|
2020-03-31 18:12:49 +00:00
|
|
|
global_state.settings.audio.output = AudioOutput::Device(name);
|
2019-07-26 02:28:53 +00:00
|
|
|
global_state.settings.save_to_file_warn();
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-06-06 19:11:39 +00:00
|
|
|
HudEvent::ChangeMaxFPS(fps) => {
|
|
|
|
global_state.settings.graphics.max_fps = fps;
|
2019-07-26 02:28:53 +00:00
|
|
|
global_state.settings.save_to_file_warn();
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-04-10 02:36:35 +00:00
|
|
|
HudEvent::UseSlot(x) => self.client.borrow_mut().use_slot(x),
|
|
|
|
HudEvent::SwapSlots(a, b) => self.client.borrow_mut().swap_slots(a, b),
|
2020-06-06 02:37:15 +00:00
|
|
|
HudEvent::DropSlot(x) => {
|
|
|
|
let mut client = self.client.borrow_mut();
|
|
|
|
client.drop_slot(x);
|
|
|
|
if let comp::slot::Slot::Equip(equip_slot) = x {
|
|
|
|
if let comp::slot::EquipSlot::Lantern = equip_slot {
|
|
|
|
client.toggle_lantern();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-06-16 13:55:37 +00:00
|
|
|
HudEvent::ChangeHotbarState(state) => {
|
|
|
|
let client = self.client.borrow();
|
|
|
|
|
|
|
|
let server = &client.server_info.name;
|
|
|
|
// If we are changing the hotbar state this CANNOT be None.
|
|
|
|
let character_id = client.active_character_id.unwrap();
|
|
|
|
|
|
|
|
// Get or update the ServerProfile.
|
|
|
|
global_state
|
|
|
|
.profile
|
|
|
|
.set_hotbar_slots(server, character_id, state.slots);
|
|
|
|
|
|
|
|
global_state.profile.save_to_file_warn();
|
|
|
|
|
2020-06-21 10:22:26 +00:00
|
|
|
info!("Event! -> ChangedHotbarState")
|
2020-06-16 13:55:37 +00:00
|
|
|
},
|
2020-04-11 06:33:06 +00:00
|
|
|
HudEvent::Ability3(state) => self.inputs.ability3.set_state(state),
|
2019-08-05 16:37:52 +00:00
|
|
|
HudEvent::ChangeFOV(new_fov) => {
|
|
|
|
global_state.settings.graphics.fov = new_fov;
|
|
|
|
global_state.settings.save_to_file_warn();
|
2019-09-26 07:28:40 +00:00
|
|
|
self.scene.camera_mut().set_fov_deg(new_fov);
|
2020-02-29 03:59:11 +00:00
|
|
|
self.scene
|
|
|
|
.camera_mut()
|
|
|
|
.compute_dependents(&*self.client.borrow().state().terrain());
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-07-01 19:05:44 +00:00
|
|
|
HudEvent::MapZoom(map_zoom) => {
|
|
|
|
global_state.settings.gameplay.map_zoom = map_zoom;
|
|
|
|
global_state.settings.save_to_file_warn();
|
|
|
|
},
|
2020-02-12 13:55:26 +00:00
|
|
|
HudEvent::ChangeGamma(new_gamma) => {
|
|
|
|
global_state.settings.graphics.gamma = new_gamma;
|
|
|
|
global_state.settings.save_to_file_warn();
|
|
|
|
},
|
2019-09-26 07:28:40 +00:00
|
|
|
HudEvent::ChangeAaMode(new_aa_mode) => {
|
|
|
|
// Do this first so if it crashes the setting isn't saved :)
|
|
|
|
global_state
|
|
|
|
.window
|
|
|
|
.renderer_mut()
|
|
|
|
.set_aa_mode(new_aa_mode)
|
|
|
|
.unwrap();
|
|
|
|
global_state.settings.graphics.aa_mode = new_aa_mode;
|
|
|
|
global_state.settings.save_to_file_warn();
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-01-19 03:58:25 +00:00
|
|
|
HudEvent::ChangeCloudMode(new_cloud_mode) => {
|
|
|
|
// Do this first so if it crashes the setting isn't saved :)
|
|
|
|
global_state
|
|
|
|
.window
|
|
|
|
.renderer_mut()
|
|
|
|
.set_cloud_mode(new_cloud_mode)
|
|
|
|
.unwrap();
|
|
|
|
global_state.settings.graphics.cloud_mode = new_cloud_mode;
|
|
|
|
global_state.settings.save_to_file_warn();
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-01-19 03:58:25 +00:00
|
|
|
HudEvent::ChangeFluidMode(new_fluid_mode) => {
|
|
|
|
// Do this first so if it crashes the setting isn't saved :)
|
|
|
|
global_state
|
|
|
|
.window
|
|
|
|
.renderer_mut()
|
|
|
|
.set_fluid_mode(new_fluid_mode)
|
|
|
|
.unwrap();
|
|
|
|
global_state.settings.graphics.fluid_mode = new_fluid_mode;
|
|
|
|
global_state.settings.save_to_file_warn();
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-01-17 23:43:18 +00:00
|
|
|
HudEvent::ChangeLanguage(new_language) => {
|
|
|
|
global_state.settings.language.selected_language =
|
|
|
|
new_language.language_identifier;
|
|
|
|
localized_strings = load_watched::<VoxygenLocalization>(
|
|
|
|
&i18n_asset_key(&global_state.settings.language.selected_language),
|
|
|
|
&mut localization_watcher,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
localized_strings.log_missing_entries();
|
2020-01-26 19:29:46 +00:00
|
|
|
self.hud.update_language(localized_strings.clone());
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-01-02 03:48:11 +00:00
|
|
|
HudEvent::ToggleFullscreen => {
|
|
|
|
global_state
|
|
|
|
.window
|
|
|
|
.toggle_fullscreen(&mut global_state.settings);
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-01-02 03:48:11 +00:00
|
|
|
HudEvent::AdjustWindowSize(new_size) => {
|
|
|
|
global_state.window.set_size(new_size.into());
|
|
|
|
global_state.settings.graphics.window_size = new_size;
|
|
|
|
global_state.settings.save_to_file_warn();
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-04-08 17:36:37 +00:00
|
|
|
HudEvent::ChangeBinding(game_input) => {
|
|
|
|
global_state.window.set_keybinding_mode(game_input);
|
|
|
|
},
|
2020-03-27 18:35:52 +00:00
|
|
|
HudEvent::ChangeFreeLookBehavior(behavior) => {
|
|
|
|
global_state.settings.gameplay.free_look_behavior = behavior;
|
|
|
|
},
|
2020-06-06 18:09:01 +00:00
|
|
|
HudEvent::ChangeAutoWalkBehavior(behavior) => {
|
|
|
|
global_state.settings.gameplay.auto_walk_behavior = behavior;
|
|
|
|
},
|
|
|
|
HudEvent::ChangeStopAutoWalkOnInput(state) => {
|
|
|
|
global_state.settings.gameplay.stop_auto_walk_on_input = state;
|
|
|
|
},
|
2019-03-16 02:03:21 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-26 14:04:44 +00:00
|
|
|
|
2020-03-03 19:51:15 +00:00
|
|
|
{
|
2020-02-29 03:59:11 +00:00
|
|
|
let client = self.client.borrow();
|
|
|
|
let scene_data = SceneData {
|
|
|
|
state: client.state(),
|
|
|
|
player_entity: client.entity(),
|
|
|
|
loaded_distance: client.loaded_distance(),
|
|
|
|
view_distance: client.view_distance().unwrap_or(1),
|
|
|
|
tick: client.get_tick(),
|
|
|
|
thread_pool: client.thread_pool(),
|
2020-04-23 22:59:34 +00:00
|
|
|
gamma: global_state.settings.graphics.gamma,
|
|
|
|
mouse_smoothing: global_state.settings.gameplay.smooth_pan_enable,
|
2020-04-25 13:37:08 +00:00
|
|
|
sprite_render_distance: global_state.settings.graphics.sprite_render_distance
|
|
|
|
as f32,
|
2020-04-26 01:44:56 +00:00
|
|
|
figure_lod_render_distance: global_state
|
|
|
|
.settings
|
|
|
|
.graphics
|
|
|
|
.figure_lod_render_distance
|
|
|
|
as f32,
|
2020-05-19 13:26:53 +00:00
|
|
|
is_aiming,
|
2020-02-29 03:59:11 +00:00
|
|
|
};
|
2019-01-23 20:01:58 +00:00
|
|
|
|
2020-04-25 13:04:30 +00:00
|
|
|
// Runs if either in a multiplayer server or the singleplayer server is unpaused
|
|
|
|
if global_state.singleplayer.is_none()
|
|
|
|
|| !global_state.singleplayer.as_ref().unwrap().is_paused()
|
|
|
|
{
|
|
|
|
self.scene.maintain(
|
|
|
|
global_state.window.renderer_mut(),
|
|
|
|
&mut global_state.audio,
|
|
|
|
&scene_data,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let renderer = global_state.window.renderer_mut();
|
|
|
|
// Clear the screen
|
|
|
|
renderer.clear();
|
|
|
|
// Render the screen using the global renderer
|
2020-04-25 13:37:08 +00:00
|
|
|
self.scene.render(
|
|
|
|
renderer,
|
|
|
|
client.state(),
|
|
|
|
client.entity(),
|
|
|
|
client.get_tick(),
|
|
|
|
&scene_data,
|
|
|
|
);
|
2020-04-25 13:04:30 +00:00
|
|
|
// Draw the UI to the screen
|
|
|
|
self.hud.render(renderer, self.scene.globals());
|
|
|
|
// Finish the frame
|
|
|
|
renderer.flush();
|
|
|
|
}
|
2019-01-11 23:18:34 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Display the frame on the window.
|
2019-04-02 01:05:18 +00:00
|
|
|
global_state
|
|
|
|
.window
|
2019-01-12 01:14:58 +00:00
|
|
|
.swap_buffers()
|
2019-05-17 09:22:32 +00:00
|
|
|
.expect("Failed to swap window buffers!");
|
2019-01-12 15:57:19 +00:00
|
|
|
|
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-01-23 20:01:58 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Clean things up after the tick.
|
2019-01-23 20:01:58 +00:00
|
|
|
self.cleanup();
|
2019-05-24 19:10:18 +00:00
|
|
|
|
|
|
|
current_client_state = self.client.borrow().get_client_state();
|
2019-01-11 23:18:34 +00:00
|
|
|
}
|
2019-05-19 17:22:35 +00:00
|
|
|
|
2020-03-08 20:31:27 +00:00
|
|
|
if let ClientState::Registered = current_client_state {
|
|
|
|
return PlayStateResult::Switch(Box::new(CharSelectionState::new(
|
|
|
|
global_state,
|
|
|
|
self.client.clone(),
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
2019-05-19 17:22:35 +00:00
|
|
|
PlayStateResult::Pop
|
2019-01-11 23:18:34 +00:00
|
|
|
}
|
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
fn name(&self) -> &'static str { "Session" }
|
2019-01-11 23:18:34 +00:00
|
|
|
}
|