pub fn execute_turn(
  player_turn_ended: RemovedComponents<PlayerTurnAnimating>,
  enemy_turn_ended: RemovedComponents<EnemyTurnAnimating>,
  mut entity_q: Query<(&mut TilePos, &MapEntityType, &mut EntityPendingAction)>,
  mut health: Query<&mut EntityHealth>,
) {
  let has_player_turn_ended = player_turn_ended.iter().count() > 0;
  let has_enemy_turn_ended = enemy_turn_ended.iter().count() > 0;

  if !has_player_turn_ended && !has_enemy_turn_ended {
    return;
  }

  entity_q
    .iter_mut()
    .filter(|(_, kind, _)| {
      if let MapEntityType::Player(_) = kind {
        has_player_turn_ended
      } else {
        has_enemy_turn_ended
      }
    })
    .for_each(|(mut pos, entity_type, mut action)| {
      match action.action {
        EntityAction::Move((_, end)) => {
          pos.0 = end.0;
          pos.1 = end.1;
        }
        EntityAction::Attack(PendingAttack {
          enemy_entity: target_entity,
          new_standing_position: new_pos,
          enemy_position: target_pos,
          ..
        }) => {
          // TODO: sometimes this fails
          if utils::tile_distance(&new_pos, &target_pos) == 1 {
            // Todo: the damage amount should be contained in `PendingAttack`
            let damage = match entity_type {
              &MapEntityType::Enemy => constants::ENEMY_ATTACK_DAMAGE,
              &MapEntityType::Player(_) => constants::PLAYER_ATTACK_DAMAGE,
              _ => 0.,
            };

            if let Ok(mut health) = health.get_mut(target_entity) {
              health.health -= damage;
            }
          }
          pos.0 = new_pos.0;
          pos.1 = new_pos.1;
        }
        _ => {}
      }

      action.is_ready = true;
      action.action = EntityAction::Wait;
    });
}
/// Fires at the end of a turn animation
/// has to update the state/positions of map entities
Fires at the end of a turn animation
has to update the state/positions of map entities
Fires at the end of a turn animation has to update the state / positions of map entities
pub fn execute_turn(
  player_turn_ended: RemovedComponents<PlayerTurnAnimating>,
  enemy_turn_ended: RemovedComponents<EnemyTurnAnimating>,
  mut entity_q: Query<(&mut TilePos, &MapEntityType, &mut EntityPendingAction)>,
  mut health: Query<&mut EntityHealth>,
) {
  let has_player_turn_ended = player_turn_ended.iter().count() > 0;
  let has_enemy_turn_ended = enemy_turn_ended.iter().count() > 0;
  if !has_player_turn_ended && !has_enemy_turn_ended {
    return;
  }
  entity_q
    .iter_mut()
    .filter(|(_, kind, _)| {
      if let MapEntityType::Player(_) = kind {
        has_player_turn_ended
      } else {
        has_enemy_turn_ended
      }
    })
    .for_each(|(mut pos, entity_type, mut action)| {
      match action.action {
        EntityAction::Move((_, end)) => {
          pos.0 = end.0;
          pos.1 = end.1;
        }
        EntityAction::Attack(PendingAttack {
          enemy_entity: target_entity,
          new_standing_position: new_pos,
          enemy_position: target_pos,
          ..
        }) => {
          // TODO: sometimes this fails
          if utils::tile_distance(&new_pos, &target_pos) == 1 {
            // Todo: the damage amount should be contained in `PendingAttack`
            let damage = match entity_type {
              &MapEntityType::Enemy => constants::ENEMY_ATTACK_DAMAGE,
              &MapEntityType::Player(_) => constants::PLAYER_ATTACK_DAMAGE,
              _ => 0.,
            };
            if let Ok(mut health) = health.get_mut(target_entity) {
              health.health -= damage;
            }
          }
          pos.0 = new_pos.0;
          pos.1 = new_pos.1;
        }
        _ => {}
      }
      action.is_ready = true;
      action.action = EntityAction::Wait;
    });
}
pub fn execute_turn ( player_turn_ended : RemovedComponents < PlayerTurnAnimating > , enemy_turn_ended : RemovedComponents < EnemyTurnAnimating > , mut entity_q : Query < ( & mut TilePos , & MapEntityType , & mut EntityPendingAction ) > , mut health : Query < & mut EntityHealth > , ) { let has_player_turn_ended = player_turn_ended . iter ( ) . count ( ) > 0 ; let has_enemy_turn_ended = enemy_turn_ended . iter ( ) . count ( ) > 0 ; if ! has_player_turn_ended && ! has_enemy_turn_ended { return ; } entity_q . iter_mut ( ) . filter ( | ( _ , kind , _ ) | { if let MapEntityType :: Player ( _ ) = kind { has_player_turn_ended } else { has_enemy_turn_ended } } ) . for_each ( | ( mut pos , entity_type , mut action ) | { match action . action { EntityAction :: Move ( ( _ , end ) ) => { pos . 0 = end . 0 ; pos . 1 = end . 1 ; } EntityAction :: Attack ( PendingAttack { enemy_entity : target_entity , new_standing_position : new_pos , enemy_position : target_pos , .. } ) => { // TODO: sometimes this fails if utils :: tile_distance ( & new_pos , & target_pos ) == 1 { // Todo: the damage amount should be contained in `PendingAttack` let damage = match entity_type { & MapEntityType :: Enemy => constants :: ENEMY_ATTACK_DAMAGE , & MapEntityType :: Player ( _ ) => constants :: PLAYER_ATTACK_DAMAGE , _ => 0. , } ; if let Ok ( mut health ) = health . get_mut ( target_entity ) { health . health -= damage ; } } pos . 0 = new_pos . 0 ; pos . 1 = new_pos . 1 ; } _ => { } } action . is_ready = true ; action . action = EntityAction :: Wait ; } ) ; }
Fires at the end of a turn animation
has to update the state/positions of map entities
Fires at the end of a turn animation has to update the state / positions of map entities
player_turn_ended
RemovedComponents<PlayerTurnAnimating>
enemy_turn_ended
RemovedComponents<EnemyTurnAnimating>
entity_q
Query<(&mut TilePos, &MapEntityType, &mut EntityPendingAction)>
health
Query<&mut EntityHealth>
player_turn_ended
RemovedComponents<PlayerTurnAnimating>
enemy_turn_ended
RemovedComponents<EnemyTurnAnimating>
entity_q
Query<(&mut TilePos, &MapEntityType, &mut EntityPendingAction)>
health
Query<&mut EntityHealth>