Skip to content

Commit

Permalink
Transition to using quads for hitbox visualizations
Browse files Browse the repository at this point in the history
This allows us to remove the second camera, as no 2D objects are
rendered besides the UI, which renders just fine with t he 3D camera.
  • Loading branch information
haihala committed Sep 18, 2023
1 parent d6762be commit 5094efe
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 65 deletions.
55 changes: 11 additions & 44 deletions client/lib/src/camera.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use bevy::core_pipeline::clear_color::ClearColorConfig;
use bevy::prelude::*;
use wag_core::Player;

Expand All @@ -14,53 +13,21 @@ pub struct CustomCameraPlugin;

impl Plugin for CustomCameraPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, add_cameras)
app.add_systems(Startup, add_camera)
.add_systems(Update, center_camera);
}
}

fn add_cameras(mut commands: Commands) {
let projection = PerspectiveProjection { ..default() };

let camera_container = commands
.spawn((
SpatialBundle {
transform: Transform::from_xyz(0.0, 1.8, 10.0),
..default()
},
Name::new("Cameras"),
WorldCamera,
))
.id();

commands
.spawn((
Camera3dBundle {
projection: projection.clone().into(),
..default()
},
Name::new("3d Cam"),
))
.set_parent(camera_container);

commands
.spawn((
Camera2dBundle {
transform: Transform::from_translation(Vec3::ZERO),
camera: Camera {
// Higher is rendered later
order: 1,
..default()
},
camera_2d: Camera2d {
// Don't draw a clear color on top of the 3d stuff
clear_color: ClearColorConfig::None,
},
..default()
},
Name::new("2d Cam"),
))
.set_parent(camera_container);
fn add_camera(mut commands: Commands) {
commands.spawn((
Camera3dBundle {
transform: Transform::from_xyz(0.0, 1.8, 5.0),
projection: PerspectiveProjection { ..default() }.into(),
..default()
},
Name::new("Cameras"),
WorldCamera,
));
}

#[allow(clippy::type_complexity)]
Expand Down
52 changes: 31 additions & 21 deletions client/lib/src/dev/box_visualization.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy::prelude::*;
use bevy::prelude::{shape::Quad, *};
use characters::{Hitbox, Hurtbox};

use crate::{assets::Colors, physics::Pushbox};
Expand All @@ -12,77 +12,84 @@ pub(super) enum BoxVisual {

pub(super) fn spawn_boxes(
mut commands: Commands,
mut mesh_assets: ResMut<Assets<Mesh>>,
mut material_assets: ResMut<Assets<StandardMaterial>>,
colors: Res<Colors>,
hitboxes: Query<(Entity, &Hitbox, Option<&Children>)>,
hurtboxes: Query<(Entity, &Hurtbox, Option<&Children>)>,
pushboxes: Query<(Entity, &Pushbox, Option<&Children>)>,
sprites: Query<&Sprite, With<BoxVisual>>,
spawned: Query<(), With<BoxVisual>>,
) {
for (entity, hitbox, maybe_children) in &hitboxes {
handle_box_spawning(
&mut commands,
&sprites,
&mut mesh_assets,
&mut material_assets,
&spawned,
entity,
maybe_children,
BoxVisual::Hitbox,
colors.hitbox,
hitbox.center(),
Some(hitbox.size()),
hitbox.size(),
);
}
for (entity, hurtbox, maybe_children) in &hurtboxes {
handle_box_spawning(
&mut commands,
&sprites,
&mut mesh_assets,
&mut material_assets,
&spawned,
entity,
maybe_children,
BoxVisual::Hurtbox,
colors.hurtbox,
hurtbox.center(),
Some(hurtbox.size()),
hurtbox.size(),
);
}
for (entity, pushbox, maybe_children) in &pushboxes {
handle_box_spawning(
&mut commands,
&sprites,
&mut mesh_assets,
&mut material_assets,
&spawned,
entity,
maybe_children,
BoxVisual::Pushbox,
colors.pushbox,
pushbox.center(),
Some(pushbox.size()),
pushbox.size(),
);
}
}

#[allow(clippy::too_many_arguments)]
fn handle_box_spawning(
commands: &mut Commands,
sprites: &Query<&Sprite, With<BoxVisual>>,
mesh_assets: &mut ResMut<Assets<Mesh>>,
material_assets: &mut ResMut<Assets<StandardMaterial>>,
spawned: &Query<(), With<BoxVisual>>,
entity: Entity,
maybe_children: Option<&Children>,
marker: BoxVisual,
color: Color,
offset: Vec2,
custom_size: Option<Vec2>,
custom_size: Vec2,
) {
// Only add visual to components with no children with the marker
if maybe_children.is_none()
|| !maybe_children
.unwrap()
.iter()
.any(|e| sprites.get(*e).is_ok())
.any(|e| spawned.get(*e).is_ok())
{
// If there is no child entity that has the marker component and a sprite (has been handled)
commands.entity(entity).with_children(|parent| {
parent.spawn((
SpriteBundle {
PbrBundle {
transform: Transform::from_translation(offset.extend(0.0)),
sprite: Sprite {
color,
custom_size,
..default()
},
material: material_assets.add(color.into()),
mesh: mesh_assets.add(Quad::new(custom_size).into()),
..default()
},
marker,
Expand All @@ -95,15 +102,18 @@ pub(super) fn size_adjustment(
players: Query<&Pushbox>,
hitboxes: Query<&Hitbox>,
hurtboxes: Query<&Hurtbox>,
mut sprites: Query<(&mut Sprite, &mut Transform, &BoxVisual, &Parent)>,
mut mesh_assets: ResMut<Assets<Mesh>>,
mut spawned: Query<(&Handle<Mesh>, &mut Transform, &BoxVisual, &Parent)>,
) {
for (mut sprite, mut tf, box_type, parent) in &mut sprites {
for (mesh_handle, mut tf, box_type, parent) in &mut spawned {
let area = match box_type {
BoxVisual::Hurtbox => hurtboxes.get(**parent).unwrap().0,
BoxVisual::Hitbox => hitboxes.get(**parent).unwrap().0,
BoxVisual::Pushbox => players.get(**parent).unwrap().0,
};
sprite.custom_size = Some(area.size());
tf.translation = area.center().extend(0.0);
if let Some(mesh) = mesh_assets.get_mut(mesh_handle) {
*mesh = Quad::new(area.size()).into();
}
}
}

0 comments on commit 5094efe

Please sign in to comment.