2020-03-16 11:32:57 +00:00
|
|
|
use crate::{
|
2020-03-24 19:31:54 +00:00
|
|
|
comp::{Body, CharacterState, Gravity, LightEmitter, Projectile, StateUpdate},
|
2020-03-16 11:32:57 +00:00
|
|
|
event::ServerEvent,
|
2020-03-20 14:45:36 +00:00
|
|
|
states::utils::*,
|
2020-09-30 00:47:11 +00:00
|
|
|
sys::character_behavior::{CharacterBehavior, JoinData},
|
2020-03-16 11:32:57 +00:00
|
|
|
};
|
2020-07-06 14:23:08 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2020-03-21 21:16:26 +00:00
|
|
|
use std::time::Duration;
|
2020-03-16 11:32:57 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct Data {
|
2020-08-25 12:21:25 +00:00
|
|
|
/// Can you hold the ability beyond the prepare duration
|
2020-03-26 21:52:44 +00:00
|
|
|
pub holdable: bool,
|
2020-03-24 13:42:31 +00:00
|
|
|
/// How long we have to prepare the weapon
|
|
|
|
pub prepare_duration: Duration,
|
2020-03-16 11:32:57 +00:00
|
|
|
/// How long we prepared the weapon already
|
|
|
|
pub prepare_timer: Duration,
|
|
|
|
/// How long the state has until exiting
|
|
|
|
pub recover_duration: Duration,
|
|
|
|
pub projectile: Projectile,
|
|
|
|
pub projectile_body: Body,
|
2020-03-24 19:31:54 +00:00
|
|
|
pub projectile_light: Option<LightEmitter>,
|
|
|
|
pub projectile_gravity: Option<Gravity>,
|
2020-08-06 18:17:38 +00:00
|
|
|
pub projectile_speed: f32,
|
2020-03-16 11:32:57 +00:00
|
|
|
/// Whether the attack fired already
|
|
|
|
pub exhausted: bool,
|
2020-10-08 02:12:44 +00:00
|
|
|
/// What key is used to press ability
|
|
|
|
pub ability_key: AbilityKey,
|
2020-03-16 11:32:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CharacterBehavior for Data {
|
|
|
|
fn behavior(&self, data: &JoinData) -> StateUpdate {
|
2020-03-21 21:16:26 +00:00
|
|
|
let mut update = StateUpdate::from(data);
|
2020-03-16 11:32:57 +00:00
|
|
|
|
2020-03-27 17:17:41 +00:00
|
|
|
handle_move(data, &mut update, 0.3);
|
2020-03-16 11:32:57 +00:00
|
|
|
handle_jump(data, &mut update);
|
|
|
|
|
2020-07-07 00:01:39 +00:00
|
|
|
if !self.exhausted
|
|
|
|
&& if self.holdable {
|
2020-10-08 02:12:44 +00:00
|
|
|
ability_key_is_pressed(data, self.ability_key)
|
|
|
|
|| self.prepare_timer < self.prepare_duration
|
2020-07-07 00:01:39 +00:00
|
|
|
} else {
|
|
|
|
self.prepare_timer < self.prepare_duration
|
|
|
|
}
|
|
|
|
{
|
2020-03-16 11:32:57 +00:00
|
|
|
// Prepare (draw the bow)
|
|
|
|
update.character = CharacterState::BasicRanged(Data {
|
|
|
|
prepare_timer: self.prepare_timer + Duration::from_secs_f32(data.dt.0),
|
2020-03-26 21:52:44 +00:00
|
|
|
holdable: self.holdable,
|
2020-03-24 13:42:31 +00:00
|
|
|
prepare_duration: self.prepare_duration,
|
2020-03-16 11:32:57 +00:00
|
|
|
recover_duration: self.recover_duration,
|
|
|
|
projectile: self.projectile.clone(),
|
|
|
|
projectile_body: self.projectile_body,
|
2020-03-24 19:31:54 +00:00
|
|
|
projectile_light: self.projectile_light,
|
|
|
|
projectile_gravity: self.projectile_gravity,
|
2020-08-06 18:17:38 +00:00
|
|
|
projectile_speed: self.projectile_speed,
|
2020-03-16 11:32:57 +00:00
|
|
|
exhausted: false,
|
2020-10-08 02:12:44 +00:00
|
|
|
ability_key: self.ability_key,
|
2020-03-16 11:32:57 +00:00
|
|
|
});
|
|
|
|
} else if !self.exhausted {
|
|
|
|
// Fire
|
2020-03-17 13:15:39 +00:00
|
|
|
let mut projectile = self.projectile.clone();
|
2020-07-25 23:57:04 +00:00
|
|
|
projectile.owner = Some(*data.uid);
|
2020-03-16 11:32:57 +00:00
|
|
|
update.server_events.push_front(ServerEvent::Shoot {
|
|
|
|
entity: data.entity,
|
|
|
|
dir: data.inputs.look_dir,
|
|
|
|
body: self.projectile_body,
|
2020-03-17 13:15:39 +00:00
|
|
|
projectile,
|
2020-03-24 19:31:54 +00:00
|
|
|
light: self.projectile_light,
|
|
|
|
gravity: self.projectile_gravity,
|
2020-08-06 18:17:38 +00:00
|
|
|
speed: self.projectile_speed,
|
2020-03-16 11:32:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
update.character = CharacterState::BasicRanged(Data {
|
|
|
|
prepare_timer: self.prepare_timer,
|
2020-03-26 21:52:44 +00:00
|
|
|
holdable: self.holdable,
|
2020-03-24 13:42:31 +00:00
|
|
|
prepare_duration: self.prepare_duration,
|
2020-03-16 11:32:57 +00:00
|
|
|
recover_duration: self.recover_duration,
|
|
|
|
projectile: self.projectile.clone(),
|
|
|
|
projectile_body: self.projectile_body,
|
2020-03-24 19:31:54 +00:00
|
|
|
projectile_light: self.projectile_light,
|
|
|
|
projectile_gravity: self.projectile_gravity,
|
2020-08-06 18:17:38 +00:00
|
|
|
projectile_speed: self.projectile_speed,
|
2020-03-16 11:32:57 +00:00
|
|
|
exhausted: true,
|
2020-10-08 02:12:44 +00:00
|
|
|
ability_key: self.ability_key,
|
2020-03-16 11:32:57 +00:00
|
|
|
});
|
|
|
|
} else if self.recover_duration != Duration::default() {
|
|
|
|
// Recovery
|
|
|
|
update.character = CharacterState::BasicRanged(Data {
|
|
|
|
prepare_timer: self.prepare_timer,
|
2020-03-26 21:52:44 +00:00
|
|
|
holdable: self.holdable,
|
2020-03-24 13:42:31 +00:00
|
|
|
prepare_duration: self.prepare_duration,
|
2020-03-16 11:32:57 +00:00
|
|
|
recover_duration: self
|
|
|
|
.recover_duration
|
|
|
|
.checked_sub(Duration::from_secs_f32(data.dt.0))
|
|
|
|
.unwrap_or_default(),
|
|
|
|
projectile: self.projectile.clone(),
|
|
|
|
projectile_body: self.projectile_body,
|
2020-03-24 19:31:54 +00:00
|
|
|
projectile_light: self.projectile_light,
|
|
|
|
projectile_gravity: self.projectile_gravity,
|
2020-08-06 18:17:38 +00:00
|
|
|
projectile_speed: self.projectile_speed,
|
2020-03-16 11:32:57 +00:00
|
|
|
exhausted: true,
|
2020-10-08 02:12:44 +00:00
|
|
|
ability_key: self.ability_key,
|
2020-03-16 11:32:57 +00:00
|
|
|
});
|
|
|
|
return update;
|
|
|
|
} else {
|
|
|
|
// Done
|
|
|
|
update.character = CharacterState::Wielding;
|
|
|
|
}
|
|
|
|
|
|
|
|
update
|
|
|
|
}
|
|
|
|
}
|