diff options
author | Alexander Foremny <aforemny@posteo.de> | 2024-03-15 16:31:03 +0100 |
---|---|---|
committer | Alexander Foremny <aforemny@posteo.de> | 2024-03-15 16:35:32 +0100 |
commit | 1812cafee45128d823a00fd19efb736aa1306b28 (patch) | |
tree | a97727b51dc1c2ad35ce084bab20d72101e63ad6 | |
parent | 9114f5670876223d37f8338f3acc6a45530bf654 (diff) |
fix: attack in the same tick as approaching
-rw-r--r-- | src/server.rs | 47 |
1 files changed, 42 insertions, 5 deletions
diff --git a/src/server.rs b/src/server.rs index 1b9b7ef..1fe20a4 100644 --- a/src/server.rs +++ b/src/server.rs @@ -55,7 +55,9 @@ impl Plugin for ServerPlugin { .add_systems( Update, ( - imperative_attack.after(cooldown_decrement), + (imperative_attack_approach, imperative_attack_attack) + .chain() + .after(cooldown_decrement), imperative_walk_to, ) .after(player_input), @@ -187,10 +189,8 @@ fn move_to_target(position: Vec2, target_position: Vec2, max_distance: f32) -> ( } } -fn imperative_attack( +fn imperative_attack_approach( entity_map: Res<EntityMap>, - mut commands: Commands, - mut cooldowns: Query<&mut Cooldown>, mut players: Query<(&PlayerId, &mut Imperative)>, mut positions: Query<&mut PlayerPosition>, champions: Query<&Champion>, @@ -225,7 +225,44 @@ fn imperative_attack( MOVEMENT_SPEED * time.delta().as_secs_f32(), ); position.0 = new_position; - } else { + } + } + _ => {} + } + } +} + +fn imperative_attack_attack( + entity_map: Res<EntityMap>, + mut commands: Commands, + mut cooldowns: Query<&mut Cooldown>, + mut players: Query<(&PlayerId, &mut Imperative)>, + mut positions: Query<&mut PlayerPosition>, + champions: Query<&Champion>, +) { + for (id, mut imperative) in players.iter_mut() { + match *imperative { + Imperative::Attack(target_player) => { + let Some(entity) = entity_map.0.get(&id.0) else { + *imperative = Imperative::Idle; + return; + }; + let Ok(champion) = champions.get(*entity) else { + *imperative = Imperative::Idle; + return; + }; + let Some(target_entity) = entity_map.0.get(&target_player.0) else { + *imperative = Imperative::Idle; + return; + }; + let Ok([position, target_position]) = + positions.get_many_mut([*entity, *target_entity]) + else { + *imperative = Imperative::Idle; + return; + }; + let distance = target_position.0.distance(position.0); + if distance <= Stats::from_champion(*champion).attack_range { let Ok(mut cooldown) = cooldowns.get_mut(*entity) else { *imperative = Imperative::Idle; return; |