2020-02-03 10:54:50 +00:00
|
|
|
use crate::{
|
|
|
|
comp::{CharacterState, EcsStateData, ItemKind::Tool, StateUpdate},
|
|
|
|
event::LocalEvent,
|
2019-12-28 16:10:39 +00:00
|
|
|
};
|
2020-01-21 22:54:32 +00:00
|
|
|
use vek::vec::{Vec2, Vec3};
|
2019-12-28 16:10:39 +00:00
|
|
|
|
2020-01-21 22:54:32 +00:00
|
|
|
pub fn handle_move_dir(ecs_data: &EcsStateData, update: &mut StateUpdate) {
|
|
|
|
let (accel, speed): (f32, f32) = if ecs_data.physics.on_ground {
|
2020-02-03 19:43:36 +00:00
|
|
|
let accel = 100.0;
|
|
|
|
let speed = 8.0;
|
2020-01-21 22:54:32 +00:00
|
|
|
(accel, speed)
|
|
|
|
} else {
|
2020-02-03 19:43:36 +00:00
|
|
|
let accel = 100.0;
|
|
|
|
let speed = 8.0;
|
2020-01-21 22:54:32 +00:00
|
|
|
(accel, speed)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Move player according to move_dir
|
2020-02-03 19:43:36 +00:00
|
|
|
if update.vel.0.magnitude_squared() < speed.powf(2.0) {
|
|
|
|
update.vel.0 =
|
|
|
|
update.vel.0 + Vec2::broadcast(ecs_data.dt.0) * ecs_data.inputs.move_dir * accel;
|
|
|
|
let mag2 = update.vel.0.magnitude_squared();
|
|
|
|
if mag2 > speed.powf(2.0) {
|
|
|
|
update.vel.0 = update.vel.0.normalized() * speed;
|
|
|
|
}
|
|
|
|
}
|
2020-01-21 22:54:32 +00:00
|
|
|
|
|
|
|
// Set direction based on move direction
|
|
|
|
let ori_dir = if update.character.is_attack() || update.character.is_block() {
|
|
|
|
Vec2::from(ecs_data.inputs.look_dir).normalized()
|
|
|
|
} else {
|
|
|
|
Vec2::from(update.vel.0)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Smooth orientation
|
|
|
|
if ori_dir.magnitude_squared() > 0.0001
|
|
|
|
&& (update.ori.0.normalized() - Vec3::from(ori_dir).normalized()).magnitude_squared()
|
|
|
|
> 0.001
|
|
|
|
{
|
|
|
|
update.ori.0 = vek::ops::Slerp::slerp(update.ori.0, ori_dir.into(), 9.0 * ecs_data.dt.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_wield(ecs_data: &EcsStateData, update: &mut StateUpdate) {
|
|
|
|
if ecs_data.inputs.primary.is_pressed() || ecs_data.inputs.secondary.is_pressed() {
|
|
|
|
if let Some(Tool(_)) = ecs_data.stats.equipment.main.as_ref().map(|i| &i.kind) {
|
|
|
|
update.character = CharacterState::Wielding(None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_sit(ecs_data: &EcsStateData, update: &mut StateUpdate) {
|
|
|
|
if ecs_data.inputs.sit.is_pressed() && ecs_data.physics.on_ground && ecs_data.body.is_humanoid()
|
|
|
|
{
|
|
|
|
update.character = CharacterState::Sit(None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_climb(ecs_data: &EcsStateData, update: &mut StateUpdate) {
|
2020-02-03 19:43:36 +00:00
|
|
|
if (ecs_data.inputs.climb.is_just_pressed() || ecs_data.inputs.climb_down.is_pressed())
|
2020-01-21 22:54:32 +00:00
|
|
|
&& ecs_data.physics.on_wall.is_some()
|
2020-02-03 19:43:36 +00:00
|
|
|
&& !ecs_data.physics.on_ground
|
|
|
|
//&& update.vel.0.z < 0.0
|
2020-01-21 22:54:32 +00:00
|
|
|
&& ecs_data.body.is_humanoid()
|
|
|
|
{
|
|
|
|
update.character = CharacterState::Climb(None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_unwield(ecs_data: &EcsStateData, update: &mut StateUpdate) {
|
|
|
|
if let CharacterState::Wielded(_) = update.character {
|
|
|
|
if ecs_data.inputs.toggle_wield.is_pressed() {
|
|
|
|
update.character = CharacterState::Idle(None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_glide(ecs_data: &EcsStateData, update: &mut StateUpdate) {
|
2020-02-03 10:54:50 +00:00
|
|
|
if let CharacterState::Idle(_) | CharacterState::Wielded(_) = update.character {
|
|
|
|
if ecs_data.inputs.glide.is_pressed()
|
|
|
|
&& !ecs_data.physics.on_ground
|
|
|
|
&& ecs_data.body.is_humanoid()
|
|
|
|
{
|
|
|
|
update.character = CharacterState::Glide(None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_jump(ecs_data: &EcsStateData, update: &mut StateUpdate) {
|
|
|
|
if ecs_data.inputs.jump.is_pressed() && ecs_data.physics.on_ground {
|
|
|
|
update
|
|
|
|
.local_events
|
|
|
|
.push_front(LocalEvent::Jump(*ecs_data.entity));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_primary(ecs_data: &EcsStateData, update: &mut StateUpdate) {
|
|
|
|
if let Some(state) = ecs_data.ability_pool.primary {
|
|
|
|
if let CharacterState::Wielded(_) = update.character {
|
|
|
|
if ecs_data.inputs.primary.is_pressed() {
|
|
|
|
update.character = state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_dodge(ecs_data: &EcsStateData, update: &mut StateUpdate) {
|
|
|
|
if let Some(state) = ecs_data.ability_pool.dodge {
|
|
|
|
if let CharacterState::Idle(_) | CharacterState::Wielded(_) = update.character {
|
|
|
|
if ecs_data.inputs.roll.is_pressed()
|
|
|
|
&& ecs_data.physics.on_ground
|
|
|
|
&& ecs_data.body.is_humanoid()
|
|
|
|
{
|
|
|
|
update.character = state;
|
|
|
|
}
|
|
|
|
}
|
2020-01-21 22:54:32 +00:00
|
|
|
}
|
2019-12-28 16:10:39 +00:00
|
|
|
}
|