Cleaned up codebase, added cursor grab toggle

This commit is contained in:
Joshua Barretto 2019-01-23 22:21:47 +00:00
parent 248577bdef
commit 8a37662cf0
13 changed files with 54 additions and 47 deletions

View File

@ -13,14 +13,6 @@ use crate::{
// Local
use self::cell::Cell;
/// A type representing a single figure bone (e.g: the limb of a character).
#[derive(Copy, Clone)]
pub struct Bone {
origin: Vec3<f32>,
offset: Vec3<f32>,
ori: Vec3<f32>,
}
/// A type representing a volume that may be part of an animated figure.
///
/// Figures are used to represent things like characters, NPCs, mobs, etc.

View File

@ -1 +1 @@
pub mod worker_pool;

View File

@ -1,10 +0,0 @@
// Library
use threadpool::ThreadPool;
pub struct WorkerPool {
thread_pool: ThreadPool,
}
impl WorkerPool {
}

View File

@ -14,6 +14,7 @@ use crate::vol::{
VolSize,
};
#[derive(Debug)]
pub enum ChunkErr {
OutOfBounds,
}

View File

@ -10,6 +10,7 @@ use crate::vol::{
WriteVol,
};
#[derive(Debug)]
pub enum DynaErr {
OutOfBounds,
}

View File

@ -21,6 +21,7 @@ use crate::{
},
};
#[derive(Debug)]
pub enum VolMapErr {
NoSuchChunk,
ChunkErr(ChunkErr),

View File

@ -34,7 +34,7 @@ impl GlobalState {
/// Called after a change in play state has occured (usually used to reverse any temporary
/// effects a state may have made).
pub fn on_play_state_changed(&mut self) {
self.window.untrap_cursor();
self.window.grab_cursor(false);
}
}

View File

@ -1,9 +1,6 @@
pub mod segment;
pub mod terrain;
// Library
use vek::*;
// Crate
use crate::render::{
self,

View File

@ -9,7 +9,7 @@ use common::{
ReadVol,
},
volumes::dyna::Dyna,
terrain::{Block, TerrainChunk},
terrain::Block,
};
// Crate

View File

@ -12,10 +12,7 @@ use vek::*;
use client::Client;
use common::{
terrain::TerrainMap,
volumes::vol_map::{
VolMap,
VolMapErr,
},
volumes::vol_map::VolMapErr,
vol::SampleVol,
};
@ -179,7 +176,7 @@ impl Terrain {
}
pub fn render(&self, renderer: &mut Renderer, globals: &Consts<Globals>) {
for (pos, chunk) in &self.chunks {
for (_, chunk) in &self.chunks {
renderer.render_terrain_chunk(
&chunk.model,
globals,

View File

@ -17,7 +17,7 @@ use crate::{
PlayState,
PlayStateResult,
GlobalState,
window::Event,
window::{Event, Key},
render::Renderer,
scene::Scene,
};
@ -75,14 +75,14 @@ impl SessionState {
impl PlayState for SessionState {
fn play(&mut self, global_state: &mut GlobalState) -> PlayStateResult {
// Trap the cursor
global_state.window.trap_cursor();
global_state.window.grab_cursor(true);
// Set up an fps clock
let mut clock = Clock::new();
// Load a few chunks TODO: Remove this
for x in -2..3 {
for y in -2..3 {
for x in -4..5 {
for y in -4..5 {
for z in -1..2 {
self.client.load_chunk(Vec3::new(x, y, z));
}
@ -97,8 +97,12 @@ impl PlayState for SessionState {
Event::Close => return PlayStateResult::Shutdown,
// When 'q' is pressed, exit the session
Event::Char('q') => return PlayStateResult::Pop,
// Toggle cursor grabbing
Event::KeyDown(Key::ToggleCursor) => {
global_state.window.grab_cursor(!global_state.window.is_cursor_grabbed());
},
// Pass all other events to the scene
event => self.scene.handle_input_event(event),
event => { self.scene.handle_input_event(event); },
};
// TODO: Do something if the event wasn't handled?
}

View File

@ -15,8 +15,9 @@ use crate::{
pub struct Window {
events_loop: glutin::EventsLoop,
window: glutin::GlWindow,
renderer: Renderer,
window: glutin::GlWindow,
cursor_grabbed: bool,
}
@ -47,13 +48,14 @@ impl Window {
let tmp = Ok(Self {
events_loop,
window,
renderer: Renderer::new(
device,
factory,
tgt_color_view,
tgt_depth_view,
)?,
window,
cursor_grabbed: false,
});
tmp
}
@ -62,15 +64,27 @@ impl Window {
pub fn renderer_mut(&mut self) -> &mut Renderer { &mut self.renderer }
pub fn fetch_events(&mut self) -> Vec<Event> {
// Copy data that is needed by the events closure to avoid lifetime errors
// TODO: Remove this if/when the compiler permits it
let cursor_grabbed = self.cursor_grabbed;
let mut events = vec![];
self.events_loop.poll_events(|event| match event {
glutin::Event::WindowEvent { event, .. } => match event {
glutin::WindowEvent::CloseRequested => events.push(Event::Close),
glutin::WindowEvent::ReceivedCharacter(c) => events.push(Event::Char(c)),
glutin::WindowEvent::KeyboardInput { input, .. } => match input.virtual_keycode {
Some(glutin::VirtualKeyCode::Escape) => events.push(if input.state == glutin::ElementState::Pressed {
Event::KeyDown(Key::ToggleCursor)
} else {
Event::KeyUp(Key::ToggleCursor)
}),
_ => {},
},
_ => {},
},
glutin::Event::DeviceEvent { event, .. } => match event {
glutin::DeviceEvent::MouseMotion { delta: (dx, dy), .. } =>
glutin::DeviceEvent::MouseMotion { delta: (dx, dy), .. } if cursor_grabbed =>
events.push(Event::CursorPan(Vec2::new(dx as f32, dy as f32))),
_ => {},
},
@ -84,25 +98,33 @@ impl Window {
.map_err(|err| Error::BackendError(Box::new(err)))
}
pub fn trap_cursor(&mut self) {
self.window.hide_cursor(true);
self.window.grab_cursor(true)
.expect("Failed to grab cursor");
pub fn is_cursor_grabbed(&self) -> bool {
self.cursor_grabbed
}
pub fn untrap_cursor(&mut self) {
self.window.hide_cursor(false);
self.window.grab_cursor(false)
.expect("Failed to ungrab cursor");
pub fn grab_cursor(&mut self, grab: bool) {
self.cursor_grabbed = grab;
self.window.hide_cursor(grab);
self.window.grab_cursor(grab)
.expect("Failed to grab/ungrab cursor");
}
}
/// Represents a key that the game recognises after keyboard mapping
pub enum Key {
ToggleCursor,
}
/// Represents an incoming event from the window
pub enum Event {
/// The window has been requested to close.
Close,
/// A key has been typed that corresponds to a specific character.
Char(char),
/// The cursor has been panned across the screen while trapped.
/// The cursor has been panned across the screen while grabbed.
CursorPan(Vec2<f32>),
/// A key that the game recognises has been pressed down
KeyDown(Key),
/// A key that the game recognises has been released down
KeyUp(Key),
}

View File

@ -25,6 +25,8 @@ impl World {
}
pub fn generate_chunk(&self, chunk_pos: Vec3<i32>) -> TerrainChunk {
// TODO: This is all test code, remove/improve this later
let mut chunk = TerrainChunk::filled(Block::empty(), TerrainChunkMeta::void());
let air = Block::empty();
@ -50,7 +52,7 @@ impl World {
}
} else {
air
});
}).unwrap();
}
chunk