Skip to content

Commit

Permalink
refactor(camera): replace map_range with remap
Browse files Browse the repository at this point in the history
Replace the custom map_range function with the remap function from the
glam crate to simplify the code.
  • Loading branch information
ShenMian committed Jul 20, 2024
1 parent 4e230a8 commit d244718
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/camera.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bevy::glam::remap;
use bevy::{prelude::*, render::camera::ScalingMode};

use crate::{tilemap::Map, GameState};
Expand Down Expand Up @@ -52,23 +53,25 @@ pub fn update(
};

if let Some(cursor_position) = window.cursor_position() {
fn map_range(from: (f32, f32), to: (f32, f32), value: f32) -> f32 {
to.0 + (value - from.0) * (to.1 - to.0) / (from.1 - from.0)
}

let cursor_position = Vec2::new(cursor_position.x, cursor_position.y);
let half_viewport_size = window.resolution.size() / 2.;
let center_to_cursor = cursor_position - half_viewport_size;
let normalized_length = center_to_cursor / half_viewport_size;

if normalized_length.x.abs() >= 0.8 {
transform.translation.x += time.delta_seconds()
* map_range((0.8, 1.), (min_speed, max_speed), normalized_length.x.abs())
* normalized_length
.x
.abs()
.remap(0.8, 1., min_speed, max_speed)
.copysign(normalized_length.x);
}
if normalized_length.y.abs() >= 0.8 {
transform.translation.y -= time.delta_seconds()
* map_range((0.8, 1.), (min_speed, max_speed), normalized_length.y.abs())
* normalized_length
.y
.abs()
.remap(0.8, 1., min_speed, max_speed)
.copysign(normalized_length.y);
}
}
Expand Down

0 comments on commit d244718

Please sign in to comment.