mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-11-10 01:28:41 -06:00
camera controls!
This commit is contained in:
parent
a91834a65c
commit
f076172fb8
16
src/game.rs
16
src/game.rs
|
@ -1,6 +1,6 @@
|
||||||
use glium::{Surface, uniform};
|
use glium::{Surface, uniform};
|
||||||
use glium::glutin::{
|
use glium::glutin::{
|
||||||
event::{Event, WindowEvent, DeviceEvent, VirtualKeyCode},
|
event::{Event, WindowEvent, DeviceEvent, KeyboardInput, VirtualKeyCode},
|
||||||
event_loop::{EventLoop, ControlFlow},
|
event_loop::{EventLoop, ControlFlow},
|
||||||
};
|
};
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
@ -41,6 +41,8 @@ pub fn run() {
|
||||||
let assets = Assets::load_all_sync(&display);
|
let assets = Assets::load_all_sync(&display);
|
||||||
log::info!("init game state");
|
log::info!("init game state");
|
||||||
let mut state = State::init();
|
let mut state = State::init();
|
||||||
|
state.camera.position = [0., 0., 1.];
|
||||||
|
state.camera.direction = [0., 0., -1.];
|
||||||
log::info!("game loaded");
|
log::info!("game loaded");
|
||||||
|
|
||||||
//=======================
|
//=======================
|
||||||
|
@ -58,8 +60,7 @@ pub fn run() {
|
||||||
match event {
|
match event {
|
||||||
Event::MainEventsCleared => (),
|
Event::MainEventsCleared => (),
|
||||||
Event::DeviceEvent {
|
Event::DeviceEvent {
|
||||||
event: DeviceEvent::MouseMotion{ delta, },
|
event: DeviceEvent::MouseMotion{ delta, }, ..
|
||||||
..
|
|
||||||
} => {
|
} => {
|
||||||
state.controls.process_mouse_input(delta.0, delta.1);
|
state.controls.process_mouse_input(delta.0, delta.1);
|
||||||
}
|
}
|
||||||
|
@ -70,6 +71,14 @@ pub fn run() {
|
||||||
*control_flow = ControlFlow::Exit;
|
*control_flow = ControlFlow::Exit;
|
||||||
return
|
return
|
||||||
},
|
},
|
||||||
|
WindowEvent::KeyboardInput {
|
||||||
|
input: KeyboardInput {
|
||||||
|
virtual_keycode: Some(key),
|
||||||
|
state: el_state, ..
|
||||||
|
}, ..
|
||||||
|
} => {
|
||||||
|
state.controls.process_keyboard_input(key, el_state);
|
||||||
|
},
|
||||||
_ => return
|
_ => return
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -81,6 +90,7 @@ pub fn run() {
|
||||||
last_render = now;
|
last_render = now;
|
||||||
|
|
||||||
let actions = state.controls.calculate(dt);
|
let actions = state.controls.calculate(dt);
|
||||||
|
log::trace!("{}", actions.rotation[0]);
|
||||||
actions.apply_to_camera(&mut state.camera);
|
actions.apply_to_camera(&mut state.camera);
|
||||||
|
|
||||||
let mut target = display.draw();
|
let mut target = display.draw();
|
||||||
|
|
|
@ -11,18 +11,18 @@ pub struct InputAmounts {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Actions {
|
pub struct Actions {
|
||||||
movement: [f32; 3],
|
pub movement: [f32; 3],
|
||||||
rotation: [f32; 2],
|
pub rotation: [f32; 2],
|
||||||
}
|
}
|
||||||
impl Actions {
|
impl Actions {
|
||||||
pub fn apply_to_camera(&self, camera: &mut Camera) {
|
pub fn apply_to_camera(&self, camera: &mut Camera) {
|
||||||
//Apply movement
|
//Apply movement
|
||||||
for v in camera.position.iter_mut().zip(self.movement) {
|
for v in camera.position.iter_mut().zip(self.movement) {
|
||||||
*v.0 += v.1;
|
*v.0 -= v.1;
|
||||||
}
|
}
|
||||||
//Apply rotation
|
//Apply rotation
|
||||||
camera.yaw += self.rotation[0];
|
camera.yaw -= self.rotation[0];
|
||||||
camera.pitch += self.rotation[1];
|
camera.pitch -= self.rotation[1];
|
||||||
camera.update_direction();
|
camera.update_direction();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,13 +62,17 @@ impl Controls {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn calculate(&mut self, dt: f32) -> Actions {
|
pub fn calculate(&mut self, dt: f32) -> Actions {
|
||||||
let mut movement = {
|
let movement = {
|
||||||
let magnitude = (self.inputs.move_x.powi(2) + self.inputs.move_y.powi(2) + self.inputs.move_z.powi(2)).sqrt();
|
let magnitude = (self.inputs.move_x.powi(2) + self.inputs.move_y.powi(2) + self.inputs.move_z.powi(2)).sqrt();
|
||||||
|
if magnitude == 0. {
|
||||||
|
[0., 0., 0.]
|
||||||
|
} else {
|
||||||
[
|
[
|
||||||
dt * self.speed * (self.inputs.move_x / magnitude),
|
dt * self.speed * (self.inputs.move_x / magnitude),
|
||||||
dt * self.speed * (self.inputs.move_y / magnitude),
|
dt * self.speed * (self.inputs.move_y / magnitude),
|
||||||
dt * self.speed * (self.inputs.move_z / magnitude)
|
dt * self.speed * (self.inputs.move_z / magnitude)
|
||||||
]
|
]
|
||||||
|
}
|
||||||
};
|
};
|
||||||
let rotation = [
|
let rotation = [
|
||||||
dt * self.inputs.look_h * self.sensitivity,
|
dt * self.inputs.look_h * self.sensitivity,
|
||||||
|
@ -82,8 +86,8 @@ impl Default for Controls {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
inputs: Default::default(),
|
inputs: Default::default(),
|
||||||
speed: 1.,
|
speed: 10.,
|
||||||
sensitivity: 1.,
|
sensitivity: 2.,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue