Fix first person weapon visible while not wielding

This commit is contained in:
scott-c
2019-08-18 17:01:57 +08:00
parent d045dbb2f6
commit bc1ccfc99b
5 changed files with 119 additions and 54 deletions

View File

@ -2,7 +2,7 @@ use specs::{Component, FlaggedStorage, HashMapStorage};
use specs_idvs::IDVStorage; use specs_idvs::IDVStorage;
use std::time::Duration; use std::time::Duration;
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] #[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
pub enum MovementState { pub enum MovementState {
Stand, Stand,
Run, Run,
@ -22,7 +22,7 @@ impl MovementState {
} }
} }
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] #[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
pub enum ActionState { pub enum ActionState {
Idle, Idle,
Wield { time_left: Duration }, Wield { time_left: Duration },
@ -57,7 +57,7 @@ impl ActionState {
} }
} }
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] #[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
pub struct CharacterState { pub struct CharacterState {
pub movement: MovementState, pub movement: MovementState,
pub action: ActionState, pub action: ActionState,

View File

@ -164,8 +164,8 @@ impl Scene {
Body::Humanoid(body), Body::Humanoid(body),
Some(equipment), Some(equipment),
client.get_tick(), client.get_tick(),
false, None,
false, None,
) )
.0; .0;

View File

@ -12,12 +12,18 @@ const THIRD_PERSON_INTERP_TIME: f32 = 0.1;
pub const MIN_ZOOM: f32 = 0.1; pub const MIN_ZOOM: f32 = 0.1;
// Possible TODO: Add more modes // Possible TODO: Add more modes
#[derive(PartialEq, Clone, Copy)] #[derive(PartialEq, Clone, Copy, Eq, Hash)]
pub enum CameraMode { pub enum CameraMode {
FirstPerson, FirstPerson,
ThirdPerson, ThirdPerson,
} }
impl Default for CameraMode {
fn default() -> Self {
Self::ThirdPerson
}
}
pub struct Camera { pub struct Camera {
tgt_focus: Vec3<f32>, tgt_focus: Vec3<f32>,
focus: Vec3<f32>, focus: Vec3<f32>,

View File

@ -2,17 +2,23 @@ use super::load::*;
use crate::{ use crate::{
anim::SkeletonAttr, anim::SkeletonAttr,
render::{FigurePipeline, Mesh, Model, Renderer}, render::{FigurePipeline, Mesh, Model, Renderer},
scene::camera::CameraMode,
}; };
use common::{ use common::{
assets::watch::ReloadIndicator, assets::watch::ReloadIndicator,
comp::{Body, Equipment}, comp::{ActionState, Body, CharacterState, Equipment},
}; };
use hashbrown::HashMap; use hashbrown::HashMap;
#[derive(PartialEq, Eq, Hash, Clone)] #[derive(PartialEq, Eq, Hash, Clone)]
enum FigureKey { enum FigureKey {
Simple(Body), Simple(Body),
Complex(Body, Option<Equipment>, bool, bool), Complex(
Body,
Option<Equipment>,
Option<CameraMode>,
Option<CharacterState>,
),
} }
pub struct FigureModelCache { pub struct FigureModelCache {
@ -34,11 +40,16 @@ impl FigureModelCache {
body: Body, body: Body,
equipment: Option<&Equipment>, equipment: Option<&Equipment>,
tick: u64, tick: u64,
first_person: bool, camera_mode: Option<CameraMode>,
gliding: bool, character_state: Option<&CharacterState>,
) -> &(Model<FigurePipeline>, SkeletonAttr) { ) -> &(Model<FigurePipeline>, SkeletonAttr) {
let key = if equipment.is_some() { let key = if equipment.is_some() {
FigureKey::Complex(body, equipment.cloned(), first_person, gliding) FigureKey::Complex(
body,
equipment.cloned(),
camera_mode,
character_state.cloned(),
)
} else { } else {
FigureKey::Simple(body) FigureKey::Simple(body)
}; };
@ -56,36 +67,69 @@ impl FigureModelCache {
HumHeadSpec::load_watched(&mut self.manifest_indicator); HumHeadSpec::load_watched(&mut self.manifest_indicator);
let bone_meshes = match body { let bone_meshes = match body {
Body::Humanoid(body) => [ Body::Humanoid(body) => [
match camera_mode.unwrap_or_default() {
if !first_person { CameraMode::ThirdPerson => {
Some(humanoid_head_spec.mesh_head(
Some(humanoid_head_spec.mesh_head( body.race,
body.race, body.body_type,
body.body_type, body.hair_color,
body.hair_color, body.hair_style,
body.hair_style, body.beard,
body.beard, body.eye_color,
body.eye_color, body.skin,
body.skin, body.eyebrows,
body.eyebrows, body.accessory,
body.accessory, ))
)) }
CameraMode::FirstPerson => None,
} else { },
None match camera_mode.unwrap_or_default() {
} CameraMode::ThirdPerson => Some(mesh_chest(body.chest)),
CameraMode::FirstPerson => None,
, },
Some(mesh_chest(body.chest)), match camera_mode.unwrap_or_default() {
Some(mesh_belt(body.belt)), CameraMode::ThirdPerson => Some(mesh_belt(body.belt)),
Some(mesh_pants(body.pants)), CameraMode::FirstPerson => None,
},
match camera_mode.unwrap_or_default() {
CameraMode::ThirdPerson => Some(mesh_pants(body.pants)),
CameraMode::FirstPerson => None,
},
Some(mesh_left_hand(body.hand)), Some(mesh_left_hand(body.hand)),
Some(mesh_right_hand(body.hand)), Some(mesh_right_hand(body.hand)),
Some(mesh_left_foot(body.foot)), match camera_mode.unwrap_or_default() {
Some(mesh_right_foot(body.foot)), CameraMode::ThirdPerson => Some(mesh_left_foot(body.foot)),
Some(mesh_main(equipment.and_then(|e| e.main.as_ref()))), CameraMode::FirstPerson => None,
Some(mesh_left_shoulder(body.shoulder)), },
Some(mesh_right_shoulder(body.shoulder)), match camera_mode.unwrap_or_default() {
CameraMode::ThirdPerson => Some(mesh_right_foot(body.foot)),
CameraMode::FirstPerson => None,
},
if camera_mode.unwrap_or_default() != CameraMode::FirstPerson
|| character_state
.map(|cs| {
cs.action.is_attack()
|| cs.action.is_block()
|| cs.action.is_wield()
})
.unwrap_or_default()
{
Some(mesh_main(equipment.and_then(|e| e.main.as_ref())))
} else {
None
},
match camera_mode.unwrap_or_default() {
CameraMode::ThirdPerson => {
Some(mesh_left_shoulder(body.shoulder))
}
CameraMode::FirstPerson => None,
},
match camera_mode.unwrap_or_default() {
CameraMode::ThirdPerson => {
Some(mesh_right_shoulder(body.shoulder))
}
CameraMode::FirstPerson => None,
},
Some(mesh_draw()), Some(mesh_draw()),
None, None,
None, None,

View File

@ -116,7 +116,14 @@ impl FigureMgr {
let skeleton_attr = &self let skeleton_attr = &self
.model_cache .model_cache
.get_or_create_model(renderer, *body, stats.map(|s| &s.equipment), tick, false, false) .get_or_create_model(
renderer,
*body,
stats.map(|s| &s.equipment),
tick,
None,
None,
)
.1; .1;
match body { match body {
@ -382,25 +389,33 @@ impl FigureMgr {
.map(|state| (state.locals(), state.bone_consts())), .map(|state| (state.locals(), state.bone_consts())),
} { } {
// Don't render the player's body while in first person mode // Don't render the player's body while in first person mode
let fp = let player_camera_mode = if client
camera.get_mode() == CameraMode::FirstPerson
&& client
.state()
.read_storage::<Body>()
.get(client.entity())
.is_some()
&& entity == client.entity();
let gliding = client
.state() .state()
.read_storage::<common::comp::CharacterState>() .read_storage::<common::comp::Body>()
.get(client.entity()) .get(client.entity())
.unwrap_or(&common::comp::CharacterState::default()) .is_some()
.movement == common::comp::MovementState::Glide; && entity == client.entity()
{
Some(camera.get_mode())
} else {
None
};
let character_state_storage = client
.state()
.read_storage::<common::comp::CharacterState>();
let character_state = character_state_storage.get(client.entity());
let model = &self let model = &self
.model_cache .model_cache
.get_or_create_model(renderer, *body, stats.map(|s| &s.equipment), tick, fp, gliding) .get_or_create_model(
renderer,
*body,
stats.map(|s| &s.equipment),
tick,
player_camera_mode,
character_state,
)
.0; .0;
renderer.render_figure(model, globals, locals, bone_consts, lights); renderer.render_figure(model, globals, locals, bone_consts, lights);