mouse camera controls

This commit is contained in:
2024-07-11 20:23:34 +02:00
parent 4ad09802b8
commit 2bb8cbc852
2 changed files with 52 additions and 39 deletions

View File

@@ -86,7 +86,13 @@ impl Plugin for GridPlugin {
app.add_systems(Startup, generate_grid);
app.add_systems(
First,
(camera::movement, update_cursor_pos, click_tile).chain(),
(
camera::mouse_movement,
camera::mouse_zoom,
update_cursor_pos,
click_tile,
)
.chain(),
);
app.add_systems(Update, update_tiles);
}
@@ -131,7 +137,6 @@ fn generate_grid(mut commands: Commands, asset_server: Res<AssetServer>) {
});
}
// This is where we check which tile the cursor is hovered over.
fn click_tile(
buttons: Res<ButtonInput<MouseButton>>,
cursor_pos: Res<CursorPos>,
@@ -146,18 +151,13 @@ fn click_tile(
let offset = offset.single();
for (map_size, grid_size, tile_storage, map_transform) in tilemap_q.iter() {
// Grab the cursor position from the `Res<CursorPos>`
let cursor_pos: Vec2 = cursor_pos.0;
// We need to make sure that the cursor's world position is correct relative to the map
// due to any map transformation.
let cursor_in_map_pos: Vec2 = {
// Extend the cursor_pos vec3 by 0.0 and 1.0
let cursor_pos = Vec4::from((cursor_pos, 0.0, 1.0));
let cursor_in_map_pos = map_transform.compute_matrix().inverse() * cursor_pos;
cursor_in_map_pos.xy()
};
// Once we have a world position we can transform it into a possible tile position.
let Some(tile_pos) = TilePos::from_world_pos(
&cursor_in_map_pos,
map_size,