-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef9d564
commit 3261d0b
Showing
6 changed files
with
89 additions
and
26 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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,7 @@ | ||
[package] | ||
name = "bevy_camera" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
bevy = "0.11.3" |
This file contains 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,61 @@ | ||
use bevy::prelude::{ | ||
App, Bundle, Camera2dBundle, Component, Entity, Plugin, PostUpdate, Query, Transform, With, | ||
Without, | ||
}; | ||
|
||
#[derive(Component)] | ||
pub struct Target; | ||
|
||
#[derive(Component)] | ||
pub struct Camera(pub Entity); | ||
|
||
#[derive(Bundle)] | ||
pub struct CameraBundle { | ||
camera: Camera, | ||
bundle: Camera2dBundle, | ||
} | ||
|
||
impl CameraBundle { | ||
pub fn new(target: Entity, bundle: Camera2dBundle) -> Self { | ||
Self { | ||
camera: Camera(target), | ||
bundle, | ||
} | ||
} | ||
|
||
pub fn new_with_default_bundle(target: Entity) -> Self { | ||
Self { | ||
camera: Camera(target), | ||
bundle: Camera2dBundle::default(), | ||
} | ||
} | ||
} | ||
|
||
pub struct CameraPlugin; | ||
|
||
impl Plugin for CameraPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_systems(PostUpdate, cameraman); | ||
} | ||
} | ||
|
||
fn cameraman( | ||
mut query_camera: Query<(&mut Transform, &Camera), Without<Target>>, | ||
query_targets: Query<(&Transform, Entity), With<Target>>, | ||
) { | ||
for (mut camera_transform, camera) in &mut query_camera { | ||
for (target_transform, target_entity) in &query_targets { | ||
if camera.0 != target_entity { | ||
continue; | ||
} | ||
|
||
// TODO: for now we follow the first target but we could think of doing an average positions of all the targets | ||
if camera.0 == target_entity { | ||
camera_transform.translation.x = target_transform.translation.x; | ||
camera_transform.translation.y = target_transform.translation.y; | ||
|
||
break; | ||
} | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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