From 87c502b5e8291b7a30607cee1461b49916fa20e3 Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Mon, 28 Jul 2025 21:24:09 -0700 Subject: [PATCH] Mostly 2d converted, little jank left to fix up but thats just polish --- examples/game2d.rs | 32 ++++++++++++++++++ src/base_game.rs | 2 ++ src/bin/flappy/main.rs | 73 +++++++++++++++++++++--------------------- src/physics2d.rs | 2 +- 4 files changed, 71 insertions(+), 38 deletions(-) create mode 100644 examples/game2d.rs diff --git a/examples/game2d.rs b/examples/game2d.rs new file mode 100644 index 0000000..41ace00 --- /dev/null +++ b/examples/game2d.rs @@ -0,0 +1,32 @@ +use games::*; + +fn main() { + App::new() + .add_plugins(( + BaseGamePlugin { + name: "2d game example".into(), + game_type: GameType::Two, + }, + )).add_systems(Startup, spawn_guy).run(); +} + +fn spawn_guy( + mut commands: Commands, + server: Res, + mut meshes: ResMut>, + mut materials: ResMut>, +) { + let material = MeshMaterial2d(materials.add(ColorMaterial { + texture: Some(server.load("flappy/bevy.png")), + ..default() + })); + + let mesh = Mesh2d(meshes.add(Rectangle::new(100.0, 100.0))); + + let name = Name::new("lil guy"); + + let t = Transform::from_xyz(0.0, 0.0, 0.0); + + commands.spawn((name, material, mesh, t)); +} + diff --git a/src/base_game.rs b/src/base_game.rs index 2a60c77..518b6af 100644 --- a/src/base_game.rs +++ b/src/base_game.rs @@ -59,6 +59,7 @@ pub fn toggle_state_visibility( pub fn create_camera_3d(mut commands: Commands) { // 3d camera + info!("Spawning 3d camera"); commands.spawn(( Camera3d { ..default() }, Camera { ..default() }, @@ -68,5 +69,6 @@ pub fn create_camera_3d(mut commands: Commands) { pub fn create_camera_2d(mut commands: Commands) { // 2d camera + info!("Spawning 2d camera"); commands.spawn((Camera2d, Camera { ..default() })); } diff --git a/src/bin/flappy/main.rs b/src/bin/flappy/main.rs index 566a2e6..0d45e56 100644 --- a/src/bin/flappy/main.rs +++ b/src/bin/flappy/main.rs @@ -1,7 +1,7 @@ // Bevy basically forces "complex types" with Querys #![allow(clippy::type_complexity)] -use games::physics3d::*; +use games::physics2d::*; use games::*; fn main() { @@ -9,10 +9,11 @@ fn main() { .add_plugins(( BaseGamePlugin { name: "flappy bird (with rewind)".into(), - ..default() + game_type: GameType::Two, }, - Physics3dPlugin, + Physics2dPlugin, )) + .insert_resource(Gravity(Vec2::NEG_Y * 9.8 * 100.0)) .init_state::() .add_systems( Startup, @@ -20,7 +21,7 @@ fn main() { init_bird, init_obstacles, init_ui, - tweak_camera.after(create_camera_3d), + tweak_camera.after(create_camera_2d), ), ) .add_systems(OnEnter(PlayerState::Alive), alive_bird) @@ -68,13 +69,10 @@ fn main() { .run(); } -fn tweak_camera(mut camera: Query<(&mut Camera, &mut AmbientLight, &mut Transform), With>) { - camera.iter_mut().for_each(|(mut c, mut al, mut t)| { - c.clear_color = ClearColorConfig::Custom(WHITE.into()); - al.brightness = 100.0; - // move the camera "back" so everything else is at 0 on the Z axis - t.translation.z = 10.0; - }); +fn tweak_camera(mut camera: Single<&mut Camera>) { + info!("Tweaking camera"); + let mut c = camera.into_inner(); + c.clear_color = ClearColorConfig::Custom(WHITE.into()); } #[derive(Component)] @@ -102,27 +100,26 @@ fn init_bird( mut commands: Commands, server: Res, mut meshes: ResMut>, - mut materials: ResMut>, + mut materials: ResMut>, ) { - let material = MeshMaterial3d(materials.add(StandardMaterial { - base_color_texture: Some(server.load("flappy/bevy.png")), - base_color: WHITE.into(), - alpha_mode: AlphaMode::Blend, + let material = MeshMaterial2d(materials.add(ColorMaterial { + texture: Some(server.load("flappy/bevy.png")), + color: WHITE.into(), + alpha_mode: AlphaMode2d::Blend, ..default() })); - let mesh = Mesh3d(meshes.add(Plane3d::new(Vec3::Y, Vec2::splat(1.0)))); + let mesh = Mesh2d(meshes.add(Rectangle::new(1.0, 1.0))); let name = Name::new("bird"); - let t = Transform::from_xyz(0.0, 0.0, 0.0).with_rotation(Quat::from_rotation_x(PI / 2.0)); + let t = Transform::from_xyz(0.0, 0.0, 0.0).with_scale(Vec3::splat(100.0)); let physics = ( RigidBody::Static, - Collider::capsule(1.0, 1.0), - Mass(1.0), + Collider::circle(0.5), + Mass(10.0), ExternalImpulse::default().with_persistence(false), - LockedAxes::ROTATION_LOCKED.lock_translation_z(), ); let tape = Tape::default(); @@ -139,30 +136,30 @@ struct Pipe; fn init_obstacles( mut commands: Commands, mut meshes: ResMut>, - mut materials: ResMut>, + mut materials: ResMut>, ) { let ground = { - let material = MeshMaterial3d(materials.add(StandardMaterial { - base_color: LIGHT_GREEN.into(), + let material = MeshMaterial2d(materials.add(ColorMaterial { + color: LIGHT_GREEN.into(), ..default() })); - let mesh = Mesh3d(meshes.add(Cuboid::new(10.0, 1.0, 1.0))); + let mesh = Mesh2d(meshes.add(Rectangle::new(10.0, 1.0))); let name = Name::new("ground"); - let physics = (RigidBody::Static, Collider::cuboid(1.0, 1.0, 1.0)); + let physics = (RigidBody::Static, Collider::rectangle(1.0, 1.0)); (name, mesh, material, physics, Ground) }; let pipe = { - let material = MeshMaterial3d(materials.add(StandardMaterial { - base_color: GREEN.into(), + let material = MeshMaterial2d(materials.add(ColorMaterial { + color: GREEN.into(), ..default() })); - let mesh = Mesh3d(meshes.add(Cuboid::new(1.0, 3.0, 1.0))); - let physics = (RigidBody::Static, Collider::cuboid(1.0, 3.0, 1.0)); + let mesh = Mesh2d(meshes.add(Rectangle::new(1.0, 2.0))); + let physics = (RigidBody::Static, Collider::rectangle(1.0, 2.0)); let name = Name::new("pipe"); ( @@ -178,14 +175,15 @@ fn init_obstacles( // screens and then "move" them around. // This is considerably more complexity so can be implemented later, but would keep memory // overhead fairly static. - (1..99).for_each(|i| { + (1..10).for_each(|i| { // TODO: Jitter up/down/close/far of pipes for challenge - let above = Transform::from_xyz(5.0 * i as f32, 4.0, 0.0); - let below = Transform::from_xyz(5.0 * i as f32, -4.0, 0.0); - let floor = Transform::from_xyz(1.0 * i as f32, -4.0, 0.0); - + let above = Transform::from_xyz(300.0 * i as f32, 300.0, -1.0).with_scale(Vec3::splat(100.0)); commands.spawn((pipe.clone(), above)); + + let below = Transform::from_xyz(300.0 * i as f32, -200.0, -1.0).with_scale(Vec3::splat(100.0)); commands.spawn((pipe.clone(), below)); + + let floor = Transform::from_xyz(300.0 * i as f32, -300.0, 1.0).with_scale(Vec3::splat(100.0)); commands.spawn((ground.clone(), floor)); }); } @@ -271,8 +269,8 @@ fn flap( "Only flap when space is just pressed" ); - bird.iter_mut().for_each(|(t, mut f)| { - f.apply_impulse(t.rotation * Vec3::NEG_Z * 5.0 + t.rotation * Vec3::X); + bird.iter_mut().for_each(|(_, mut f)| { + f.apply_impulse(Vec2::Y * 5000.0 + Vec2::X * 1000.0); }); } @@ -324,6 +322,7 @@ fn rewind( if let Some(r) = tape.rotations.pop() { transform.rotation = r; } + // TODO: Only need to set {angular|linear}_velocity at end of Rewind if let Some(av) = tape.angular_velocities.pop() { *angular_velocity = av; } diff --git a/src/physics2d.rs b/src/physics2d.rs index 1fdde13..e801c83 100644 --- a/src/physics2d.rs +++ b/src/physics2d.rs @@ -6,7 +6,7 @@ pub struct Physics2dPlugin; impl Plugin for Physics2dPlugin { fn build(&self, app: &mut App) { - app.add_plugins(PhysicsDebugPlugin::default()).add_systems( + app.add_plugins((PhysicsPlugins::default(), PhysicsDebugPlugin::default())).add_systems( Update, toggle_physics_debug_render.run_if(state_changed::), );