help me
Some checks failed
Rust Checks / checks (push) Failing after 4s

This commit is contained in:
CoCo_Sol 2024-03-10 18:52:08 +01:00
parent b11c81fe08
commit 86e47a79de
4 changed files with 56 additions and 25 deletions

View file

@ -28,17 +28,16 @@ fn start(mut event: EventWriter<StartMapGeneration>) {
}); });
} }
fn update(mut event: EventReader<border_wars::map::click_tile::TileJustClicked>, query: Query<(Entity, &Tile)>) { fn update(
mut commands: Commands,
mut event: EventReader<border_wars::map::click_tile::TileJustClicked>,
query: Query<(Entity, &Tile)>,
) {
for event in event.read() { for event in event.read() {
for (entity, tile) in query.iter() { for (entity, tile) in query.iter() {
if entity.index() == event.0 { if entity.index() == event.0 {
commands.entity(entity).despawn();
}
println!("Tile clicked: {:?}", tile);
} }
} }
} }
}

View file

@ -2,7 +2,8 @@
use bevy::prelude::*; use bevy::prelude::*;
use super::Tile; use super::renderer::TilesGap;
use super::{Tile, TilePosition};
/// The event that is triggered when a tile is clicked. /// The event that is triggered when a tile is clicked.
/// ///
@ -75,31 +76,31 @@ fn mouse_handler(
/// Get the closest tile to the cursor and send it in an event. /// Get the closest tile to the cursor and send it in an event.
fn select_closest_tile( fn select_closest_tile(
tiles: Query<(Entity, &Transform, &Tile)>, tiles: Query<(Entity, &Transform, &Tile, &TilePosition)>,
mut click_event_reader: EventReader<ClickOnTheWorld>, mut click_event_reader: EventReader<ClickOnTheWorld>,
mut clicked_tile_event_writer: EventWriter<TileJustClicked>, mut clicked_tile_event_writer: EventWriter<TileJustClicked>,
gap_size: Res<TilesGap>,
) { ) {
for click_event in click_event_reader.read() { for click_event in click_event_reader.read() {
// The closest tile and its distance to the cursor.
let mut closest_entity: Option<Entity> = None;
let mut closest_position: Option<f32> = None;
for (tile_entity, tile_transform, tile_type) in tiles.iter() { for (tile_entity, tile_transform, tile_type, tile_pos) in tiles.iter() {
let mut tile_position = tile_transform.translation.truncate();
let mut pos = click_event.0;
// because the origin of tiles is the bottom center
let tile_size = tile_type.get_image_size(); let tile_size = tile_type.get_image_size();
let tile_scale = tile_transform.scale.truncate(); let tile_scale = tile_transform.scale.truncate();
pos.y -= (tile_size.y / 2.0) * tile_scale.y;
tile_position += (tile_size / 2.0) * tile_scale; //
pos /= gap_size.0;
let distance_to_cursor = tile_position.distance(click_event.0); let hex_pos = TilePosition::from_pixel_coordinates(pos);
if closest_position.is_none() || closest_position > Some(distance_to_cursor) { if *tile_pos == hex_pos {
closest_entity = Some(tile_entity); clicked_tile_event_writer.send(TileJustClicked(tile_entity.index()));
closest_position = Some(distance_to_cursor); break;
} }
} }
if let Some(tile_entity) = closest_entity {
clicked_tile_event_writer.send(TileJustClicked(tile_entity.index()));
}
} }
} }

View file

@ -262,6 +262,37 @@ impl<T: Number> HexPosition<T> {
) )
} }
/// doc : https://www.redblobgames.com/grids/hexagons/#rounding
fn axial_round(&self) -> Self {
let old_q = self.0;
let old_r = self.1;
let old_s = -old_q - old_r;
let q = T::from_f32(old_q.to_f32().round());
let r = T::from_f32(old_r.to_f32().round());
let s = T::from_f32(old_s.to_f32().round());
let q_diff = (q - old_q).abs();
let r_diff = (r - old_r).abs();
let s_diff = (s - old_s).abs();
if q_diff > r_diff && q_diff > s_diff {
Self(-r - s, r)
} else if r_diff > s_diff {
Self(q, -q - s)
} else {
Self(q, r)
}
}
/// doc : https://www.redblobgames.com/grids/hexagons/#pixel-to-hex
pub fn from_pixel_coordinates(pixel: Vec2) -> Self {
let q = ((3_f32.sqrt() / 3.0) * pixel.x) - ((1. / 3.) * pixel.y);
let r = (2.0 / 3.0) * pixel.y;
Self(T::from_f32(q), T::from_f32(r)).axial_round()
}
/// Returns the distance between two [HexPosition]s. /// Returns the distance between two [HexPosition]s.
/// ///
/// # How it works /// # How it works

View file

@ -21,7 +21,7 @@ impl Plugin for RendererPlugin {
/// The gap between the center of the tiles in the map. /// The gap between the center of the tiles in the map.
#[derive(Resource)] #[derive(Resource)]
struct TilesGap(Vec2); pub struct TilesGap(pub Vec2);
/// The size of the tiles in the map. /// The size of the tiles in the map.
#[derive(Resource, Clone, Copy)] #[derive(Resource, Clone, Copy)]
@ -81,7 +81,7 @@ fn render_map(
commands.entity(entity).insert(SpriteBundle { commands.entity(entity).insert(SpriteBundle {
sprite: Sprite { sprite: Sprite {
anchor: Anchor::BottomLeft, anchor: Anchor::BottomCenter,
..default() ..default()
}, },
texture, texture,