diff options
Diffstat (limited to 'src/server.rs')
-rw-r--r-- | src/server.rs | 90 |
1 files changed, 55 insertions, 35 deletions
diff --git a/src/server.rs b/src/server.rs index 52daf1a..c70b39e 100644 --- a/src/server.rs +++ b/src/server.rs @@ -125,16 +125,14 @@ fn imperative_walk_to( match *imperative { Imperative::WalkTo(target_position) => { if let Ok(mut position) = positions.get_mut(entity) { - let distance = (target_position - position.0).length(); - let direction = (target_position - position.0).normalize_or_zero(); - let new_position = position.0 - + f32::min(MOVEMENT_SPEED * time.delta().as_secs_f32(), distance) - * direction; - if position.0.distance(new_position) < f32::EPSILON { - position.0 = target_position; + let (new_position, target_reached) = move_to_target( + position.0, + target_position, + MOVEMENT_SPEED * time.delta().as_secs_f32(), + ); + position.0 = new_position; + if target_reached { *imperative = Imperative::Idle; - } else { - position.0 = new_position; } } else { *imperative = Imperative::Idle; @@ -145,29 +143,57 @@ fn imperative_walk_to( } } +fn move_to_target(position: Vec2, target_position: Vec2, max_distance: f32) -> (Vec2, bool) { + let distance = (target_position - position).length(); + let direction = (target_position - position).normalize_or_zero(); + let new_position = position + f32::min(max_distance, distance) * direction; + if position.distance(new_position) <= f32::EPSILON { + (target_position, true) + } else { + (new_position, false) + } +} + fn imperative_attack( entity_map: Res<EntityMap>, mut commands: Commands, mut cooldowns: Query<&mut Cooldown>, mut players: Query<(&PlayerId, &mut Imperative)>, - positions: Query<&PlayerPosition>, + mut positions: Query<&mut PlayerPosition>, + time: Res<Time>, ) { for (id, mut imperative) in players.iter_mut() { match *imperative { Imperative::Attack(target_player) => { if let Some(entity) = entity_map.0.get(&id.0) { - if let Ok(position) = positions.get(*entity) { - if let Ok(mut cooldown) = cooldowns.get_mut(*entity) { - if cooldown.a_cooldown.is_zero() { - cooldown.a_cooldown = Duration::from_secs_f32(1.5); - commands.spawn(ProjectileBundle { - projectile: Projectile { - target_player, - source_player: *id, - }, - position: ProjectilePosition(position.0), - replicate: Replicate::default(), - }); + if let Some(target_entity) = entity_map.0.get(&target_player.0) { + if let Ok([mut position, target_position]) = + positions.get_many_mut([*entity, *target_entity]) + { + let distance = target_position.0.distance(position.0); + if distance > ATTACK_RANGE { + let (new_position, _) = move_to_target( + position.0, + target_position.0, + MOVEMENT_SPEED * time.delta().as_secs_f32(), + ); + position.0 = new_position; + } else { + if let Ok(mut cooldown) = cooldowns.get_mut(*entity) { + if cooldown.a_cooldown.is_zero() { + cooldown.a_cooldown = Duration::from_secs_f32(1.5); + commands.spawn(ProjectileBundle { + projectile: Projectile { + target_player, + source_player: *id, + }, + position: ProjectilePosition(position.0), + replicate: Replicate::default(), + }); + } + } else { + } + *imperative = Imperative::Idle; } } else { *imperative = Imperative::Idle; @@ -175,8 +201,6 @@ fn imperative_attack( } else { *imperative = Imperative::Idle; } - } else { - *imperative = Imperative::Idle; } } _ => {} @@ -197,16 +221,12 @@ fn projectile_move( if let Some(target_entity) = entity_map.0.get(&projectile.target_player.0) { if let Ok(mut position) = projectile_positions.get_mut(entity) { if let Ok(target_position) = player_positions.get(*target_entity) { - let distance = (target_position.0 - position.0).length(); - let direction = (target_position.0 - position.0).normalize_or_zero(); - let new_position = position.0 - + f32::min(PROJECTILE_SPEED * time.delta().as_secs_f32(), distance) - * direction; - if position.0.distance(new_position) < f32::EPSILON { - position.0 = target_position.0; - } else { - position.0 = new_position; - } + let (new_position, _) = move_to_target( + position.0, + target_position.0, + PROJECTILE_SPEED * time.delta().as_secs_f32(), + ); + position.0 = new_position; } } } @@ -224,7 +244,7 @@ fn projectile_despawn( if let Some(target_entity) = entity_map.0.get(&projectile.target_player.0) { if let Ok(position) = projectile_positions.get(entity) { if let Ok(target_position) = player_positions.get(*target_entity) { - if position.0.distance(target_position.0) < f32::EPSILON { + if position.0.distance(target_position.0) <= f32::EPSILON { commands.entity(entity).despawn(); } } |