diff options
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | examples/render-bsp/Cargo.toml | 1 | ||||
-rw-r--r-- | examples/render-bsp/src/main.rs | 38 | ||||
-rw-r--r-- | stockton-contrib/Cargo.toml | 18 | ||||
-rw-r--r-- | stockton-contrib/src/delta_time.rs | 42 | ||||
-rw-r--r-- | stockton-contrib/src/flycam.rs | 55 | ||||
-rw-r--r-- | stockton-contrib/src/lib.rs | 25 |
7 files changed, 175 insertions, 5 deletions
@@ -5,6 +5,7 @@ members = [ "stockton-input-codegen", "stockton-render", "stockton-levels", + "stockton-contrib", "examples/render-bsp", "examples/input-codegen" ]
\ No newline at end of file diff --git a/examples/render-bsp/Cargo.toml b/examples/render-bsp/Cargo.toml index dccc9c2..90f5ef9 100644 --- a/examples/render-bsp/Cargo.toml +++ b/examples/render-bsp/Cargo.toml @@ -10,6 +10,7 @@ stockton-input = { path = "../../stockton-input" } stockton-input-codegen = { path = "../../stockton-input-codegen" } stockton-types = { path = "../../stockton-types" } stockton-levels = { path = "../../stockton-levels" } +stockton-contrib = { path = "../../stockton-contrib", features = ["delta_time", "flycam"] } winit = "^0.21" log = "0.4.0" simple_logger = "^1.11" diff --git a/examples/render-bsp/src/main.rs b/examples/render-bsp/src/main.rs index cea34cd..5c03676 100644 --- a/examples/render-bsp/src/main.rs +++ b/examples/render-bsp/src/main.rs @@ -17,13 +17,15 @@ //! Renders ./example.bsp -use std::f32::consts::PI; -use stockton_input::{Axis, InputManager}; #[macro_use] extern crate stockton_input_codegen; + use std::collections::BTreeMap; use winit::{event::Event, event_loop::EventLoop, window::WindowBuilder}; +use stockton_contrib::delta_time::*; +use stockton_contrib::flycam::*; +use stockton_input::{Axis, InputManager}; use stockton_levels::{prelude::*, q3::Q3BSPFile}; use stockton_render::{ do_render_system, draw::calc_vp_matrix_system, window::process_window_events_system, Renderer, @@ -44,6 +46,18 @@ struct MovementInputs { z: Axis, } +impl FlycamInput for MovementInputs { + fn get_x_axis(&self) -> &Axis { + &self.x + } + fn get_y_axis(&self) -> &Axis { + &self.y + } + fn get_z_axis(&self) -> &Axis { + &self.z + } +} + fn main() { // Initialise logger simple_logger::SimpleLogger::new() @@ -75,8 +89,17 @@ fn main() { // Create the input manager let manager = { - let actions = BTreeMap::new(); - // TODO: An actual control schema + use stockton_input::InputMutation::*; + use MovementInputsFields::*; + + let mut actions = BTreeMap::new(); + + actions.insert(17, (Z, PositiveAxis)); // W + actions.insert(30, (X, NegativeAxis)); // A + actions.insert(31, (Z, NegativeAxis)); // S + actions.insert(32, (X, PositiveAxis)); // D + actions.insert(29, (Y, NegativeAxis)); // Ctrl + actions.insert(57, (Y, PositiveAxis)); // Space MovementInputsManager::new(actions) }; @@ -87,10 +110,14 @@ fn main() { resources.insert(renderer); resources.insert(bsp); resources.insert(manager); + resources.insert(Timing::default()) }, move |schedule| { schedule + .add_system(update_deltatime_system()) .add_system(process_window_events_system::<MovementInputsManager>()) + .add_system(flycam_move_system::<MovementInputsManager>()) + .flush() .add_system(calc_vp_matrix_system()) .add_thread_local(do_render_system::<Q3BSPFile<VulkanSystem>>()); }, @@ -100,13 +127,14 @@ fn main() { let _player = session.world.push(( Transform { position: Vector3::new(0.0, 0.0, 0.0), - rotation: Vector3::new(0.0, PI / 2.0, 0.0), + rotation: Vector3::new(0.0, 0.0, 0.0), }, CameraSettings { far: 1024.0, fov: 90.0, near: 0.1, }, + FlycamControlled { speed: 512.0 }, )); // Done loading - This is our main loop. diff --git a/stockton-contrib/Cargo.toml b/stockton-contrib/Cargo.toml new file mode 100644 index 0000000..b06afc1 --- /dev/null +++ b/stockton-contrib/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "stockton-contrib" +version = "0.1.0" +authors = ["Oscar Shrimpton <oscar.shrimpton.personal@gmail.com>"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +stockton-input = { path = "../stockton-input" } +stockton-types = { path = "../stockton-types" } +legion = { version = "^0.3" } + +[features] +default = [] + +delta_time = [] +flycam = []
\ No newline at end of file diff --git a/stockton-contrib/src/delta_time.rs b/stockton-contrib/src/delta_time.rs new file mode 100644 index 0000000..4684e79 --- /dev/null +++ b/stockton-contrib/src/delta_time.rs @@ -0,0 +1,42 @@ +/* + * Copyright (C) Oscar Shrimpton 2020 + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +use std::time::Instant; + +#[derive(Debug, Clone)] +pub struct Timing { + pub delta_time: f32, + + pub(crate) last_frame_start: Instant, +} + +impl Default for Timing { + fn default() -> Self { + Timing { + delta_time: 0.0, + + last_frame_start: Instant::now(), + } + } +} + +#[system] +pub fn update_deltatime(#[resource] timing: &mut Timing) { + let now = Instant::now(); + timing.delta_time = now.duration_since(timing.last_frame_start).as_secs_f32(); + timing.last_frame_start = now; +} diff --git a/stockton-contrib/src/flycam.rs b/stockton-contrib/src/flycam.rs new file mode 100644 index 0000000..7b82c4f --- /dev/null +++ b/stockton-contrib/src/flycam.rs @@ -0,0 +1,55 @@ +/* + * Copyright (C) Oscar Shrimpton 2020 + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +use stockton_input::{Axis, InputManager}; +use stockton_types::components::Transform; +use stockton_types::Vector3; + +use crate::delta_time::Timing; + +pub trait FlycamInput { + fn get_x_axis(&self) -> &Axis; + fn get_y_axis(&self) -> &Axis; + fn get_z_axis(&self) -> &Axis; +} + +pub struct FlycamControlled { + pub speed: f32, +} + +#[system(for_each)] +pub fn flycam_move<T>( + #[resource] manager: &T, + #[resource] timing: &Timing, + transform: &mut Transform, + flycam: &FlycamControlled, +) where + T: 'static + InputManager, + T::Inputs: FlycamInput, +{ + // TODO: Deal with looking around + + let inputs = manager.get_inputs(); + let speed = flycam.speed; + let impulse = Vector3::new( + **inputs.get_x_axis() as f32 * speed * timing.delta_time, + **inputs.get_y_axis() as f32 * speed * timing.delta_time, + **inputs.get_z_axis() as f32 * speed * timing.delta_time, + ); + + transform.position += impulse; +} diff --git a/stockton-contrib/src/lib.rs b/stockton-contrib/src/lib.rs new file mode 100644 index 0000000..13f1ea1 --- /dev/null +++ b/stockton-contrib/src/lib.rs @@ -0,0 +1,25 @@ +/* + * Copyright (C) Oscar Shrimpton 2020 + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#[macro_use] +extern crate legion; + +#[cfg(feature = "delta_time")] +pub mod delta_time; + +#[cfg(feature = "flycam")] +pub mod flycam; |