diff --git a/assets/mole_unit.png b/assets/mole_unit.png new file mode 100644 index 0000000..8426ec6 Binary files /dev/null and b/assets/mole_unit.png differ diff --git a/src/main.rs b/src/main.rs index 54f6e36..4051811 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,27 @@ const TILE_SIZE: TilemapTileSize = TilemapTileSize { y: TILE_HEIGHT, }; +#[derive(Component)] +struct Unit { + health: u32, + position: Vec2, + digging_power: u32, +} + +#[derive(Component)] +struct MoleUnit; + +#[derive(Component)] +struct Player; + +#[derive(Component)] +struct Selected; + +#[derive(Resource)] +struct BaseTilePositions { + positions: Vec, +} + fn startup(mut commands: Commands, asset_server: Res) { commands.spawn(Camera2d); @@ -42,10 +63,13 @@ fn startup(mut commands: Commands, asset_server: Res) { } } + let mut base_positions = Vec::new(); + for x in 0..3 { for y in 11..14 { if x < MAP_SIZE.x && y < MAP_SIZE.y { let tile_pos = TilePos { x, y }; + base_positions.push(tile_pos); let tile_entity = commands .spawn(TileBundle { position: tile_pos, @@ -62,6 +86,7 @@ fn startup(mut commands: Commands, asset_server: Res) { for y in 11..14 { if x < MAP_SIZE.x && y < MAP_SIZE.y { let tile_pos = TilePos { x, y }; + base_positions.push(tile_pos); let tile_entity = commands .spawn(TileBundle { position: tile_pos, @@ -74,6 +99,10 @@ fn startup(mut commands: Commands, asset_server: Res) { } } + commands.insert_resource(BaseTilePositions { + positions: base_positions, + }); + commands.entity(tilemap_entity).insert(TilemapBundle { grid_size: TILE_SIZE.into(), map_type: TilemapType::default(), @@ -99,6 +128,117 @@ fn startup(mut commands: Commands, asset_server: Res) { }); } +fn setup_player(mut commands: Commands, asset_server: Res) { + let tile_pos = TilePos { x: 1, y: 12 }; + let world_pos = tile_pos_to_world_pos(tile_pos); + + commands.spawn(( + Sprite { + image: asset_server.load("mole_unit.png"), + custom_size: Some(Vec2::new(TILE_WIDTH, TILE_HEIGHT)), + ..Default::default() + }, + Transform::from_translation(Vec3::new(world_pos.x, world_pos.y, 2.0)), + Player, + MoleUnit {}, + Unit { + health: 100, + position: world_pos, + digging_power: 5, + }, + )); +} + +fn tile_pos_to_world_pos(tile_pos: TilePos) -> Vec2 { + let half_map_width = (TILEMAP_WIDTH as f32 * TILE_WIDTH) / 2.0; + let half_map_height = (TILEMAP_HEIGHT as f32 * TILE_HEIGHT) / 2.0; + + Vec2::new( + (tile_pos.x as f32 * TILE_WIDTH) - half_map_width + (TILE_WIDTH / 2.0), + (tile_pos.y as f32 * TILE_HEIGHT) - half_map_height + (TILE_HEIGHT / 2.0), + ) +} + +fn world_pos_to_tile_pos(world_pos: Vec2) -> TilePos { + let half_map_width = (TILEMAP_WIDTH as f32 * TILE_WIDTH) / 2.0; + let half_map_height = (TILEMAP_HEIGHT as f32 * TILE_HEIGHT) / 2.0; + + let x = ((world_pos.x + half_map_width - (TILE_WIDTH / 2.0)) / TILE_WIDTH).floor() as u32; + let y = ((world_pos.y + half_map_height - (TILE_HEIGHT / 2.0)) / TILE_HEIGHT).floor() as u32; + + TilePos { x, y } +} + +fn handle_mouse_input( + mut commands: Commands, + mouse_button: Res>, + windows: Query<&Window>, + camera_q: Query<(&Camera, &GlobalTransform)>, + mut player_query: Query<(Entity, &mut Transform, &mut Unit), (With, With)>, + selected_query: Query, With, With)>, + base_tiles: Res, +) { + let Some(window) = windows.iter().next() else { + return; + }; + + let Some((camera, camera_transform)) = camera_q.iter().next() else { + return; + }; + + if let Some(cursor_position) = window.cursor_position() { + if let Ok(world_pos) = camera.viewport_to_world_2d(camera_transform, cursor_position) { + if mouse_button.just_pressed(MouseButton::Left) { + for entity in selected_query.iter() { + commands.entity(entity).remove::(); + } + + for (entity, transform, _) in player_query.iter() { + let distance = transform.translation.truncate().distance(world_pos); + if distance < TILE_WIDTH / 2.0 { + commands.entity(entity).insert(Selected); + println!("Zaznaczono jednostkę gracza"); + break; + } + } + } + + if mouse_button.just_pressed(MouseButton::Right) { + if let Some(selected_entity) = selected_query.iter().next() { + let target_tile_pos = world_pos_to_tile_pos(world_pos); + + if base_tiles.positions.contains(&target_tile_pos) { + if let Ok((_, mut transform, mut unit)) = + player_query.get_mut(selected_entity) + { + let new_world_pos = tile_pos_to_world_pos(target_tile_pos); + transform.translation.x = new_world_pos.x; + transform.translation.y = new_world_pos.y; + unit.position = new_world_pos; + println!("Przeniesiono jednostkę na pozycję {:?}", target_tile_pos); + } + } else { + println!("Nie można się przemieścić na ten tile - to nie jest base tile!"); + } + } + } + } + } +} + +fn highlight_selected_unit( + mut gizmos: Gizmos, + selected_query: Query<&Transform, (With, With)>, +) { + for transform in selected_query.iter() { + gizmos.circle_2d( + transform.translation.truncate(), + TILE_WIDTH / 2.0 + 2.0, + Color::srgb(1.0, 1.0, 0.0), + ); + } +} + fn main() { App::new() .add_plugins( @@ -118,6 +258,7 @@ fn main() { .set(ImagePlugin::default_nearest()), ) .add_plugins(TilemapPlugin) - .add_systems(Startup, startup) + .add_systems(Startup, (startup, setup_player)) + .add_systems(Update, (handle_mouse_input, highlight_selected_unit)) .run(); }