Skip to content
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

♻️ extract camera #5

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[workspace]

[dependencies]
bevy = "0.11.3"
bevy_rapier2d = "0.22.0"
rand = "0.8.5"
bevy_camera = { path = "camera" }

# Enable a small amount of optimization in debug mode
[profile.dev]
Expand Down
7 changes: 7 additions & 0 deletions camera/Cargo.toml
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"
61 changes: 61 additions & 0 deletions camera/src/lib.rs
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;
}
}
}
}
27 changes: 3 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,16 @@ use bevy::{
prelude::*,
DefaultPlugins,
};
use bevy_camera::CameraPlugin;
use bevy_rapier2d::prelude::*;
use castles::CastlesPlugin;
use health::HealthPlugin;
use minions::MinionsPlugin;
use physics::PhysicsPlugin;
use player::{LocalPlayer, LocalPlayerPlugin};
use player::LocalPlayerPlugin;
use racks::RacksPlugin;
use teams::TeamsPlugin;

#[derive(Component)]
struct Camera;

fn main() {
let mut app = App::new();

Expand All @@ -40,6 +38,7 @@ fn main() {
CastlesPlugin,
HealthPlugin,
LocalPlayerPlugin,
CameraPlugin,
))
// --- physics ---
.add_plugins((
Expand All @@ -51,25 +50,5 @@ fn main() {
gravity: Vec2::new(0.0, 0.0),
..default()
})
// --- systems ---
.add_systems(Startup, setup)
.add_systems(Update, cameraman)
.run();
}

fn setup(mut commands: Commands) {
// Camera
commands.spawn((Camera2dBundle::default(), Camera {}));
}

fn cameraman(
mut query_camera: Query<&mut Transform, (With<Camera>, Without<LocalPlayer>)>,
query_player: Query<&Transform, With<LocalPlayer>>,
) {
if let Ok(mut camera_transform) = query_camera.get_single_mut() {
if let Ok(player_transform) = query_player.get_single() {
camera_transform.translation.x = player_transform.translation.x;
camera_transform.translation.y = player_transform.translation.y;
}
}
}
9 changes: 7 additions & 2 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use bevy::{
sprite::{Sprite, SpriteBundle},
time::Time,
};
use bevy_camera::{Camera, CameraBundle, Target};
use bevy_rapier2d::prelude::*;

const DEFAULT_HAND_COLOR: Color = Color::rgb(0.8, 0.25, 0.24);
Expand Down Expand Up @@ -107,7 +108,7 @@ fn setup(

let team = teams.get_expect("a".into());

commands
let entity = commands
.spawn((
MaterialMesh2dBundle {
mesh: meshes.add(shape::Circle::new(30.).into()).into(),
Expand Down Expand Up @@ -139,6 +140,7 @@ fn setup(
.with_health_bar_position(Vec3::new(0.0, 40.0, 0.1))
.with_health_bar_size(Vec2::new(50.0, 5.0)),
Name("local_player".to_string()),
Target,
team,
))
.with_children(|parent| {
Expand All @@ -154,7 +156,10 @@ fn setup(
},
Hand {},
));
});
})
.id();

commands.spawn(CameraBundle::new_with_default_bundle(entity));
}

fn setup_ui(mut commands: Commands, asset_server: Res<AssetServer>) {
Expand Down