Crates.io | bevy_scrolling_2d_camera |
lib.rs | bevy_scrolling_2d_camera |
version | 0.3.0 |
source | src |
created_at | 2024-06-04 05:41:24.760431 |
updated_at | 2024-08-10 13:14:38.272537 |
description | A simple 2d camera plugin for bevy supporting scrolling with right mouse dragging. |
homepage | |
repository | https://github.com/xavierwoo/bevy_scrolling_2d_camera.git |
max_upload_size | |
id | 1260746 |
size | 11,166 |
Here is a simple 2d camera plugin for bevy engine, supporting scrolling with mouse right dragging and zooming with mouse wheel.
It is suitable for RTS or Simulation 2D games.
Check the cargo project in example folder to see how it works.
bevy | bevy_scrolling_2d_camera |
---|---|
0.14.1 | 0.3.0 |
0.13 | ^0.1.* |
By pressing down the right mouse button, the plugin records the cursor position. The vector from this record position to the current cursor position is used as the camera velocity. The camera updates its translation based on the calculated veclocity.
This plugin is minimum, but it adds the following things to your project.
#[derive(Resource)]
pub struct ScrollingCamera{
pub entity: Option<Entity>,
}
#[derive(Resource)]
pub struct CameraVelocity{
pub v: Vec3,
}
#[derive(Resource)]
pub struct CapturedMouseRightClickPosition{
pub pos: Option<Vec2>,
}
#[derive(Resource)]
pub struct ZoomBound{
pub max: f32,
pub min: f32,
}
#[derive(States, Debug, Clone, Copy, Eq, PartialEq, Hash, Default)]
pub enum CameraState {
#[default]
Idling,
Scrolling,
}
pub fn camera_move(
mut query: Query<&mut Transform, With<Camera>>,
velocity: Res<CameraVelocity>,
time: Res<Time>,
scrolling_camera: Res<ScrollingCamera>,
) {/*...*/}
pub fn capture_mouse_right_click_for_scrolling(
mut commands: Commands,
windows: Query<&Window>,
input: Res<ButtonInput<MouseButton>>,
mut click_pos : ResMut<CapturedMouseRightClickPosition>,
) {/*...*/}
pub fn control_camera_movment(
mut commands: Commands,
window_query: Query<&Window>,
camera_query: Query<(&Camera, &GlobalTransform)>,
input: Res<ButtonInput<MouseButton>>,
click_pos : Res<CapturedMouseRightClickPosition>,
mut velocity: ResMut<CameraVelocity>,
mut gizmos: Gizmos,
scrolling_camera: Res<ScrollingCamera>,
){/*...*/}
pub fn camera_zoom(
mut mouse_wheel_event: EventReader<MouseWheel>,
mut query: Query<&mut OrthographicProjection>,
time: Res<Time>,
scrolling_camera: Res<ScrollingCamera>,
) {/*...*/}