Moved client to session type

This commit is contained in:
Joshua Barretto 2019-01-14 15:47:57 +00:00
parent 0a122431db
commit c5a506dad2
10 changed files with 36 additions and 48 deletions

View File

@ -17,7 +17,7 @@ uniform u_globals {
vec4 focus_pos;
vec4 view_distance;
vec4 time_of_day;
vec4 time;
vec4 tick;
};
out vec4 tgt_color;

View File

@ -18,7 +18,7 @@ uniform u_globals {
vec4 focus_pos;
vec4 view_distance;
vec4 time_of_day;
vec4 time;
vec4 tick;
};
struct BoneData {

View File

@ -15,7 +15,7 @@ uniform u_globals {
vec4 focus_pos;
vec4 view_distance;
vec4 time_of_day;
vec4 time;
vec4 tick;
};
out vec4 tgt_color;

View File

@ -15,7 +15,7 @@ uniform u_globals {
vec4 focus_pos;
vec4 view_distance;
vec4 time_of_day;
vec4 time;
vec4 tick;
};
out vec3 f_pos;

View File

@ -17,8 +17,8 @@ impl Animation for RunAnimation {
skeleton: &mut Self::Skeleton,
time: f64,
) {
let wave = (time as f32 * 10.0).sin();
let wave_fast = (time as f32 * 5.0).sin();
let wave = (time as f32 * 8.0).sin();
let wave_fast = (time as f32 * 4.0).sin();
skeleton.head.offset = Vec3::unit_z() * 13.0;
skeleton.head.ori = Quaternion::rotation_z(wave * 0.3);
@ -32,11 +32,11 @@ impl Animation for RunAnimation {
skeleton.leggings.offset = Vec3::unit_z() * 4.0;
skeleton.leggings.ori = Quaternion::rotation_z(wave * 0.3);
skeleton.l_hand.offset = Vec3::new(-8.0, wave * 4.0, 9.0);
skeleton.r_hand.offset = Vec3::new(8.0, -wave * 4.0, 9.0);
skeleton.l_hand.offset = Vec3::new(-8.0, wave * 5.0, 9.0);
skeleton.r_hand.offset = Vec3::new(8.0, -wave * 5.0, 9.0);
skeleton.l_foot.offset = Vec3::new(-3.5, -wave * 4.0, -(wave_fast.abs() - 0.5) * 3.0);
skeleton.r_foot.offset = Vec3::new(3.5, wave * 4.0, (wave_fast.abs() - 0.5) * 3.0);
skeleton.l_foot.offset = Vec3::new(-3.5, -wave * 4.0, -(wave_fast.abs() - 0.5).abs() * 4.0);
skeleton.r_foot.offset = Vec3::new(3.5, wave * 4.0, -(wave_fast.abs() - 0.5).abs() * 4.0);
skeleton.back.offset = Vec3::new(-8.0, 5.0, 16.0);
skeleton.back.ori = Quaternion::rotation_y(2.5);

View File

@ -8,7 +8,6 @@ use crate::render::FigureBoneData;
#[derive(Copy, Clone)]
pub struct Bone {
parent_idx: Option<u8>, // MUST be less than the current bone index
pub offset: Vec3<f32>,
pub ori: Quaternion<f32>,
}
@ -16,18 +15,11 @@ pub struct Bone {
impl Bone {
pub fn default() -> Self {
Self {
parent_idx: None,
offset: Vec3::zero(),
ori: Quaternion::identity(),
}
}
pub fn get_parent_idx(&self) -> Option<u8> { self.parent_idx }
pub fn set_parent_idx(&mut self, parent_idx: u8) {
self.parent_idx = Some(parent_idx);
}
pub fn compute_base_matrix(&self) -> Mat4<f32> {
Mat4::<f32>::translation_3d(self.offset) * Mat4::from(self.ori)
}

View File

@ -31,7 +31,7 @@ impl PlayState for TitleState {
Event::Close => return PlayStateResult::Shutdown,
// When space is pressed, start a session
Event::Char(' ') => return PlayStateResult::Push(
Box::new(SessionState::from_renderer(global_state.window.renderer_mut())),
Box::new(SessionState::new(global_state.window.renderer_mut())),
),
// Ignore all other events
_ => {},

View File

@ -23,7 +23,7 @@ gfx_defines! {
// TODO: Fix whatever alignment issue requires these uniforms to be aligned
view_distance: [f32; 4] = "view_distance",
time_of_day: [f32; 4] = "time_of_day", // TODO: Make this f64
time: [f32; 4] = "time",
tick: [f32; 4] = "tick",
}
}
@ -37,7 +37,7 @@ impl Globals {
focus_pos: [0.0; 4],
view_distance: [0.0; 4],
time_of_day: [0.0; 4],
time: [0.0; 4],
tick: [0.0; 4],
}
}
@ -49,7 +49,7 @@ impl Globals {
focus_pos: Vec3<f32>,
view_distance: f32,
time_of_day: f64,
time: f32,
tick: f64,
) -> Self {
Self {
view_mat: arr_to_mat(view_mat.into_col_array()),
@ -58,7 +58,7 @@ impl Globals {
focus_pos: Vec4::from(focus_pos).into_array(),
view_distance: [view_distance; 4],
time_of_day: [time_of_day as f32; 4],
time: [time; 4],
tick: [tick as f32; 4],
}
}
}

View File

@ -1,23 +1,16 @@
pub mod camera;
pub mod figure;
// Standard
use std::time::Duration;
// Library
use vek::*;
use dot_vox;
// Project
use client::{
self,
Client,
};
use common::figure::Segment;
use client::Client;
// Crate
use crate::{
Error,
render::{
Consts,
Globals,
@ -56,8 +49,6 @@ pub struct Scene {
skybox: Skybox,
test_figure: Figure<CharacterSkeleton>,
client: Client,
}
// TODO: Make a proper asset loading system
@ -105,17 +96,9 @@ impl Scene {
CharacterSkeleton::new(),
)
.unwrap(),
client: Client::new(),
}
}
/// Tick the scene (and the client attached to it)
pub fn tick(&mut self, dt: Duration) -> Result<(), Error> {
self.client.tick(client::Input {}, dt)?;
Ok(())
}
/// Handle an incoming user input event (i.e: cursor moved, key pressed, window closed, etc.).
pub fn handle_input_event(&mut self, event: Event) -> bool {
match event {
@ -130,7 +113,7 @@ impl Scene {
}
/// Maintain and update GPU data such as constant buffers, models, etc.
pub fn maintain_gpu_data(&mut self, renderer: &mut Renderer) {
pub fn maintain_gpu_data(&mut self, renderer: &mut Renderer, client: &Client) {
// Compute camera matrices
let (view_mat, proj_mat, cam_pos) = self.camera.compute_dependents();
@ -141,15 +124,15 @@ impl Scene {
cam_pos,
self.camera.get_focus_pos(),
10.0,
self.client.state().get_time_of_day(),
0.0,
client.state().get_time_of_day(),
client.state().get_tick(),
)])
.expect("Failed to update global constants");
// TODO: Don't do this here
RunAnimation::update_skeleton(
&mut self.test_figure.skeleton,
self.client.state().get_tick(),
client.state().get_tick(),
);
self.test_figure.update_locals(renderer, FigureLocals::default()).unwrap();
self.test_figure.update_skeleton(renderer).unwrap();

View File

@ -6,9 +6,14 @@ use vek::*;
// Project
use common::clock::Clock;
use client::{
self,
Client,
};
// Crate
use crate::{
Error,
PlayState,
PlayStateResult,
GlobalState,
@ -21,15 +26,17 @@ const FPS: u64 = 60;
pub struct SessionState {
scene: Scene,
client: Client,
}
/// 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 {
pub fn new(renderer: &mut Renderer) -> Self {
Self {
// Create a scene for this session. The scene handles visible elements of the game world
scene: Scene::new(renderer),
client: Client::new(),
}
}
}
@ -38,12 +45,18 @@ impl SessionState {
const BG_COLOR: Rgba<f32> = Rgba { r: 0.0, g: 0.3, b: 1.0, a: 1.0 };
impl SessionState {
/// Tick the session (and the client attached to it)
pub fn tick(&mut self, dt: Duration) -> Result<(), Error> {
self.client.tick(client::Input {}, dt)?;
Ok(())
}
/// 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);
self.scene.maintain_gpu_data(renderer, &self.client);
// Clear the screen
renderer.clear(BG_COLOR);
@ -79,7 +92,7 @@ impl PlayState for SessionState {
}
// Perform an in-game tick
self.scene.tick(clock.get_last_delta())
self.tick(clock.get_last_delta())
.expect("Failed to tick the scene");
// Render the session