Mostly 2d converted, little jank left to fix up but thats just polish

main
Elijah Voigt 3 months ago
parent ef190538ff
commit 87c502b5e8

@ -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<AssetServer>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
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));
}

@ -59,6 +59,7 @@ pub fn toggle_state_visibility<S: States + Component>(
pub fn create_camera_3d(mut commands: Commands) { pub fn create_camera_3d(mut commands: Commands) {
// 3d camera // 3d camera
info!("Spawning 3d camera");
commands.spawn(( commands.spawn((
Camera3d { ..default() }, Camera3d { ..default() },
Camera { ..default() }, Camera { ..default() },
@ -68,5 +69,6 @@ pub fn create_camera_3d(mut commands: Commands) {
pub fn create_camera_2d(mut commands: Commands) { pub fn create_camera_2d(mut commands: Commands) {
// 2d camera // 2d camera
info!("Spawning 2d camera");
commands.spawn((Camera2d, Camera { ..default() })); commands.spawn((Camera2d, Camera { ..default() }));
} }

@ -1,7 +1,7 @@
// Bevy basically forces "complex types" with Querys // Bevy basically forces "complex types" with Querys
#![allow(clippy::type_complexity)] #![allow(clippy::type_complexity)]
use games::physics3d::*; use games::physics2d::*;
use games::*; use games::*;
fn main() { fn main() {
@ -9,10 +9,11 @@ fn main() {
.add_plugins(( .add_plugins((
BaseGamePlugin { BaseGamePlugin {
name: "flappy bird (with rewind)".into(), 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::<PlayerState>() .init_state::<PlayerState>()
.add_systems( .add_systems(
Startup, Startup,
@ -20,7 +21,7 @@ fn main() {
init_bird, init_bird,
init_obstacles, init_obstacles,
init_ui, init_ui,
tweak_camera.after(create_camera_3d), tweak_camera.after(create_camera_2d),
), ),
) )
.add_systems(OnEnter(PlayerState::Alive), alive_bird) .add_systems(OnEnter(PlayerState::Alive), alive_bird)
@ -68,13 +69,10 @@ fn main() {
.run(); .run();
} }
fn tweak_camera(mut camera: Query<(&mut Camera, &mut AmbientLight, &mut Transform), With<Camera>>) { fn tweak_camera(mut camera: Single<&mut Camera>) {
camera.iter_mut().for_each(|(mut c, mut al, mut t)| { info!("Tweaking camera");
let mut c = camera.into_inner();
c.clear_color = ClearColorConfig::Custom(WHITE.into()); 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;
});
} }
#[derive(Component)] #[derive(Component)]
@ -102,27 +100,26 @@ fn init_bird(
mut commands: Commands, mut commands: Commands,
server: Res<AssetServer>, server: Res<AssetServer>,
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<ColorMaterial>>,
) { ) {
let material = MeshMaterial3d(materials.add(StandardMaterial { let material = MeshMaterial2d(materials.add(ColorMaterial {
base_color_texture: Some(server.load("flappy/bevy.png")), texture: Some(server.load("flappy/bevy.png")),
base_color: WHITE.into(), color: WHITE.into(),
alpha_mode: AlphaMode::Blend, alpha_mode: AlphaMode2d::Blend,
..default() ..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 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 = ( let physics = (
RigidBody::Static, RigidBody::Static,
Collider::capsule(1.0, 1.0), Collider::circle(0.5),
Mass(1.0), Mass(10.0),
ExternalImpulse::default().with_persistence(false), ExternalImpulse::default().with_persistence(false),
LockedAxes::ROTATION_LOCKED.lock_translation_z(),
); );
let tape = Tape::default(); let tape = Tape::default();
@ -139,30 +136,30 @@ struct Pipe;
fn init_obstacles( fn init_obstacles(
mut commands: Commands, mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<ColorMaterial>>,
) { ) {
let ground = { let ground = {
let material = MeshMaterial3d(materials.add(StandardMaterial { let material = MeshMaterial2d(materials.add(ColorMaterial {
base_color: LIGHT_GREEN.into(), color: LIGHT_GREEN.into(),
..default() ..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 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) (name, mesh, material, physics, Ground)
}; };
let pipe = { let pipe = {
let material = MeshMaterial3d(materials.add(StandardMaterial { let material = MeshMaterial2d(materials.add(ColorMaterial {
base_color: GREEN.into(), color: GREEN.into(),
..default() ..default()
})); }));
let mesh = Mesh3d(meshes.add(Cuboid::new(1.0, 3.0, 1.0))); let mesh = Mesh2d(meshes.add(Rectangle::new(1.0, 2.0)));
let physics = (RigidBody::Static, Collider::cuboid(1.0, 3.0, 1.0)); let physics = (RigidBody::Static, Collider::rectangle(1.0, 2.0));
let name = Name::new("pipe"); let name = Name::new("pipe");
( (
@ -178,14 +175,15 @@ fn init_obstacles(
// screens and then "move" them around. // screens and then "move" them around.
// This is considerably more complexity so can be implemented later, but would keep memory // This is considerably more complexity so can be implemented later, but would keep memory
// overhead fairly static. // overhead fairly static.
(1..99).for_each(|i| { (1..10).for_each(|i| {
// TODO: Jitter up/down/close/far of pipes for challenge // 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 above = Transform::from_xyz(300.0 * i as f32, 300.0, -1.0).with_scale(Vec3::splat(100.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);
commands.spawn((pipe.clone(), above)); 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)); 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)); commands.spawn((ground.clone(), floor));
}); });
} }
@ -271,8 +269,8 @@ fn flap(
"Only flap when space is just pressed" "Only flap when space is just pressed"
); );
bird.iter_mut().for_each(|(t, mut f)| { bird.iter_mut().for_each(|(_, mut f)| {
f.apply_impulse(t.rotation * Vec3::NEG_Z * 5.0 + t.rotation * Vec3::X); f.apply_impulse(Vec2::Y * 5000.0 + Vec2::X * 1000.0);
}); });
} }
@ -324,6 +322,7 @@ fn rewind(
if let Some(r) = tape.rotations.pop() { if let Some(r) = tape.rotations.pop() {
transform.rotation = r; transform.rotation = r;
} }
// TODO: Only need to set {angular|linear}_velocity at end of Rewind
if let Some(av) = tape.angular_velocities.pop() { if let Some(av) = tape.angular_velocities.pop() {
*angular_velocity = av; *angular_velocity = av;
} }

@ -6,7 +6,7 @@ pub struct Physics2dPlugin;
impl Plugin for Physics2dPlugin { impl Plugin for Physics2dPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_plugins(PhysicsDebugPlugin::default()).add_systems( app.add_plugins((PhysicsPlugins::default(), PhysicsDebugPlugin::default())).add_systems(
Update, Update,
toggle_physics_debug_render.run_if(state_changed::<DebuggingState>), toggle_physics_debug_render.run_if(state_changed::<DebuggingState>),
); );

Loading…
Cancel
Save