Skip to content

Commit

Permalink
Use EventReader::read() instead of iter()
Browse files Browse the repository at this point in the history
  • Loading branch information
Indy2222 committed Jan 26, 2024
1 parent 017e239 commit b9de61e
Show file tree
Hide file tree
Showing 44 changed files with 93 additions and 93 deletions.
2 changes: 1 addition & 1 deletion crates/behaviour/src/chase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl ChaseTarget {
}

fn handle_chase_events(mut commands: Commands, mut events: EventReader<ChaseTargetEvent>) {
for event in events.iter() {
for event in events.read() {
let mut entity_commands = commands.entity(event.entity());
match event.target() {
Some(target) => entity_commands.insert(ChaseTargetComponent::new(target.clone())),
Expand Down
10 changes: 5 additions & 5 deletions crates/camera/src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ fn process_move_focus_events(
terrain: TerrainCollider,
mut out_events: EventWriter<UpdateTranslationEvent>,
) {
let event = match in_events.iter().last() {
let event = match in_events.read().last() {
Some(event) => event,
None => return,
};
Expand Down Expand Up @@ -516,7 +516,7 @@ fn handle_horizontal_events(
mut movement: ResMut<HorizontalMovement>,
mut events: EventReader<MoveCameraHorizontallyEvent>,
) {
if let Some(event) = events.iter().last() {
if let Some(event) = events.read().last() {
movement.set(event.direction());
}
}
Expand All @@ -526,7 +526,7 @@ fn handle_zoom_events(
mut events: EventReader<ZoomCameraEvent>,
mut desired: ResMut<DesiredDistance>,
) {
for event in events.iter() {
for event in events.read() {
desired.zoom_clamped(conf.camera(), event.factor());
}
}
Expand All @@ -535,7 +535,7 @@ fn handle_tilt_events(
mut events: EventReader<TiltCameraEvent>,
mut desired: ResMut<DesiredOffNadir>,
) {
for event in events.iter() {
for event in events.read() {
desired.tilt_clamped(Radian::ONE * event.delta());
}
}
Expand All @@ -544,7 +544,7 @@ fn handle_rotate_events(
mut events: EventReader<RotateCameraEvent>,
mut desired: ResMut<DesiredAzimuth>,
) {
for event in events.iter() {
for event in events.read() {
desired.rotate(Radian::ONE * event.delta());
}
}
2 changes: 1 addition & 1 deletion crates/combat/src/attack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ fn attack(
cannons: Query<&LaserCannon>,
mut chase_events: EventWriter<ChaseTargetEvent>,
) {
for event in attack_events.iter() {
for event in attack_events.read() {
if let Ok(cannon) = cannons.get(event.attacker()) {
commands
.entity(event.attacker())
Expand Down
6 changes: 3 additions & 3 deletions crates/combat/src/health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn update_local_health(
mut out_events: EventWriter<UpdateHealthEvent>,
mut net_events: EventWriter<ToPlayersEvent>,
) {
for event in in_events.iter() {
for event in in_events.read() {
out_events.send(UpdateHealthEvent::new(event.entity, event.delta));

if config.multiplayer() {
Expand All @@ -97,7 +97,7 @@ fn update_remote_health(
mut in_events: EventReader<NetRecvHealthEvent>,
mut out_events: EventWriter<UpdateHealthEvent>,
) {
for event in in_events.iter() {
for event in in_events.read() {
out_events.send(UpdateHealthEvent::new(event.entity(), event.delta()));
}
}
Expand All @@ -107,7 +107,7 @@ fn update_health(
mut health_events: EventReader<UpdateHealthEvent>,
mut bar_events: EventWriter<UpdateBarValueEvent>,
) {
for event in health_events.iter() {
for event in health_events.read() {
let Ok(mut health) = healths.get_mut(event.entity) else {
continue;
};
Expand Down
2 changes: 1 addition & 1 deletion crates/combat/src/laser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn fire(
mut health: EventWriter<LocalUpdateHealthEvent>,
mut trail: EventWriter<LocalLaserTrailEvent>,
) {
for fire in fires.iter() {
for fire in fires.read() {
let observation = sightline.sight(fire.ray(), fire.max_toi(), fire.attacker());

trail.send(LocalLaserTrailEvent::new(Ray::new(
Expand Down
8 changes: 4 additions & 4 deletions crates/combat/src/trail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ fn local_laser_trail(
mut out_events: EventWriter<LaserTrailEvent>,
mut net_events: EventWriter<ToPlayersEvent>,
) {
for event in in_events.iter() {
for event in in_events.read() {
out_events.send(LaserTrailEvent(event.0));

if config.multiplayer() {
Expand All @@ -147,7 +147,7 @@ fn remote_laser_trail(
mut in_events: EventReader<NetRecvProjectileEvent>,
mut out_events: EventWriter<LaserTrailEvent>,
) {
for event in in_events.iter() {
for event in in_events.read() {
match **event {
NetProjectile::Laser { origin, direction } => {
out_events.send(LaserTrailEvent(Ray::new(origin.into(), direction.into())));
Expand All @@ -163,7 +163,7 @@ fn laser_trail(
mesh: Res<MeshHandle>,
mut events: EventReader<LaserTrailEvent>,
) {
for event in events.iter() {
for event in events.read() {
let material = materials.add(TrailMaterial::new(time.elapsed_seconds_wrapped()));

commands.spawn((
Expand All @@ -189,7 +189,7 @@ fn laser_sound(
mut events: EventReader<LaserTrailEvent>,
mut sound_events: EventWriter<PlaySpatialAudioEvent>,
) {
for event in events.iter() {
for event in events.read() {
sound_events.send(PlaySpatialAudioEvent::new(
Sound::LaserFire,
event.0.origin.into(),
Expand Down
6 changes: 3 additions & 3 deletions crates/construction/src/manufacturing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ fn change_locations(
mut pole_events: EventWriter<UpdatePoleLocationEvent>,
mut line_events: EventWriter<UpdateLineEndEvent>,
) {
for event in events.iter() {
for event in events.read() {
if let Ok(mut location) = locations.get_mut(event.factory()) {
let owner = event.factory();
location.0 = event.position();
Expand All @@ -337,7 +337,7 @@ fn enqueue(
mut events: EventReader<EnqueueAssemblyEvent>,
mut lines: Query<&mut AssemblyLine>,
) {
for event in events.iter() {
for event in events.read() {
let Ok(mut line) = lines.get_mut(event.factory()) else {
continue;
};
Expand Down Expand Up @@ -415,7 +415,7 @@ fn deliver(
&DeliveryLocation,
)>,
) {
for delivery in deliver_events.iter() {
for delivery in deliver_events.read() {
info!(
"Manufacturing of {} in {:?} just finished.",
delivery.unit(),
Expand Down
6 changes: 3 additions & 3 deletions crates/controller/src/commands/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn send_selected_system(
mut path_events: EventWriter<UpdateEntityPathEvent>,
mut chase_events: EventWriter<ChaseTargetEvent>,
) {
if let Some(send) = send_events.iter().last() {
if let Some(send) = send_events.read().last() {
for entity in selected.iter() {
chase_events.send(ChaseTargetEvent::new(entity, None));
path_events.send(UpdateEntityPathEvent::new(
Expand All @@ -104,7 +104,7 @@ fn delivery_location_system(
selected: Query<Entity, SelectedFactory>,
mut out_events: EventWriter<ChangeDeliveryLocationEvent>,
) {
if let Some(event) = in_events.iter().last() {
if let Some(event) = in_events.read().last() {
for entity in selected.iter() {
out_events.send(ChangeDeliveryLocationEvent::new(entity, event.target()));
}
Expand All @@ -116,7 +116,7 @@ fn attack_system(
selected: Query<Entity, SelectedMovable>,
mut individual_events: EventWriter<AttackEvent>,
) {
if let Some(group_event) = group_events.iter().last() {
if let Some(group_event) = group_events.read().last() {
for attacker in selected.iter() {
individual_events.send(AttackEvent::new(attacker, group_event.target()));
}
Expand Down
12 changes: 6 additions & 6 deletions crates/controller/src/commands/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ fn on_click(button: MouseButton) -> impl Fn(EventReader<MouseClickedEvent>) -> b
move |mut events: EventReader<MouseClickedEvent>| {
// It is desirable to exhaust the iterator, thus .filter().count() is
// used instead of .any()
events.iter().filter(|e| e.button() == button).count() > 0
events.read().filter(|e| e.button() == button).count() > 0
}
}

fn on_double_click(button: MouseButton) -> impl Fn(EventReader<MouseDoubleClickedEvent>) -> bool {
move |mut events: EventReader<MouseDoubleClickedEvent>| {
// It is desirable to exhaust the iterator, thus .filter().count() is
// used instead of .any()
events.iter().filter(|e| e.button() == button).count() > 0
events.read().filter(|e| e.button() == button).count() > 0
}
}

Expand Down Expand Up @@ -210,7 +210,7 @@ fn move_camera_arrows_system(
mut key_events: EventReader<KeyboardInput>,
mut move_events: EventWriter<MoveCameraHorizontallyEvent>,
) {
for key_event in key_events.iter() {
for key_event in key_events.read() {
let Some(key_code) = key_event.key_code else {
continue;
};
Expand Down Expand Up @@ -277,7 +277,7 @@ fn zoom_camera(
) {
let conf = conf.camera();
let factor = wheel_events
.iter()
.read()
.fold(1.0, |factor, event| match event.unit {
MouseScrollUnit::Line => factor * conf.wheel_zoom_sensitivity().powf(event.y),
MouseScrollUnit::Pixel => factor * conf.touchpad_zoom_sensitivity().powf(event.y),
Expand All @@ -297,7 +297,7 @@ fn pivot_camera(
return;
}

let delta = mouse_event.iter().fold(Vec2::ZERO, |sum, e| sum + e.delta);
let delta = mouse_event.read().fold(Vec2::ZERO, |sum, e| sum + e.delta);
let sensitivity = conf.camera().rotation_sensitivity();
if delta.x != 0. {
rotate_event.send(RotateCameraEvent::new(sensitivity * delta.x));
Expand Down Expand Up @@ -390,7 +390,7 @@ fn update_drags(
mut ui_events: EventWriter<UpdateSelectionBoxEvent>,
mut select_events: EventWriter<SelectInRectEvent>,
) {
for drag_event in drag_events.iter() {
for drag_event in drag_events.read() {
if drag_event.button() != MouseButton::Left {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/controller/src/commands/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl KeyCondition {
pub(super) fn build(self) -> impl Fn(Res<Input<KeyCode>>, EventReader<KeyboardInput>) -> bool {
move |keys: Res<Input<KeyCode>>, mut events: EventReader<KeyboardInput>| {
let proper_key = events
.iter()
.read()
.filter(|k| {
k.state == ButtonState::Pressed && k.key_code.map_or(false, |c| c == self.key)
})
Expand Down
2 changes: 1 addition & 1 deletion crates/controller/src/draft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ fn new_drafts(
mut events: EventReader<NewDraftEvent>,
drafts: Query<Entity, With<DraftAllowed>>,
) {
let event = match events.iter().last() {
let event = match events.read().last() {
Some(event) => event,
None => return,
};
Expand Down
2 changes: 1 addition & 1 deletion crates/controller/src/hud/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ fn toggle_system(
mut events: EventReader<ToggleGameMenuEvent>,
mut query: Query<&mut Visibility, With<PopUpMenu>>,
) {
if events.iter().count() % 2 == 0 {
if events.read().count() % 2 == 0 {
return;
}

Expand Down
10 changes: 5 additions & 5 deletions crates/controller/src/hud/minimap/interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ fn press_handler(
) {
let cursor = window_query.single().cursor_position();

for event in input_events.iter() {
for event in input_events.read() {
match event.state {
ButtonState::Released => {
dragging.retain(|b| *b != event.button);
Expand Down Expand Up @@ -173,7 +173,7 @@ fn move_camera_system(
mut drag_events: EventReader<MinimapDragEvent>,
mut camera_events: EventWriter<MoveFocusEvent>,
) {
for press in press_events.iter() {
for press in press_events.read() {
if press.button() != MouseButton::Left {
continue;
}
Expand All @@ -182,7 +182,7 @@ fn move_camera_system(
camera_events.send(event);
}

for drag in drag_events.iter() {
for drag in drag_events.read() {
if drag.button() != MouseButton::Left {
continue;
}
Expand All @@ -196,7 +196,7 @@ fn send_units_system(
mut press_events: EventReader<MinimapPressEvent>,
mut send_events: EventWriter<SendSelectedEvent>,
) {
for press in press_events.iter() {
for press in press_events.read() {
if press.button() != MouseButton::Right {
continue;
}
Expand All @@ -208,7 +208,7 @@ fn delivery_location_system(
mut press_events: EventReader<MinimapPressEvent>,
mut location_events: EventWriter<DeliveryLocationSelectedEvent>,
) {
for press in press_events.iter() {
for press in press_events.read() {
if press.button() != MouseButton::Right {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/controller/src/hud/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn process_events(
mut boxes: Query<(Entity, &mut Style), With<SelectionBox>>,
mut events: EventReader<UpdateSelectionBoxEvent>,
) {
if let Some(event) = events.iter().last() {
if let Some(event) = events.read().last() {
match event.0 {
Some(rect) => {
let size = rect.size();
Expand Down
4 changes: 2 additions & 2 deletions crates/controller/src/mouse/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ fn update_buttons(
mut clicks: EventWriter<MouseClickedEvent>,
mut drags: EventWriter<MouseDraggedEvent>,
) {
for event in input_events.iter() {
for event in input_events.read() {
match event.state {
ButtonState::Released => {
if let Some(drag_resolution) = mouse_state.resolve(event.button) {
Expand Down Expand Up @@ -305,7 +305,7 @@ fn check_double_click(
mut last_click_time: Local<f64>,
time: Res<Time>,
) {
for mouse_clicked in clicks.iter() {
for mouse_clicked in clicks.read() {
let current_time = time.elapsed_seconds_f64();

if last_click_position.map_or(true, |p| {
Expand Down
2 changes: 1 addition & 1 deletion crates/controller/src/selection/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn select_in_area(
mut in_events: EventReader<SelectInRectEvent>,
mut out_events: EventWriter<SelectEvent>,
) {
for in_event in in_events.iter() {
for in_event in in_events.read() {
let event_frustum = screen_frustum.rect(in_event.rect());
let entities: Vec<Entity> = candidates
.iter()
Expand Down
6 changes: 3 additions & 3 deletions crates/controller/src/selection/bookkeeping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl<'w, 's> Selector<'w, 's> {

fn update_selection(mut events: EventReader<SelectEvent>, selector_builder: SelectorBuilder) {
let mut selector = selector_builder.build();
for event in events.iter() {
for event in events.read() {
selector.update(event.entities(), event.mode());
}
selector.execute();
Expand All @@ -162,7 +162,7 @@ fn selected_system(
mut poles: EventWriter<UpdatePoleVisibilityEvent>,
mut lines: EventWriter<UpdateLineVisibilityEvent>,
) {
for event in events.iter() {
for event in events.read() {
if let Ok(mut visibility) = markers.get_mut(event.0) {
visibility.0.update_visible(SELECTION_BAR_ID, true);
}
Expand All @@ -185,7 +185,7 @@ fn deselected_system(
mut poles: EventWriter<UpdatePoleVisibilityEvent>,
mut lines: EventWriter<UpdateLineVisibilityEvent>,
) {
for event in events.iter() {
for event in events.read() {
if let Ok(mut visibility) = markers.get_mut(event.0) {
visibility.0.update_visible(SELECTION_BAR_ID, false);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/gui/src/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ fn focus_system(
}
}

if let Some(event) = events.iter().last() {
if let Some(event) = events.read().last() {
current = event.0;
}

Expand Down
Loading

0 comments on commit b9de61e

Please sign in to comment.