2019-01-12 15:57:19 +00:00
|
|
|
// Standard
|
|
|
|
use std::time::Duration;
|
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
// Library
|
|
|
|
use vek::*;
|
|
|
|
|
2019-01-12 15:57:19 +00:00
|
|
|
// Project
|
|
|
|
use common::clock::Clock;
|
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
// Crate
|
|
|
|
use crate::{
|
|
|
|
PlayState,
|
|
|
|
PlayStateResult,
|
|
|
|
GlobalState,
|
|
|
|
window::Event,
|
|
|
|
render::Renderer,
|
|
|
|
scene::Scene,
|
|
|
|
};
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Represents an active game session (i.e: one that is being played)
|
|
|
|
impl SessionState {
|
|
|
|
/// Create a new `SessionState`
|
|
|
|
pub fn from_renderer(renderer: &mut Renderer) -> Self {
|
|
|
|
Self {
|
|
|
|
// Create a scene for this session. The scene handles visible elements of the game world
|
|
|
|
scene: Scene::new(renderer),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The background colour
|
|
|
|
const BG_COLOR: Rgba<f32> = Rgba { r: 0.0, g: 0.3, b: 1.0, a: 1.0 };
|
|
|
|
|
2019-01-12 15:57:19 +00:00
|
|
|
impl SessionState {
|
|
|
|
/// Render the session to the screen.
|
|
|
|
///
|
|
|
|
/// This method should be called once per frame.
|
|
|
|
pub fn render(&mut self, renderer: &mut Renderer) {
|
|
|
|
// Maintain scene GPU data
|
|
|
|
self.scene.maintain_gpu_data(renderer);
|
|
|
|
|
|
|
|
// Clear the screen
|
|
|
|
renderer.clear(BG_COLOR);
|
|
|
|
|
|
|
|
// Render the screen using the global renderer
|
|
|
|
self.scene.render_to(renderer);
|
|
|
|
|
|
|
|
// Finish the frame
|
|
|
|
renderer.flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
impl PlayState for SessionState {
|
|
|
|
fn play(&mut self, global_state: &mut GlobalState) -> PlayStateResult {
|
2019-01-12 01:14:58 +00:00
|
|
|
// Trap the cursor
|
|
|
|
global_state.window.trap_cursor();
|
|
|
|
|
2019-01-12 15:57:19 +00:00
|
|
|
// Set up an fps clock
|
|
|
|
let mut clock = Clock::new();
|
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
// Game loop
|
|
|
|
loop {
|
|
|
|
// Handle window events
|
|
|
|
for event in global_state.window.fetch_events() {
|
2019-01-12 13:56:34 +00:00
|
|
|
let _handled = match event {
|
2019-01-11 23:18:34 +00:00
|
|
|
Event::Close => return PlayStateResult::Shutdown,
|
|
|
|
// When 'q' is pressed, exit the session
|
|
|
|
Event::Char('q') => return PlayStateResult::Pop,
|
2019-01-12 13:56:34 +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-01-11 23:18:34 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 15:57:19 +00:00
|
|
|
// Perform an in-game tick
|
|
|
|
self.scene.tick(clock.get_last_delta())
|
|
|
|
.expect("Failed to tick the scene");
|
2019-01-11 23:18:34 +00:00
|
|
|
|
2019-01-12 15:57:19 +00:00
|
|
|
// Render the session
|
|
|
|
self.render(global_state.window.renderer_mut());
|
2019-01-11 23:18:34 +00:00
|
|
|
|
2019-01-12 15:57:19 +00:00
|
|
|
// Display the frame on the window
|
2019-01-12 01:14:58 +00:00
|
|
|
global_state.window
|
|
|
|
.swap_buffers()
|
|
|
|
.expect("Failed to swap window buffers");
|
2019-01-12 15:57:19 +00:00
|
|
|
|
|
|
|
// Wait for the next tick
|
|
|
|
clock.tick(Duration::from_millis(1000 / FPS));
|
2019-01-11 23:18:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn name(&self) -> &'static str { "Session" }
|
|
|
|
}
|