Fix imports, update matches

This commit is contained in:
AdamWhitehurst 2020-01-08 11:31:42 -08:00
parent 5527d83a0e
commit de36e75264
19 changed files with 120 additions and 144 deletions

View File

@ -360,7 +360,7 @@ impl Client {
{
if last_character_states
.get(entity)
.map(|&l| !client_character_state.is_same_state(&l.0))
.map(|&l| !client_character_state.equals(&l.0))
.unwrap_or(true)
{
let _ = last_character_states

View File

@ -110,20 +110,18 @@ impl ActionState {
_ => false,
}
}
pub fn is_wielding(&self) -> bool {
if let Wield(_) = self {
true
} else {
false
}
/// Compares `action_state`s for shallow equality (does not check internal struct equality)
pub fn equals(&self, other: &Self) -> bool {
// Check if state is the same without looking at the inner data
std::mem::discriminant(&self) == std::mem::discriminant(&other)
}
pub fn is_idling(&self) -> bool {
if let Idle(_) = self {
true
} else {
false
}
}
impl MoveState {
/// Compares `move_state`s for shallow equality (does not check internal struct equality)
pub fn equals(&self, other: &Self) -> bool {
// Check if state is the same without looking at the inner data
std::mem::discriminant(&self) == std::mem::discriminant(&other)
}
}
@ -142,22 +140,10 @@ pub struct CharacterState {
}
impl CharacterState {
/// Compares `move_state`s for shallow equality (does not check internal struct equality)
pub fn is_same_move_state(&self, other: &Self) -> bool {
// Check if state is the same without looking at the inner data
std::mem::discriminant(&self.move_state) == std::mem::discriminant(&other.move_state)
}
/// Compares `action_state`s for shallow equality (does not check internal struct equality)
pub fn is_same_action_state(&self, other: &Self) -> bool {
// Check if state is the same without looking at the inner data
std::mem::discriminant(&self.action_state) == std::mem::discriminant(&other.action_state)
}
/// Compares both `move_state`s and `action_state`a for shallow equality
/// (does not check internal struct equality)
pub fn is_same_state(&self, other: &Self) -> bool {
self.is_same_move_state(other) && self.is_same_action_state(other)
pub fn equals(&self, other: &Self) -> bool {
self.move_state.equals(&other.move_state) && self.action_state.equals(&other.action_state)
}
}

View File

@ -1,4 +1,4 @@
use super::{ActionState::*, EcsStateData, MoveState::*, StateHandler, StateUpdate};
use super::{ActionState, EcsStateData, MoveState, StateHandler, StateUpdate};
use crate::sys::phys::GRAVITY;
use vek::vec::{Vec2, Vec3};
use vek::Lerp;
@ -22,18 +22,18 @@ impl StateHandler for State {
character: *ecs_data.character,
};
update.character.action_state = Idle(None);
update.character.action_state = ActionState::Idle(None);
// If no wall is in front of character ...
if let None = ecs_data.physics.on_wall {
if ecs_data.inputs.jump.is_pressed() {
// They've climbed atop something, give them a boost
update.character.move_state = Jump(None);
update.character.move_state = MoveState::Jump(None);
return update;
} else {
// Just fall off
update.character.move_state = Fall(None);
update.character.move_state = MoveState::Fall(None);
return update;
}
@ -41,7 +41,7 @@ impl StateHandler for State {
// Remove climb state on ground, otherwise character will get stuck
if ecs_data.physics.on_ground {
update.character.move_state = Stand(None);
update.character.move_state = MoveState::Stand(None);
return update;
}

View File

@ -1,4 +1,4 @@
use crate::comp::{EcsStateData, MoveState::*, StateHandler, StateUpdate};
use crate::comp::{ActionState, EcsStateData, MoveState, StateHandler, StateUpdate};
use crate::util::state_utils::*;
use vek::{Vec2, Vec3};
@ -32,13 +32,12 @@ impl StateHandler for State {
};
// Set orientation vector based on direction of movement when on the ground
let ori_dir = if update.character.action_state.is_attacking()
|| update.character.action_state.is_blocking()
{
Vec2::from(ecs_data.inputs.look_dir).normalized()
} else {
Vec2::from(update.vel.0)
};
let ori_dir =
if let ActionState::Attack(_) | ActionState::Block(_) = update.character.action_state {
Vec2::from(ecs_data.inputs.look_dir).normalized()
} else {
Vec2::from(update.vel.0)
};
if ori_dir.magnitude_squared() > 0.0001
&& (update.ori.0.normalized() - Vec3::from(ori_dir).normalized()).magnitude_squared()
@ -50,13 +49,13 @@ impl StateHandler for State {
// Check to start climbing
if can_climb(ecs_data.physics, ecs_data.inputs, ecs_data.body) {
update.character.move_state = Climb(None);
update.character.move_state = MoveState::Climb(None);
return update;
}
// Check gliding
if ecs_data.inputs.glide.is_pressed() {
update.character.move_state = Glide(None);
update.character.move_state = MoveState::Glide(None);
return update;
}

View File

@ -1,4 +1,4 @@
use crate::comp::{ActionState::*, EcsStateData, MoveState::*, StateHandler, StateUpdate};
use crate::comp::{ActionState, EcsStateData, MoveState, StateHandler, StateUpdate};
use vek::{Vec2, Vec3};
// Gravity is 9.81 * 4, so this makes gravity equal to .15
@ -23,24 +23,24 @@ impl StateHandler for State {
};
// Defaults for this state
update.character.action_state = Idle(None);
update.character.move_state = Glide(None);
update.character.action_state = ActionState::Idle(None);
update.character.move_state = MoveState::Glide(None);
// If glide button isn't held, start falling
if !ecs_data.inputs.glide.is_pressed() {
update.character.move_state = Fall(None);
update.character.move_state = MoveState::Fall(None);
return update;
}
// If there is a wall in front of character go to climb
if let Some(_wall_dir) = ecs_data.physics.on_wall {
update.character.move_state = Climb(None);
update.character.move_state = MoveState::Climb(None);
return update;
}
// If on ground go to stand
if ecs_data.physics.on_ground {
update.character.move_state = Stand(None);
update.character.move_state = MoveState::Stand(None);
return update;
}

View File

@ -1,4 +1,4 @@
use crate::comp::{ActionState::Wield, EcsStateData, ItemKind::Tool, StateHandler, StateUpdate};
use crate::comp::{ActionState, EcsStateData, ItemKind::Tool, StateHandler, StateUpdate};
#[derive(Clone, Copy, Default, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
pub struct State;
@ -23,7 +23,7 @@ impl StateHandler for State {
&& update.character.action_state.is_equip_finished())
{
if let Some(Tool(_)) = ecs_data.stats.equipment.main.as_ref().map(|i| &i.kind) {
update.character.action_state = Wield(None);
update.character.action_state = ActionState::Wield(None);
}
// else unarmed stuff?

View File

@ -1,4 +1,4 @@
use super::{EcsStateData, MoveState::*, StateHandler, StateUpdate};
use super::{EcsStateData, MoveState, StateHandler, StateUpdate};
use crate::event::LocalEvent;
#[derive(Clone, Copy, Default, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
@ -23,7 +23,7 @@ impl StateHandler for State {
.emit(LocalEvent::Jump(*ecs_data.entity));
// Immediately go to falling state after jump impulse
update.character.move_state = Fall(None);
update.character.move_state = MoveState::Fall(None);
return update;
}
}

View File

@ -1,5 +1,6 @@
use crate::comp::{
ActionState::*, DodgeKind::*, EcsStateData, ItemKind::Tool, StateHandler, StateUpdate, ToolData,
ActionState::Dodge, DodgeKind::Roll, EcsStateData, ItemKind::Tool, StateHandler, StateUpdate,
ToolData,
};
use crate::util::state_utils::*;
use std::time::Duration;

View File

@ -1,4 +1,4 @@
use crate::comp::{EcsStateData, MoveState::*, StateHandler, StateUpdate};
use crate::comp::{ActionState, EcsStateData, MoveState, StateHandler, StateUpdate};
use crate::util::state_utils::*;
use vek::vec::{Vec2, Vec3};
@ -31,13 +31,12 @@ impl StateHandler for State {
};
// Set direction based on move direction when on the ground
let ori_dir = if update.character.action_state.is_attacking()
|| update.character.action_state.is_blocking()
{
Vec2::from(ecs_data.inputs.look_dir).normalized()
} else {
Vec2::from(update.vel.0)
};
let ori_dir =
if let ActionState::Attack(_) | ActionState::Block(_) = update.character.action_state {
Vec2::from(ecs_data.inputs.look_dir).normalized()
} else {
Vec2::from(update.vel.0)
};
if ori_dir.magnitude_squared() > 0.0001
&& (update.ori.0.normalized() - Vec3::from(ori_dir).normalized()).magnitude_squared()
@ -49,25 +48,25 @@ impl StateHandler for State {
// Try to sit
if can_sit(ecs_data.physics, ecs_data.inputs, ecs_data.body) {
update.character.move_state = Sit(None);
update.character.move_state = MoveState::Sit(None);
return update;
}
// Try to climb
if can_climb(ecs_data.physics, ecs_data.inputs, ecs_data.body) {
update.character.move_state = Climb(None);
update.character.move_state = MoveState::Climb(None);
return update;
}
// Try to jump
if can_jump(ecs_data.physics, ecs_data.inputs) {
update.character.move_state = Jump(None);
update.character.move_state = MoveState::Jump(None);
return update;
}
// Try to glide
if can_glide(ecs_data.physics, ecs_data.inputs, ecs_data.body) {
update.character.move_state = Glide(None);
update.character.move_state = MoveState::Glide(None);
return update;
}

View File

@ -1,4 +1,4 @@
use crate::comp::{ActionState::*, EcsStateData, MoveState::*, StateHandler, StateUpdate};
use crate::comp::{ActionState, EcsStateData, MoveState, StateHandler, StateUpdate};
use crate::util::state_utils::*;
#[derive(Clone, Copy, Default, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
@ -18,8 +18,8 @@ impl StateHandler for State {
};
// Prevent action state handling
update.character.action_state = Idle(None);
update.character.move_state = Sit(None);
update.character.action_state = ActionState::Idle(None);
update.character.move_state = MoveState::Sit(None);
// Try to Fall
// ... maybe the ground disappears,
@ -31,19 +31,19 @@ impl StateHandler for State {
}
// Try to jump
if ecs_data.inputs.jump.is_pressed() {
update.character.move_state = Jump(None);
update.character.move_state = MoveState::Jump(None);
return update;
}
// Try to Run
if ecs_data.inputs.move_dir.magnitude_squared() > 0.0 {
update.character.move_state = Run(None);
update.character.move_state = MoveState::Run(None);
return update;
}
// Try to Stand
if ecs_data.inputs.sit.is_just_pressed() {
update.character.move_state = Stand(None);
update.character.move_state = MoveState::Stand(None);
return update;
}

View File

@ -1,4 +1,4 @@
use crate::comp::{EcsStateData, MoveState::*, StateHandler, StateUpdate};
use crate::comp::{EcsStateData, MoveState, StateHandler, StateUpdate};
use crate::util::state_utils::*;
#[derive(Clone, Copy, Default, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
@ -19,25 +19,25 @@ impl StateHandler for State {
// Try to sit
if can_sit(ecs_data.physics, ecs_data.inputs, ecs_data.body) {
update.character.move_state = Sit(None);
update.character.move_state = MoveState::Sit(None);
return update;
}
// Try to climb
if can_climb(ecs_data.physics, ecs_data.inputs, ecs_data.body) {
update.character.move_state = Climb(None);
update.character.move_state = MoveState::Climb(None);
return update;
}
// Try to jump
if can_jump(ecs_data.physics, ecs_data.inputs) {
update.character.move_state = Jump(None);
update.character.move_state = MoveState::Jump(None);
return update;
}
// Check gliding
if can_glide(ecs_data.physics, ecs_data.inputs, ecs_data.body) {
update.character.move_state = Glide(None);
update.character.move_state = MoveState::Glide(None);
return update;
}

View File

@ -1,4 +1,4 @@
use crate::comp::{EcsStateData, MoveState::*, StateHandler, StateUpdate};
use crate::comp::{ActionState, EcsStateData, MoveState, StateHandler, StateUpdate};
use crate::sys::phys::GRAVITY;
use std::time::Duration;
use vek::{Vec2, Vec3};
@ -32,13 +32,12 @@ impl StateHandler for State {
};
// Set direction based on move direction when on the ground
let ori_dir = if update.character.action_state.is_attacking()
|| update.character.action_state.is_blocking()
{
Vec2::from(ecs_data.inputs.look_dir).normalized()
} else {
Vec2::from(update.vel.0)
};
let ori_dir =
if let ActionState::Attack(_) | ActionState::Block(_) = update.character.action_state {
Vec2::from(ecs_data.inputs.look_dir).normalized()
} else {
Vec2::from(update.vel.0)
};
if ori_dir.magnitude_squared() > 0.0001
&& (update.ori.0.normalized() - Vec3::from(ori_dir).normalized()).magnitude_squared()
@ -64,16 +63,16 @@ impl StateHandler for State {
// Not on ground
if !ecs_data.physics.on_ground {
update.character.move_state = Swim(None);
update.character.move_state = MoveState::Swim(None);
return update;
}
// On ground
else {
// Return to running or standing based on move inputs
update.character.move_state = if ecs_data.inputs.move_dir.magnitude_squared() > 0.0 {
Run(None)
MoveState::Run(None)
} else {
Stand(None)
MoveState::Stand(None)
};
return update;

View File

@ -1,6 +1,4 @@
use crate::comp::{
ActionState::*, EcsStateData, ItemKind::Tool, StateHandler, StateUpdate, ToolData,
};
use crate::comp::{ActionState, EcsStateData, ItemKind::Tool, StateHandler, StateUpdate, ToolData};
use std::time::Duration;
#[derive(Clone, Copy, Default, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
@ -37,7 +35,7 @@ impl StateHandler for State {
if ecs_data.inputs.toggle_wield.is_just_pressed()
&& ecs_data.character.action_state.is_equip_finished()
{
update.character.action_state = Idle(None);
update.character.action_state = ActionState::Idle(None);
return update;
}
@ -54,7 +52,7 @@ impl StateHandler for State {
} else {
// Equip delay hasn't expired yet
// Update wield delay
update.character.action_state = Wield(Some(State {
update.character.action_state = ActionState::Wield(Some(State {
equip_delay: self
.equip_delay
.checked_sub(Duration::from_secs_f32(ecs_data.dt.0))

View File

@ -1,33 +1,25 @@
use crate::comp::{
ActionState, ActionState::*, AttackKind::*, BasicAttackState, BasicBlockState, BlockKind::*,
Body, ControllerInputs, FallState, IdleState, ItemKind::Tool, MoveState, MoveState::*,
PhysicsState, RunState, StandState, Stats, SwimState, WieldState,
ActionState, ActionState::*, AttackKind::*, BlockKind::*, Body, ControllerInputs,
ItemKind::Tool, MoveState, MoveState::*, PhysicsState, Stats,
};
use std::time::Duration;
/// _Determines what ability a player has selected for their primary ability,
/// and returns the corresponding `ActionState`
/// ... or Idle if nothing it possible?_
/// and returns the corresponding `ActionState` or Idle if nothing_
pub fn determine_primary_ability(stats: &Stats) -> ActionState {
if let Some(Tool(data)) = stats.equipment.main.as_ref().map(|i| &i.kind) {
Attack(BasicAttack(Some(BasicAttackState {
remaining_duration: data.attack_duration(),
})))
if let Some(Tool(_)) = stats.equipment.main.as_ref().map(|i| &i.kind) {
Attack(BasicAttack(None))
} else {
Idle(Some(IdleState))
Idle(None)
}
}
/// _Determines what ability a player has selected for their primary ability,
/// and returns the corresponding `ActionState`
/// ... or Idle if nothing it possible?_
/// and returns the corresponding `ActionState` or Idle if nothing_
pub fn determine_secondary_ability(stats: &Stats) -> ActionState {
if let Some(Tool(_data)) = stats.equipment.main.as_ref().map(|i| &i.kind) {
Block(BasicBlock(Some(BasicBlockState {
active_duration: Duration::default(),
})))
if let Some(Tool(_)) = stats.equipment.main.as_ref().map(|i| &i.kind) {
Block(BasicBlock(None))
} else {
Idle(Some(IdleState))
Idle(None)
}
}
@ -35,18 +27,18 @@ pub fn determine_secondary_ability(stats: &Stats) -> ActionState {
pub fn determine_fall_or_swim(physics: &PhysicsState) -> MoveState {
// Check if in fluid to go to swimming or back to falling
if physics.in_fluid {
Swim(Some(SwimState))
Swim(None)
} else {
Fall(Some(FallState))
Fall(None)
}
}
/// _Returns a `MoveState` based on `move_dir` magnitude_
pub fn determine_stand_or_run(inputs: &ControllerInputs) -> MoveState {
// Return to running or standing based on move inputs
if inputs.move_dir.magnitude_squared() > 0.0 {
Run(Some(RunState))
Run(None)
} else {
Stand(Some(StandState))
Stand(None)
}
}
@ -71,11 +63,9 @@ pub fn determine_move_from_grounded_state(
/// _Returns an ActionState based on whether character has a weapon equipped._
pub fn attempt_wield(stats: &Stats) -> ActionState {
if let Some(Tool(data)) = stats.equipment.main.as_ref().map(|i| &i.kind) {
Wield(Some(WieldState {
equip_delay: data.equip_time(),
}))
Wield(None)
} else {
Idle(Some(IdleState))
Idle(None)
}
}

View File

@ -238,7 +238,7 @@ impl<'a> System<'a> for Sys {
if let Some(&character_state) = character_state {
if last_character_state
.get(entity)
.map(|&l| !character_state.is_same_state(&l.0))
.map(|&l| !character_state.equals(&l.0))
.unwrap_or(true)
{
let _ = last_character_state.insert(entity, Last(character_state));

View File

@ -5,9 +5,8 @@ use crate::audio::sfx::{SfxTriggerItem, SfxTriggers};
use client::Client;
use common::{
comp::{
ActionState, AttackKind::*, BasicAttackState, Body, CharacterState, DodgeKind::*,
FallState, GlideState, IdleState, ItemKind, MoveState, Pos, RollState, RunState,
StandState, Stats, SwordKind::*, ToolData, ToolKind::*,
ActionState, AttackKind::*, Body, CharacterState, DodgeKind::*, ItemKind, MoveState, Pos,
Stats, SwordKind::*, ToolData, ToolKind::*,
},
event::{EventBus, SfxEvent, SfxEventItem},
};
@ -325,8 +324,8 @@ mod tests {
let result = SfxEventMapper::map_character_event(
&CharacterState {
move_state: MoveState::Fall((None)),
action_state: ActionState::Idle((None)),
move_state: MoveState::Fall(None),
action_state: ActionState::Idle(None),
},
SfxEvent::Idle,
&stats,

View File

@ -161,10 +161,11 @@ impl FigureModelCache {
},
if camera_mode != CameraMode::FirstPerson
|| character_state
.map(|cs| {
cs.action_state.is_attacking()
|| cs.action_state.is_blocking()
|| cs.action_state.is_wielding()
.map(|cs| match cs.action_state {
ActionState::Attack(_)
| ActionState::Block(_)
| ActionState::Wield(_) => true,
_ => false,
})
.unwrap_or_default()
{

View File

@ -189,10 +189,13 @@ impl FigureMgr {
_ => continue,
};
if !character.is_same_move_state(&last_character.0) {
if !character.move_state.equals(&last_character.0.move_state) {
state.move_state_time = 0.0;
}
if !character.is_same_action_state(&last_character.0) {
if !character
.action_state
.equals(&last_character.0.action_state)
{
state.action_time = 0.0;
}
@ -332,7 +335,7 @@ impl FigureMgr {
_ => continue,
};
if !character.is_same_move_state(&last_character.0) {
if !character.move_state.equals(&last_character.0.move_state) {
state.move_state_time = 0.0;
}
@ -389,7 +392,7 @@ impl FigureMgr {
_ => continue,
};
if !character.is_same_move_state(&last_character.0) {
if !character.move_state.equals(&last_character.0.move_state) {
state.move_state_time = 0.0;
}
@ -444,7 +447,7 @@ impl FigureMgr {
_ => continue,
};
if !character.is_same_move_state(&last_character.0) {
if !character.move_state.equals(&last_character.0.move_state) {
state.move_state_time = 0.0;
}
@ -499,7 +502,7 @@ impl FigureMgr {
_ => continue,
};
if !character.is_same_move_state(&last_character.0) {
if !character.move_state.equals(&last_character.0.move_state) {
state.move_state_time = 0.0;
}
@ -554,7 +557,7 @@ impl FigureMgr {
_ => continue,
};
if !character.is_same_move_state(&last_character.0) {
if !character.move_state.equals(&last_character.0.move_state) {
state.move_state_time = 0.0;
}
@ -609,7 +612,7 @@ impl FigureMgr {
_ => continue,
};
if !character.is_same_move_state(&last_character.0) {
if !character.move_state.equals(&last_character.0.move_state) {
state.move_state_time = 0.0;
}
@ -664,7 +667,7 @@ impl FigureMgr {
_ => continue,
};
if !character.is_same_move_state(&last_character.0) {
if !character.move_state.equals(&last_character.0.move_state) {
state.move_state_time = 0.0;
}
@ -719,7 +722,7 @@ impl FigureMgr {
_ => continue,
};
if !character.is_same_move_state(&last_character.0) {
if !character.move_state.equals(&last_character.0.move_state) {
state.move_state_time = 0.0;
}

View File

@ -10,7 +10,7 @@ use client::{self, Client, Event::Chat};
use common::{
clock::Clock,
comp,
comp::{Pos, Vel},
comp::{ActionState, Pos, Vel},
msg::ClientState,
terrain::{Block, BlockKind},
vol::ReadVol,
@ -201,10 +201,11 @@ impl PlayState for SessionState {
.state()
.read_storage::<comp::CharacterState>()
.get(client.entity())
.map(|cs| {
cs.action_state.is_wielding()
|| cs.action_state.is_blocking()
|| cs.action_state.is_attacking()
.map(|cs| match cs.action_state {
ActionState::Attack(_)
| ActionState::Block(_)
| ActionState::Wield(_) => true,
_ => false,
})
.unwrap_or(false)
{