From 7108e8649267f025c158c423aa608a4c39440fff Mon Sep 17 00:00:00 2001 From: CoCo_Sol007 Date: Wed, 6 Mar 2024 18:46:25 +0100 Subject: [PATCH] change struct --- crates/border-wars/src/map/mod.rs | 2 +- crates/border-wars/src/map/selection.rs | 61 ++++--------------------- 2 files changed, 9 insertions(+), 54 deletions(-) diff --git a/crates/border-wars/src/map/mod.rs b/crates/border-wars/src/map/mod.rs index 24c15d6..543fdc6 100644 --- a/crates/border-wars/src/map/mod.rs +++ b/crates/border-wars/src/map/mod.rs @@ -2,8 +2,8 @@ pub mod generation; pub mod hex; -pub mod renderer; pub mod selection; +pub mod renderer; use bevy::prelude::*; diff --git a/crates/border-wars/src/map/selection.rs b/crates/border-wars/src/map/selection.rs index 3a65939..6a7e5f4 100644 --- a/crates/border-wars/src/map/selection.rs +++ b/crates/border-wars/src/map/selection.rs @@ -1,17 +1,14 @@ //! All programs related to the selection of tiles. use bevy::prelude::*; -use bevy::sprite::Anchor; use super::Tile; -/// A component that mark a tile as selected. -#[derive(Component)] -pub struct SelectedTile; - -/// A component that mark an entity as a selection. -#[derive(Component)] -struct Selection; +/// The event that is triggered when a tile is selected. +/// +/// The event contains the index (ID) of the selected tile. +#[derive(Event)] +pub struct TileJustClicked(pub u32); /// A event that is triggered when a mouse button is clicked. /// @@ -19,14 +16,13 @@ struct Selection; #[derive(Event)] struct ClickOnTheWorld(Vec2); -/// A plugin that handles and render the selection of tiles. +/// A plugin that handles the selection of tiles. pub struct SelectorPlugin; impl Plugin for SelectorPlugin { fn build(&self, app: &mut App) { app.add_systems(PreUpdate, mouse_handler) .add_systems(PreUpdate, select_closest_tile) - .add_systems(Update, render_selection) .add_event::(); } } @@ -66,11 +62,9 @@ fn mouse_handler( /// Selects the closest tile to the cursor and marks it as selected. /// It also unmarks the old selection if there is one. fn select_closest_tile( - mut commands: Commands, tiles: Query<(Entity, &Transform, &Tile)>, - mut old_selection: Query>, - mut selected_tile: Local>, mut click_event_reader: EventReader, + mut select_tile_event_writer: EventWriter, ) { for click_event in click_event_reader.read() { // The closest tile and its distance to the cursor. @@ -93,46 +87,7 @@ fn select_closest_tile( } } if let Some(tile_entity) = closest_entity { - if *selected_tile == Some(tile_entity.index()) { - return; - } - commands.entity(tile_entity).insert(SelectedTile); - *selected_tile = Some(tile_entity.index()); - } - - for entity in old_selection.iter_mut() { - commands.entity(entity).remove::(); - } - } -} - -/// Renders the selection with a sprite. -/// -/// The position updates when the selected tile changes. -fn render_selection( - mut commands: Commands, - asset_server: Res, - query: Query<&Transform, Added>, - mut selection_query: Query<&mut Transform, (With, Without)>, -) { - for target_transform in query.iter() { - let mut transform = *target_transform; - transform.translation.z += 1.0; - if selection_query.is_empty() { - commands - .spawn(SpriteBundle { - sprite: Sprite { - anchor: Anchor::BottomRight, - ..Default::default() - }, - transform, - // TODO: This image is a temporary image of the selection. - texture: asset_server.load("selection.png"), - ..default() - }) - .insert(Selection); - } else if let Ok(mut selection_transform) = selection_query.get_single_mut() { - *selection_transform = transform; + select_tile_event_writer.send(TileJustClicked(tile_entity.index())); } } }