generated from tipragot/rust
parent
445bc5e10e
commit
b7e3e9da9c
|
@ -5,6 +5,7 @@ use bevy::prelude::*;
|
|||
pub mod camera;
|
||||
pub mod map;
|
||||
pub mod scenes;
|
||||
pub mod ui;
|
||||
|
||||
/// The current scene of the game.
|
||||
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)]
|
||||
|
|
|
@ -3,8 +3,10 @@
|
|||
use bevy::prelude::*;
|
||||
use border_wars::camera::CameraPlugin;
|
||||
use border_wars::map::click_tile::TilesClickable;
|
||||
use border_wars::map::generation::{MapGenerationPlugin, StartMapGeneration};
|
||||
use border_wars::map::renderer::RendererPlugin;
|
||||
use border_wars::scenes::ScenesPlugin;
|
||||
use border_wars::ui::tiles_info::TilesInfoPlugin;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
|
@ -13,5 +15,15 @@ fn main() {
|
|||
.add_plugins(RendererPlugin)
|
||||
.add_plugins(CameraPlugin)
|
||||
.add_plugins(TilesClickable)
|
||||
.add_plugins(MapGenerationPlugin)
|
||||
.add_systems(OnEnter(border_wars::CurrentScene::Game), start)
|
||||
.add_plugins(TilesInfoPlugin)
|
||||
.run();
|
||||
}
|
||||
|
||||
fn start(mut event: EventWriter<StartMapGeneration>) {
|
||||
event.send(StartMapGeneration {
|
||||
seed: 0,
|
||||
radius: 10,
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//! All programs related to the clicking on a tile.
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy::{input::mouse::MouseButtonInput, prelude::*};
|
||||
|
||||
use super::Tile;
|
||||
|
||||
|
@ -36,14 +36,18 @@ impl Plugin for TilesClickable {
|
|||
/// Handles the mouse click and gets the position of the cursor in the world.
|
||||
/// Finally, it sends an event with the position of the cursor.
|
||||
fn mouse_handler(
|
||||
mouse_button_input: Res<Input<MouseButton>>,
|
||||
mut mouse_button_event: EventReader<MouseButtonInput>,
|
||||
windows: Query<&Window>,
|
||||
cameras: Query<(&Camera, &GlobalTransform)>,
|
||||
mut events_writer: EventWriter<ClickOnTheWorld>,
|
||||
not_clickable_zones: Query<(&Node, &GlobalTransform), With<ZoneNotClickable>>,
|
||||
ui_scale: Res<UiScale>,
|
||||
) {
|
||||
if !mouse_button_input.just_pressed(MouseButton::Left) {
|
||||
let Some(event) = mouse_button_event.read().next() else {
|
||||
return;
|
||||
};
|
||||
|
||||
if !(event.state.is_pressed() && event.button == MouseButton::Left) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ use self::hex::*;
|
|||
pub type TilePosition = HexPosition<i32>;
|
||||
|
||||
/// The tile of the map.
|
||||
#[derive(Component, Debug)]
|
||||
#[derive(Component, Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Tile {
|
||||
/// The hill tile.
|
||||
Hill,
|
||||
|
|
3
crates/border-wars/src/ui/mod.rs
Normal file
3
crates/border-wars/src/ui/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
//! TODO
|
||||
|
||||
pub mod tiles_info;
|
111
crates/border-wars/src/ui/tiles_info.rs
Normal file
111
crates/border-wars/src/ui/tiles_info.rs
Normal file
|
@ -0,0 +1,111 @@
|
|||
//! TODO
|
||||
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::map::click_tile::TileJustClicked;
|
||||
use crate::map::Tile;
|
||||
|
||||
/// TODO
|
||||
pub struct TilesInfoPlugin;
|
||||
|
||||
impl Plugin for TilesInfoPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Update, handle_tile_click)
|
||||
.init_resource::<SelectedTile>()
|
||||
.add_systems(Update, update_tile_info_text)
|
||||
.add_systems(OnEnter(crate::CurrentScene::Game), init_text_zone);
|
||||
}
|
||||
}
|
||||
|
||||
impl Tile {
|
||||
fn get_info_text(&self) -> String {
|
||||
match self {
|
||||
Tile::Hill => "This is a hill".to_string(),
|
||||
Tile::Grass => "This is grass".to_string(),
|
||||
Tile::Forest => "This is a forest".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Resource, Default)]
|
||||
enum SelectedTile {
|
||||
Tile(Tile, u32),
|
||||
#[default]
|
||||
None,
|
||||
}
|
||||
|
||||
impl SelectedTile {
|
||||
fn index(&self) -> Option<u32> {
|
||||
match self {
|
||||
SelectedTile::Tile(_, index) => Some(*index),
|
||||
SelectedTile::None => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_tile_click(
|
||||
mut event: EventReader<TileJustClicked>,
|
||||
mut query: Query<(&Tile, Entity, &mut Transform)>,
|
||||
mut selected: ResMut<SelectedTile>,
|
||||
) {
|
||||
if let Some(event) = event.read().last() {
|
||||
let save_selected = selected.index();
|
||||
for (_, entity, mut transform) in query.iter_mut() {
|
||||
if selected.index() == Some(entity.index()) {
|
||||
if event.0 == entity.index() {
|
||||
*selected = SelectedTile::None;
|
||||
}
|
||||
transform.translation.y -= 10.;
|
||||
}
|
||||
}
|
||||
|
||||
for (tile, entity, mut transform) in query.iter_mut() {
|
||||
if event.0 == entity.index() && save_selected != Some(event.0) {
|
||||
*selected = SelectedTile::Tile(*tile, entity.index());
|
||||
transform.translation.y += 10.;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
fn init_text_zone(mut commands: Commands) {
|
||||
commands
|
||||
.spawn(TextBundle {
|
||||
style: Style {
|
||||
position_type: PositionType::Absolute,
|
||||
|
||||
..default()
|
||||
},
|
||||
..default()
|
||||
})
|
||||
.insert(TileInfoText);
|
||||
}
|
||||
|
||||
/// TODO
|
||||
#[derive(Component)]
|
||||
pub struct TileInfoText;
|
||||
|
||||
fn update_tile_info_text(
|
||||
mut query: Query<(&mut Transform, &mut Text, &mut Visibility), With<TileInfoText>>,
|
||||
selected: Res<SelectedTile>,
|
||||
) {
|
||||
for (mut transform, mut text, mut visibility) in query.iter_mut() {
|
||||
if selected.index().is_none() {
|
||||
*visibility = Visibility::Hidden;
|
||||
return;
|
||||
}
|
||||
if let SelectedTile::Tile(tile, _) = *selected {
|
||||
text.sections = vec![TextSection {
|
||||
value: tile.get_info_text(),
|
||||
style: TextStyle {
|
||||
font_size: 20.0,
|
||||
color: Color::WHITE,
|
||||
..default()
|
||||
},
|
||||
}];
|
||||
*visibility = Visibility::Visible;
|
||||
transform.translation.z = 1.0;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue