Skip to content

Commit

Permalink
Merge pull request #446 from extrawurst/examples-testbed2
Browse files Browse the repository at this point in the history
testbed2d combining examples into one
  • Loading branch information
sebcrozet authored Nov 16, 2023
2 parents fdf7172 + 82a6f4e commit 8a3c744
Show file tree
Hide file tree
Showing 11 changed files with 283 additions and 56 deletions.
2 changes: 1 addition & 1 deletion bevy_rapier2d/examples/boxes2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
.run();
}

fn setup_graphics(mut commands: Commands) {
pub fn setup_graphics(mut commands: Commands) {
commands.spawn(Camera2dBundle {
transform: Transform::from_xyz(0.0, 20.0, 0.0),
..default()
Expand Down
8 changes: 5 additions & 3 deletions bevy_rapier2d/examples/debug_despawn2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Stats {
}

#[derive(Resource)]
struct Game {
pub struct Game {
n_lanes: usize,
n_rows: usize,
stats: Stats,
Expand Down Expand Up @@ -79,7 +79,9 @@ fn byte_rgb(r: u8, g: u8, b: u8) -> Color {
Color::rgb(r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0)
}

fn setup_game(mut commands: Commands, mut game: ResMut<Game>) {
pub fn setup_game(mut commands: Commands, mut game: ResMut<Game>) {
game.current_cube_joints = vec![];

game.cube_colors = vec![
byte_rgb(0, 244, 243),
byte_rgb(238, 243, 0),
Expand Down Expand Up @@ -219,7 +221,7 @@ fn spawn_block(
.id()
}

fn cube_sleep_detection(
pub fn cube_sleep_detection(
mut commands: Commands,
mut game: ResMut<Game>,
block_query: Query<(Entity, &GlobalTransform)>,
Expand Down
73 changes: 41 additions & 32 deletions bevy_rapier2d/examples/despawn2.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
use bevy::prelude::*;
use bevy_rapier2d::prelude::*;

#[derive(Component, Default)]
pub struct Despawn;
#[derive(Component, Default)]
pub struct Resize;

#[derive(Resource, Default)]
pub struct DespawnResource {
pub entities: Vec<Entity>,
timer: Timer,
}

#[derive(Resource, Default)]
pub struct ResizeResource {
pub entities: Vec<Entity>,
timer: Timer,
}

fn main() {
Expand All @@ -30,25 +35,27 @@ fn main() {
.run();
}

fn setup_graphics(mut commands: Commands) {
pub fn setup_graphics(
mut commands: Commands,
mut despawn: ResMut<DespawnResource>,
mut resize: ResMut<ResizeResource>,
) {
resize.timer = Timer::from_seconds(6.0, TimerMode::Once);
despawn.timer = Timer::from_seconds(5.0, TimerMode::Once);

commands.spawn(Camera2dBundle {
transform: Transform::from_xyz(0.0, 20.0, 0.0),
..default()
});
}

pub fn setup_physics(
mut commands: Commands,
mut despawn: ResMut<DespawnResource>,
mut resize: ResMut<ResizeResource>,
) {
pub fn setup_physics(mut commands: Commands) {
/*
* Ground
*/
let ground_size = 250.0;

let entity = commands.spawn(Collider::cuboid(ground_size, 12.0)).id();
despawn.entities.push(entity);
commands.spawn((Collider::cuboid(ground_size, 12.0), Despawn));

commands.spawn((
TransformBundle::from(Transform::from_xyz(ground_size, ground_size * 2.0, 0.0)),
Expand All @@ -75,39 +82,41 @@ pub fn setup_physics(
let x = i as f32 * shift - centerx;
let y = j as f32 * shift + centery + 2.0;

let entity = commands
.spawn((
TransformBundle::from(Transform::from_xyz(x, y, 0.0)),
RigidBody::Dynamic,
Collider::cuboid(rad, rad),
))
.id();
let mut entity = commands.spawn((
TransformBundle::from(Transform::from_xyz(x, y, 0.0)),
RigidBody::Dynamic,
Collider::cuboid(rad, rad),
));

if (i + j * num) % 100 == 0 {
resize.entities.push(entity);
entity.insert(Resize);
}
}
}
}

pub fn despawn(mut commands: Commands, time: Res<Time>, mut despawn: ResMut<DespawnResource>) {
if time.elapsed_seconds() > 5.0 {
for entity in &despawn.entities {
println!("Despawning ground entity");
commands.entity(*entity).despawn();
pub fn despawn(
mut commands: Commands,
time: Res<Time>,
mut despawn: ResMut<DespawnResource>,
query: Query<Entity, With<Despawn>>,
) {
if despawn.timer.tick(time.delta()).just_finished() {
for e in &query {
commands.entity(e).despawn();
}
despawn.entities.clear();
}
}

pub fn resize(mut commands: Commands, time: Res<Time>, mut resize: ResMut<ResizeResource>) {
if time.elapsed_seconds() > 6.0 {
for entity in &resize.entities {
println!("Resizing a block");
commands
.entity(*entity)
.insert(Collider::cuboid(20.0, 20.0));
pub fn resize(
mut commands: Commands,
time: Res<Time>,
mut resize: ResMut<ResizeResource>,
query: Query<Entity, With<Resize>>,
) {
if resize.timer.tick(time.delta()).just_finished() {
for e in &query {
commands.entity(e).insert(Collider::cuboid(20.0, 20.0));
}
resize.entities.clear();
}
}
4 changes: 2 additions & 2 deletions bevy_rapier2d/examples/events2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ fn main() {
.run();
}

fn setup_graphics(mut commands: Commands) {
pub fn setup_graphics(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
}

fn display_events(
pub fn display_events(
mut collision_events: EventReader<CollisionEvent>,
mut contact_force_events: EventReader<ContactForceEvent>,
) {
Expand Down
2 changes: 1 addition & 1 deletion bevy_rapier2d/examples/joints2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
.run();
}

fn setup_graphics(mut commands: Commands) {
pub fn setup_graphics(mut commands: Commands) {
commands.spawn(Camera2dBundle {
transform: Transform::from_xyz(0.0, -200.0, 0.0),
..default()
Expand Down
31 changes: 20 additions & 11 deletions bevy_rapier2d/examples/joints_despawn2.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use bevy::prelude::*;
use bevy_rapier2d::prelude::*;

#[derive(Component, Default)]
pub struct Despawn;

#[derive(Resource, Default)]
pub struct DespawnResource {
entities: Vec<Entity>,
timer: Timer,
}

fn main() {
Expand All @@ -24,14 +27,16 @@ fn main() {
.run();
}

fn setup_graphics(mut commands: Commands) {
pub fn setup_graphics(mut commands: Commands) {
commands.spawn(Camera2dBundle {
transform: Transform::from_xyz(0.0, -200.0, 0.0),
..default()
});
}

pub fn setup_physics(mut commands: Commands, mut despawn: ResMut<DespawnResource>) {
despawn.timer = Timer::from_seconds(4.0, TimerMode::Once);

// Build the rigid body.
let rad = 4.0;
let numi = 40; // Num vertical nodes.
Expand Down Expand Up @@ -67,9 +72,9 @@ pub fn setup_physics(mut commands: Commands, mut despawn: ResMut<DespawnResource
// NOTE: we want to attach multiple impulse joints to this entity, so
// we need to add the components to children of the entity. Otherwise
// the second joint component would just overwrite the first one.
let entity = cmd.spawn(ImpulseJoint::new(parent_entity, joint)).id();
let mut entity = cmd.spawn(ImpulseJoint::new(parent_entity, joint));
if i == (numi / 2) || (k % 4 == 0 || k == numk - 1) {
despawn.entities.push(entity);
entity.insert(Despawn);
}
});
}
Expand All @@ -83,9 +88,9 @@ pub fn setup_physics(mut commands: Commands, mut despawn: ResMut<DespawnResource
// NOTE: we want to attach multiple impulse joints to this entity, so
// we need to add the components to children of the entity. Otherwise
// the second joint component would just overwrite the first one.
let entity = cmd.spawn(ImpulseJoint::new(parent_entity, joint)).id();
let mut entity = cmd.spawn(ImpulseJoint::new(parent_entity, joint));
if i == (numi / 2) || (k % 4 == 0 || k == numk - 1) {
despawn.entities.push(entity);
entity.insert(Despawn);
}
});
}
Expand All @@ -95,12 +100,16 @@ pub fn setup_physics(mut commands: Commands, mut despawn: ResMut<DespawnResource
}
}

pub fn despawn(mut commands: Commands, time: Res<Time>, mut despawn: ResMut<DespawnResource>) {
if time.elapsed_seconds() > 4.0 {
for entity in &despawn.entities {
pub fn despawn(
mut commands: Commands,
time: Res<Time>,
mut despawn: ResMut<DespawnResource>,
query: Query<Entity, With<Despawn>>,
) {
if despawn.timer.tick(time.delta()).just_finished() {
for e in &query {
println!("Despawning joint entity");
commands.entity(*entity).despawn();
commands.entity(e).despawn();
}
despawn.entities.clear();
}
}
2 changes: 1 addition & 1 deletion bevy_rapier2d/examples/locked_rotations2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
.run();
}

fn setup_graphics(mut commands: Commands) {
pub fn setup_graphics(mut commands: Commands) {
commands.spawn(Camera2dBundle {
transform: Transform::from_xyz(0.0, 200.0, 0.0),
..default()
Expand Down
2 changes: 1 addition & 1 deletion bevy_rapier2d/examples/multiple_colliders2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
.run();
}

fn setup_graphics(mut commands: Commands) {
pub fn setup_graphics(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
}

Expand Down
6 changes: 3 additions & 3 deletions bevy_rapier2d/examples/player_movement2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ fn main() {

// The float value is the player movement speed in 'pixels/second'.
#[derive(Component)]
struct Player(f32);
pub struct Player(f32);

fn spawn_player(mut commands: Commands, mut rapier_config: ResMut<RapierConfiguration>) {
pub fn spawn_player(mut commands: Commands, mut rapier_config: ResMut<RapierConfiguration>) {
// Set gravity to 0.0 and spawn camera.
rapier_config.gravity = Vec2::ZERO;
commands.spawn(Camera2dBundle::default());
Expand All @@ -48,7 +48,7 @@ fn spawn_player(mut commands: Commands, mut rapier_config: ResMut<RapierConfigur
));
}

fn player_movement(
pub fn player_movement(
keyboard_input: Res<Input<KeyCode>>,
mut player_info: Query<(&Player, &mut Velocity)>,
) {
Expand Down
2 changes: 1 addition & 1 deletion bevy_rapier2d/examples/rope_joint2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
.run();
}

fn setup_graphics(mut commands: Commands) {
pub fn setup_graphics(mut commands: Commands) {
commands.spawn(Camera2dBundle {
transform: Transform::from_xyz(0.0, -200.0, 0.0),
..default()
Expand Down
Loading

0 comments on commit 8a3c744

Please sign in to comment.