feat: hitting enemies with basic_attack gives energy

This commit is contained in:
timokoesters 2020-03-10 18:54:59 +01:00
parent c88c4687f3
commit 70027da9aa
8 changed files with 34 additions and 11 deletions

View File

@ -115,6 +115,8 @@ impl Component for CharacterState {
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
pub struct Attacking {
pub weapon: Option<ToolData>,
pub applied: bool,
pub hit_count: u32,
}
impl Component for Attacking {

View File

@ -15,6 +15,7 @@ pub enum EnergySource {
Roll,
Climb,
LevelUp,
HitEnemy,
Regen,
Revive,
Unknown,

View File

@ -1,5 +1,5 @@
use crate::{
comp::{Attacking, CharacterState, ItemKind::Tool, StateUpdate},
comp::{Attacking, CharacterState, EnergySource, ItemKind::Tool, StateUpdate},
states::utils::*,
sys::character_behavior::JoinData,
};
@ -35,20 +35,26 @@ pub fn behavior(data: &JoinData) -> StateUpdate {
if can_apply_damage {
if let Some(Tool(tool)) = tool_kind {
data.updater
.insert(data.entity, Attacking { weapon: Some(tool) });
} else {
data.updater.insert(data.entity, Attacking { weapon: None });
data.updater.insert(data.entity, Attacking {
weapon: Some(tool),
applied: false,
hit_count: 0,
});
}
new_exhausted = true;
} else {
data.updater.remove::<Attacking>(data.entity);
}
let new_remaining_duration = remaining_duration
.checked_sub(Duration::from_secs_f32(data.dt.0))
.unwrap_or_default();
if let Some(attack) = data.attacking {
if attack.applied && attack.hit_count > 0 {
data.updater.remove::<Attacking>(data.entity);
update.energy.change_by(100, EnergySource::HitEnemy);
}
}
// Tick down
update.character = CharacterState::BasicAttack {
remaining_duration: new_remaining_duration,

View File

@ -39,6 +39,7 @@ pub fn behavior(data: &JoinData) -> StateUpdate {
if *remaining_duration == Duration::default() {
// Roll duration has expired
update.vel.0 *= 0.3;
update.character = CharacterState::Idle {};
} else {
// Otherwise, tick down remaining_duration

View File

@ -37,6 +37,8 @@ pub fn behavior(data: &JoinData) -> StateUpdate {
// Try to deal damage
data.updater.insert(data.entity, Attacking {
weapon: Some(*tool),
applied: false,
hit_count: 0,
});
new_stage_exhausted = true;
} else {

View File

@ -58,7 +58,7 @@ fn basic_move(data: &JoinData, update: &mut StateUpdate) {
{
Vec2::from(data.inputs.look_dir).normalized()
} else {
Vec2::from(update.vel.0)
Vec2::from(data.inputs.move_dir)
};
// Smooth orientation

View File

@ -1,7 +1,7 @@
use crate::{
comp::{
AbilityPool, Body, CharacterState, Controller, ControllerInputs, Energy, Mounting, Ori,
PhysicsState, Pos, Stats, Vel,
AbilityPool, Attacking, Body, CharacterState, Controller, ControllerInputs, Energy,
Mounting, Ori, PhysicsState, Pos, Stats, Vel,
},
event::{EventBus, LocalEvent, ServerEvent},
state::DeltaTime,
@ -29,6 +29,7 @@ pub struct JoinData<'a> {
pub body: &'a Body,
pub physics: &'a PhysicsState,
pub ability_pool: &'a AbilityPool,
pub attacking: Option<&'a Attacking>,
pub updater: &'a LazyUpdate,
}
@ -45,6 +46,7 @@ pub type JoinTuple<'a> = (
&'a Body,
&'a PhysicsState,
&'a AbilityPool,
Option<&'a Attacking>,
);
impl<'a> JoinData<'a> {
@ -63,6 +65,7 @@ impl<'a> JoinData<'a> {
body: j.9,
physics: j.10,
ability_pool: j.11,
attacking: j.12,
updater,
dt,
}
@ -95,6 +98,7 @@ impl<'a> System<'a> for Sys {
ReadStorage<'a, Body>,
ReadStorage<'a, PhysicsState>,
ReadStorage<'a, AbilityPool>,
ReadStorage<'a, Attacking>,
ReadStorage<'a, Uid>,
ReadStorage<'a, Mounting>,
);
@ -118,6 +122,7 @@ impl<'a> System<'a> for Sys {
bodies,
physics_states,
ability_pools,
attacking_storage,
uids,
mountings,
): Self::SystemData,
@ -135,6 +140,7 @@ impl<'a> System<'a> for Sys {
&bodies,
&physics_states,
&ability_pools,
attacking_storage.maybe(),
)
.join();

View File

@ -68,6 +68,11 @@ impl<'a> System<'a> for Sys {
)
.join()
{
if attack.applied {
continue;
}
attack.applied = true;
// Go through all other entities
for (b, uid_b, pos_b, ori_b, scale_b_maybe, character_b, stats_b, body_b) in (
&entities,
@ -115,9 +120,9 @@ impl<'a> System<'a> for Sys {
cause: HealthSource::Attack { by: *uid },
},
});
attack.hit_count += 1;
}
}
}
attacking_storage.clear();
}
}