2019-01-11 23:18:34 +00:00
|
|
|
use crate::{
|
2019-05-23 09:30:46 +00:00
|
|
|
hud::{DebugInfo, Event as HudEvent, Hud},
|
2019-03-02 03:48:30 +00:00
|
|
|
key_state::KeyState,
|
2019-01-11 23:18:34 +00:00
|
|
|
render::Renderer,
|
|
|
|
scene::Scene,
|
2019-04-25 15:32:59 +00:00
|
|
|
settings::Settings,
|
2019-04-29 20:37:19 +00:00
|
|
|
window::{Event, Key, Window},
|
|
|
|
Direction, Error, GlobalState, PlayState, PlayStateResult,
|
2019-01-11 23:18:34 +00:00
|
|
|
};
|
2019-05-09 17:58:16 +00:00
|
|
|
use client::{self, Client, Input, InputEvent};
|
|
|
|
use common::clock::Clock;
|
|
|
|
use std::{cell::RefCell, mem, rc::Rc, time::Duration};
|
|
|
|
use vek::*;
|
2019-01-11 23:18:34 +00:00
|
|
|
|
2019-01-12 15:57:19 +00:00
|
|
|
const FPS: u64 = 60;
|
|
|
|
|
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-02 03:48:30 +00:00
|
|
|
key_state: KeyState,
|
2019-05-01 16:55:29 +00:00
|
|
|
input_events: Vec<InputEvent>,
|
2019-03-15 04:55:52 +00:00
|
|
|
hud: Hud,
|
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-04-25 15:32:59 +00:00
|
|
|
pub fn new(window: &mut Window, client: Rc<RefCell<Client>>, settings: Settings) -> Self {
|
2019-05-17 09:22:32 +00:00
|
|
|
// Create a scene for this session. The scene handles visible elements of the game world.
|
2019-04-04 14:45:57 +00:00
|
|
|
let scene = Scene::new(window.renderer_mut(), &client.borrow());
|
|
|
|
Self {
|
|
|
|
scene,
|
2019-01-15 15:13:11 +00:00
|
|
|
client,
|
2019-03-02 03:48:30 +00:00
|
|
|
key_state: KeyState::new(),
|
2019-05-21 01:21:31 +00:00
|
|
|
hud: Hud::new(window),
|
2019-05-01 16:55:29 +00:00
|
|
|
input_events: Vec::new(),
|
2019-04-04 14:45:57 +00:00
|
|
|
}
|
2019-01-11 23:18:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Background colour
|
2019-04-02 01:05:18 +00:00
|
|
|
const BG_COLOR: Rgba<f32> = Rgba {
|
|
|
|
r: 0.0,
|
|
|
|
g: 0.3,
|
|
|
|
b: 1.0,
|
|
|
|
a: 1.0,
|
|
|
|
};
|
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).
|
2019-01-14 15:47:57 +00:00
|
|
|
pub fn tick(&mut self, dt: Duration) -> Result<(), Error> {
|
2019-05-17 09:22:32 +00:00
|
|
|
// Calculate the movement input vector of the player from the current key presses
|
|
|
|
// and the camera direction.
|
2019-03-02 03:48:30 +00:00
|
|
|
let ori = self.scene.camera().get_orientation();
|
|
|
|
let unit_vecs = (
|
|
|
|
Vec2::new(ori[0].cos(), -ori[0].sin()),
|
|
|
|
Vec2::new(ori[0].sin(), ori[0].cos()),
|
|
|
|
);
|
|
|
|
let dir_vec = self.key_state.dir_vec();
|
2019-03-02 19:43:51 +00:00
|
|
|
let move_dir = unit_vecs.0 * dir_vec[0] + unit_vecs.1 * dir_vec[1];
|
2019-03-02 03:48:30 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Take the input events.
|
2019-05-01 16:55:29 +00:00
|
|
|
let mut input_events = Vec::new();
|
|
|
|
mem::swap(&mut self.input_events, &mut input_events);
|
|
|
|
|
2019-05-09 17:58:16 +00:00
|
|
|
for event in self.client.borrow_mut().tick(
|
|
|
|
Input {
|
2019-05-01 16:55:29 +00:00
|
|
|
move_dir,
|
|
|
|
jumping: self.key_state.jump,
|
2019-05-13 20:59:42 +00:00
|
|
|
gliding: self.key_state.glide,
|
2019-05-01 16:55:29 +00:00
|
|
|
events: input_events,
|
2019-05-09 17:58:16 +00:00
|
|
|
},
|
|
|
|
dt,
|
|
|
|
)? {
|
2019-03-17 05:26:51 +00:00
|
|
|
match event {
|
|
|
|
client::Event::Chat(msg) => {
|
|
|
|
self.hud.new_message(msg);
|
|
|
|
}
|
2019-04-23 12:01:16 +00:00
|
|
|
client::Event::Disconnect => {
|
|
|
|
// TODO
|
|
|
|
}
|
2019-03-17 05:26:51 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-02 01:05:18 +00:00
|
|
|
|
2019-01-14 15:47:57 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Clean up the session (and the client attached to it) after a tick.
|
2019-01-23 20:01:58 +00:00
|
|
|
pub fn cleanup(&mut self) {
|
2019-04-04 14:45:57 +00:00
|
|
|
self.client.borrow_mut().cleanup();
|
2019-01-23 20:01:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 15:57:19 +00:00
|
|
|
/// Render the session to the screen.
|
|
|
|
///
|
|
|
|
/// This method should be called once per frame.
|
|
|
|
pub fn render(&mut self, renderer: &mut Renderer) {
|
|
|
|
// Clear the screen
|
|
|
|
renderer.clear(BG_COLOR);
|
|
|
|
|
|
|
|
// Render the screen using the global renderer
|
2019-04-23 11:55:48 +00:00
|
|
|
self.scene.render(renderer, &mut self.client.borrow_mut());
|
2019-02-16 03:01:42 +00:00
|
|
|
// Draw the UI to the screen
|
2019-05-14 06:43:07 +00:00
|
|
|
self.hud.render(renderer, self.scene.globals());
|
2019-01-12 15:57:19 +00:00
|
|
|
|
|
|
|
// Finish the frame
|
|
|
|
renderer.flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01-12 15:57:19 +00:00
|
|
|
let mut clock = Clock::new();
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Load a few chunks. TODO: Remove this.
|
2019-04-10 17:23:27 +00:00
|
|
|
/*
|
2019-01-23 22:39:31 +00:00
|
|
|
for x in -6..7 {
|
|
|
|
for y in -6..7 {
|
2019-01-23 20:01:58 +00:00
|
|
|
for z in -1..2 {
|
2019-04-04 14:45:57 +00:00
|
|
|
self.client.borrow_mut().load_chunk(Vec3::new(x, y, z));
|
2019-01-23 20:01:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-10 17:23:27 +00:00
|
|
|
*/
|
2019-01-23 20:01:58 +00:00
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
// Game loop
|
|
|
|
loop {
|
2019-05-17 09:22:32 +00:00
|
|
|
// Handle window events.
|
2019-01-11 23:18:34 +00:00
|
|
|
for event in global_state.window.fetch_events() {
|
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-01-12 13:56:34 +00:00
|
|
|
let _handled = match event {
|
2019-04-17 15:22:26 +00:00
|
|
|
Event::Close => {
|
|
|
|
return PlayStateResult::Shutdown;
|
2019-04-29 20:37:19 +00:00
|
|
|
}
|
2019-03-02 03:48:30 +00:00
|
|
|
// Movement Key Pressed
|
|
|
|
Event::KeyDown(Key::MoveForward) => self.key_state.up = true,
|
|
|
|
Event::KeyDown(Key::MoveBack) => self.key_state.down = true,
|
|
|
|
Event::KeyDown(Key::MoveLeft) => self.key_state.left = true,
|
|
|
|
Event::KeyDown(Key::MoveRight) => self.key_state.right = true,
|
2019-05-01 16:55:29 +00:00
|
|
|
Event::KeyDown(Key::Jump) => {
|
|
|
|
self.input_events.push(InputEvent::Jump);
|
|
|
|
self.key_state.jump = true;
|
2019-05-09 17:58:16 +00:00
|
|
|
}
|
2019-05-13 20:59:42 +00:00
|
|
|
Event::KeyDown(Key::Glide) => self.key_state.glide = true,
|
2019-03-02 03:48:30 +00:00
|
|
|
// Movement Key Released
|
|
|
|
Event::KeyUp(Key::MoveForward) => self.key_state.up = false,
|
|
|
|
Event::KeyUp(Key::MoveBack) => self.key_state.down = false,
|
|
|
|
Event::KeyUp(Key::MoveLeft) => self.key_state.left = false,
|
|
|
|
Event::KeyUp(Key::MoveRight) => self.key_state.right = false,
|
2019-05-01 16:55:29 +00:00
|
|
|
Event::KeyUp(Key::Jump) => self.key_state.jump = false,
|
2019-05-13 20:59:42 +00:00
|
|
|
Event::KeyUp(Key::Glide) => self.key_state.glide = false,
|
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);
|
|
|
|
}
|
2019-01-12 13:56:34 +00:00
|
|
|
};
|
|
|
|
// TODO: Do something if the event wasn't handled?
|
2019-01-11 23:18:34 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Perform an in-game tick.
|
2019-01-14 15:47:57 +00:00
|
|
|
self.tick(clock.get_last_delta())
|
2019-05-17 09:22:32 +00:00
|
|
|
.expect("Failed to tick the scene!");
|
2019-01-11 23:18:34 +00:00
|
|
|
|
2019-05-18 20:10:02 +00:00
|
|
|
// Maintain global state
|
|
|
|
global_state.maintain();
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Maintain the scene.
|
2019-05-19 18:07:50 +00:00
|
|
|
self.scene
|
|
|
|
.maintain(global_state.window.renderer_mut(), &self.client.borrow());
|
2019-05-18 20:10:02 +00:00
|
|
|
|
2019-05-23 09:30:46 +00:00
|
|
|
// extract HUD events ensuring the client borrow gets dropped
|
|
|
|
let 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(),
|
|
|
|
ping_ms: self.client.borrow().get_ping_ms(),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
// 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);
|
2019-04-29 20:37:19 +00:00
|
|
|
}
|
2019-03-16 02:03:21 +00:00
|
|
|
HudEvent::Logout => return PlayStateResult::Pop,
|
2019-04-17 15:22:26 +00:00
|
|
|
HudEvent::Quit => {
|
|
|
|
return PlayStateResult::Shutdown;
|
2019-04-29 20:37:19 +00:00
|
|
|
}
|
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;
|
|
|
|
global_state.settings.save_to_file();
|
2019-05-18 23:55:06 +00:00
|
|
|
}
|
2019-05-19 19:31:32 +00:00
|
|
|
HudEvent::AdjustVolume(volume) => {
|
|
|
|
global_state.audio.set_volume(volume);
|
2019-05-20 20:18:01 +00:00
|
|
|
|
|
|
|
global_state.settings.audio.music_volume = volume;
|
|
|
|
global_state.settings.save_to_file();
|
2019-05-19 19:31:32 +00:00
|
|
|
}
|
2019-05-20 17:40:35 +00:00
|
|
|
HudEvent::ChangeAudioDevice(name) => {
|
2019-05-20 20:18:01 +00:00
|
|
|
global_state.audio.set_device(name.clone());
|
|
|
|
|
2019-05-22 11:29:38 +00:00
|
|
|
global_state.settings.audio.audio_device = Some(name);
|
2019-05-20 20:18:01 +00:00
|
|
|
global_state.settings.save_to_file();
|
2019-05-20 17:40:35 +00:00
|
|
|
}
|
2019-03-16 02:03:21 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-23 20:01:58 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Render the session.
|
2019-01-12 15:57:19 +00:00
|
|
|
self.render(global_state.window.renderer_mut());
|
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-01-12 15:57:19 +00:00
|
|
|
clock.tick(Duration::from_millis(1000 / FPS));
|
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-01-11 23:18:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 01:05:18 +00:00
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"Session"
|
|
|
|
}
|
2019-01-11 23:18:34 +00:00
|
|
|
}
|