use bevy::prelude::*; use bevy::sprite::Anchor; use bevy_pixel_camera::{PixelCameraPlugin, PixelViewport, PixelZoom}; // GAME CONSTANTS ///////////////////////////////////////////////////////////// const WIDTH: f32 = 240.0; const HEIGHT: f32 = 240.0; const LEFT: f32 = -WIDTH / 2.0; const RIGHT: f32 = LEFT + WIDTH; const BOTTOM: f32 = -HEIGHT / 2.0; const _TOP: f32 = BOTTOM + HEIGHT; const CLOUD_WIDTH: f32 = 66.0; const CLOUD_HEIGHT: f32 = 20.0; const PILLAR_WIDTH: f32 = 21.0; const PILLAR_HEIGHT: f32 = 482.0; const PILLAR_SPACING: f32 = 80.0; const PILLAR_GAP: f32 = 70.0; const PILLAR_RANGE: f32 = 105.0; const BIRD_X: f32 = -80.0; const BIRD_DX: f32 = 15.0; const BIRD_DY: f32 = 11.0; const BIRD_RADIUS: f32 = 6.0; const FALLING_JERK: f32 = -2300.0; const FLAP_VELOCITY: f32 = 100.0; const FLAP_ACCELERATION: f32 = 90.0; // SETUP ////////////////////////////////////////////////////////////////////// #[derive(States, Default, Clone, Eq, PartialEq, Hash, Debug)] enum GameState { #[default] StartScreen, Playing, GameOver, } fn main() { App::new() .init_state::() .add_plugins( DefaultPlugins .set(ImagePlugin::default_nearest()) .set(WindowPlugin { primary_window: Some(Window { title: "Flappin'".to_string(), // resolution: bevy::window::WindowResolution::default() // .with_scale_factor_override(1.0), ..default() }), ..default() }), ) .add_plugins(PixelCameraPlugin) .insert_resource(Rng { mz: 0, mw: 0 }) .insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0))) .insert_resource(FlapTimer(Timer::from_seconds(0.5, TimerMode::Once))) .insert_resource(Action { just_pressed: false, }) .add_systems(Startup, setup) .add_systems(Startup, (spawn_bird, spawn_clouds).after(setup)) .add_systems(Update, bevy::window::close_on_esc) .add_systems(Update, on_press) .add_systems( Update, ( press_to_start, animate_flying_bird, animate_pillars, animate_clouds, ) .run_if(in_state(GameState::StartScreen)), ) .add_systems(OnEnter(GameState::Playing), spawn_pillars) .add_systems( Update, ( flap, animate_flappin_bird, collision_detection, animate_pillars, animate_clouds, ) .chain() .run_if(in_state(GameState::Playing)), ) .add_systems(OnEnter(GameState::GameOver), game_over) .add_systems(Update, press_to_start.run_if(in_state(GameState::GameOver))) .add_systems(OnExit(GameState::GameOver), despawn_pillars) .run(); } #[derive(Resource)] struct Textures { bird: Handle, bird_layout: Handle, pillars: Handle, clouds: Handle, clouds_layout: Handle, } fn setup( mut commands: Commands, time: Res