-
Notifications
You must be signed in to change notification settings - Fork 1
Feature/3/camera system #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Cartier32
wants to merge
4
commits into
main
Choose a base branch
from
feature/3/camera-system
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod camera_plugin; |
Cartier32 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use avian2d::prelude::*; | ||
use bevy::{input::mouse::MouseWheel, prelude::*}; | ||
|
||
pub struct CameraPlugin; | ||
|
||
///Component that marks the Camera's current target | ||
#[derive(Component)] | ||
pub struct CameraTarget; | ||
|
||
///Component that marks the main camera | ||
#[derive(Component)] | ||
pub struct MainCamera; | ||
|
||
impl Plugin for CameraPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_systems(Startup, setup_main_camera); | ||
app.add_systems( | ||
PostUpdate, | ||
update_camera | ||
.after(PhysicsSet::Sync) | ||
.before(TransformSystem::TransformPropagate), | ||
); | ||
app.add_systems(Update, zoom_camera); | ||
} | ||
} | ||
|
||
///Sets up a 2d camera and attaches the MainCamera marker to it | ||
fn setup_main_camera(mut commands: Commands) { | ||
commands.spawn((Camera2dBundle::default(), MainCamera)); | ||
} | ||
|
||
///Update camera function will have the main camera follow any entity with the CameraTarget tag | ||
fn update_camera( | ||
mut camera_query: Query<&mut Transform, (With<MainCamera>, Without<CameraTarget>)>, | ||
target_query: Query<&Transform, (With<CameraTarget>, Without<MainCamera>)>, | ||
) { | ||
let num_targets = target_query.iter().len(); | ||
|
||
if (num_targets > 1) { | ||
error!("More than one entity with CameraTarget Component!"); | ||
return; | ||
} | ||
|
||
let Ok(mut camera) = camera_query.get_single_mut() else { | ||
error!("Could not execute query for single MainCamera component"); | ||
return; | ||
Cartier32 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
let Ok(target) = target_query.get_single() else { | ||
return; | ||
Cartier32 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
let Vec3 { x, y, .. } = target.translation; | ||
|
||
let direction = Vec3::new(x, y, camera.translation.z); | ||
|
||
camera.translation = direction; | ||
} | ||
|
||
///Camera Zooming Function, uses MouseWheel Scrolling up/down for zooming in/out | ||
fn zoom_camera( | ||
mut scroll_event: EventReader<MouseWheel>, | ||
mut camera: Query<&mut OrthographicProjection, With<MainCamera>>, | ||
) { | ||
//Zoom Sensitivity | ||
let zoom_factor = 0.1; | ||
|
||
//Amount to zoom in this update cycle | ||
let mut zoom_amount = 0.0; | ||
|
||
for event in scroll_event.read() { | ||
zoom_amount += -event.y * zoom_factor | ||
} | ||
|
||
if zoom_amount != 0.0 { | ||
for mut projection in camera.iter_mut() { | ||
projection.scale += zoom_amount; | ||
|
||
//This statement makes sure that the amount of zoom is capped, can change if need be | ||
projection.scale = projection.scale.clamp(0.1, 5.0); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.