Code quality

This commit is contained in:
Ludvig Böklin 2021-05-20 10:31:37 +02:00
parent 8da59bb8e8
commit fe186fb44d
2 changed files with 24 additions and 37 deletions

View File

@ -931,6 +931,11 @@ impl Client {
pub fn is_dead(&self) -> bool { self.current::<comp::Health>().map_or(false, |h| h.is_dead) }
pub fn is_gliding(&self) -> bool {
self.current::<comp::CharacterState>()
.map_or(false, |cs| matches!(cs, comp::CharacterState::Glide(_)))
}
pub fn split_swap_slots(&mut self, a: comp::slot::Slot, b: comp::slot::Slot) {
match (a, b) {
(Slot::Equip(equip), slot) | (slot, Slot::Equip(equip)) => self.control_action(
@ -1234,7 +1239,7 @@ impl Client {
}
pub fn toggle_glide(&mut self) {
let is_gliding = self
let using_glider = self
.state
.ecs()
.read_storage::<comp::CharacterState>()
@ -1246,7 +1251,7 @@ impl Client {
)
});
match is_gliding {
match using_glider {
Some(true) => self.control_action(ControlAction::Unwield),
Some(false) => self.control_action(ControlAction::GlideWield),
None => warn!("Can't toggle glide, client entity doesn't have a `CharacterState`"),

View File

@ -686,20 +686,8 @@ impl PlayState for SessionState {
|b| hud.auto_walk(b),
);
if self
.client
.borrow()
.state()
.ecs()
.read_storage::<comp::CharacterState>()
.get(self.client.borrow().entity())
.filter(|cs| matches!(cs, comp::CharacterState::Glide(_)))
.is_some()
{
self.key_state.auto_walk = false
} else {
self.key_state.auto_walk = self.auto_walk
}
self.key_state.auto_walk =
self.auto_walk && !self.client.borrow().is_gliding();
},
GameInput::CameraClamp => {
let hud = &mut self.hud;
@ -764,30 +752,24 @@ impl PlayState for SessionState {
}
}
if let Some(dir) = self
.auto_walk
.then_some((
self.client.borrow().state().ecs(),
self.client.borrow().entity(),
))
.filter(|(ecs, entity)| {
ecs.read_storage::<comp::CharacterState>()
.get(*entity)
.map_or(false, |cs| matches!(cs, comp::CharacterState::Glide(_)))
})
.and_then(|(ecs, entity)| {
ecs.read_storage::<comp::PhysicsState>()
// If auto-gliding, point camera into the wind
if let Some(dir) = (self.auto_walk && self.client.borrow().is_gliding())
.then_some(self.client.borrow())
.and_then(|client| {
let ecs = client.state().ecs();
let entity = client.entity();
let fluid = ecs
.read_storage::<comp::PhysicsState>()
.get(entity)?
.in_fluid
.and_then(|fluid| {
Dir::from_unnormalized(
-fluid
.relative_flow(ecs.read_storage::<comp::Vel>().get(entity)?)
.0,
)
})
.in_fluid?;
let wind = ecs
.read_storage::<comp::Vel>()
.get(entity)
.map(|vel| -fluid.relative_flow(vel).0)?;
Dir::from_unnormalized(wind)
})
{
self.key_state.auto_walk = false;
self.inputs.look_dir = dir;
} else if !self.free_look {
self.walk_forward_dir = self.scene.camera().forward_xy();