From a4e280904e470af98959a6daddf89ff142141b67 Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 4 Jun 2022 18:20:37 +0100 Subject: [PATCH 01/11] flappy bevy example birds sometimes fall out of the sky --- Cargo.toml | 4 + examples/games/flappy_bevy.rs | 278 ++++++++++++++++++++++++++++++++++ 2 files changed, 282 insertions(+) create mode 100644 examples/games/flappy_bevy.rs diff --git a/Cargo.toml b/Cargo.toml index 999157fea460d..4e6d7f17f6851 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -432,6 +432,10 @@ path = "examples/games/breakout.rs" name = "contributors" path = "examples/games/contributors.rs" +[[example]] +name = "flappy_bevy" +path = "examples/games/flappy_bevy.rs" + [[example]] name = "game_menu" path = "examples/games/game_menu.rs" diff --git a/examples/games/flappy_bevy.rs b/examples/games/flappy_bevy.rs new file mode 100644 index 0000000000000..b5b25625b86d9 --- /dev/null +++ b/examples/games/flappy_bevy.rs @@ -0,0 +1,278 @@ +//! A simplified Flappy Bird but with many birds + +use bevy::prelude::*; + +use bevy::sprite::collide_aabb::collide; +use bevy::window::PresentMode; + +use rand::Rng; + +const CHUNK_SIZE: f32 = 300.0; +const CAMERA_SPEED: f32 = 120.0; +const GAME_HEIGHT: f32 = 500.0; +const SCREEN_HEIGHT: f32 = 1500.0; +const CLEANUP_X_DIST: f32 = 1500.0; +const CHAOS_AMOUNT_X: f32 = 250.0; +const CHAOS_AMOUNT_Y: f32 = 600.0; +const DRIFT_TO_CENTER_AMOUNT: f32 = 0.01; +const FLAP_STRENGTH: f32 = 250.0; +const BIRD_SIZE: f32 = 24.0; +const BIRD_REPRODUCTION_CHANCE: f32 = 1.0; +const MAX_BIRDS: usize = 500; +const GRAVITY: f32 = 400.0; + +fn randf() -> f32 { + rand::thread_rng().gen::() +} + +pub fn main() { + let mut app = App::new(); + + app.insert_resource(WindowDescriptor { + width: 1600., + height: 900., + title: "Flappy Bevy".to_string(), + present_mode: PresentMode::Immediate, // smooth but power hungry + resizable: true, + ..Default::default() + }) + .add_plugins(DefaultPlugins) + .add_startup_system(spawn_camera) + .add_startup_system(load_art) + .add_startup_system(bird_startup) + .add_system(spawn_bird) + .add_system(bird_collision) + .add_system(bird_reproduction) + .add_system(bird_control.after(gravity)) + .add_system(terrain_gen) + .add_system(advance_camera) + .add_system(chaos) + .add_system(velocity) + .add_system(gravity) + .add_system(terrain_cleanup) + .add_system(drift_to_center) + .add_event::() + .add_event::(); + + app.run() +} + +pub struct Art { + bird_icon: Handle, +} + +fn load_art(mut commands: Commands, asset_server: Res) { + commands.insert_resource(Art { + bird_icon: asset_server.load("branding/icon.png"), + }); +} +struct GenerateChunk { + new_chunk_index: i32, +} + +fn advance_camera( + mut q: Query<&mut Transform, With>, + time: Res