use avian2d::{math::*, prelude::*}; use bevy::prelude::*; fn main() { App::new() .add_plugins(( DefaultPlugins, PhysicsPlugins::default().with_length_unit(20.0), )) .insert_resource(ClearColor(Color::srgb(0.05, 0.05, 0.1))) .insert_resource(Gravity(Vector::NEG_Y * 1000.0)) .add_systems(Startup, setup) .add_systems(Update, movement) .run(); } #[derive(Component)] struct Controllable; fn setup(mut commands: Commands, asset_server: Res) { commands.spawn(Camera2d); let square_sprite = Sprite { color: Color::srgb(0.7, 0.7, 0.8), custom_size: Some(Vec2::splat(50.0)), ..default() }; // Ceiling commands.spawn(( square_sprite.clone(), Transform::from_xyz(0.0, 50.0 * 6.0, 0.0).with_scale(Vec3::new(20.0, 1.0, 1.0)), RigidBody::Static, Collider::rectangle(50.0, 50.0), )); // Floor commands.spawn(( square_sprite.clone(), Transform::from_xyz(0.0, -50.0 * 6.0, 0.0).with_scale(Vec3::new(20.0, 1.0, 1.0)), RigidBody::Static, Collider::rectangle(50.0, 50.0), )); // Left wall commands.spawn(( square_sprite.clone(), Transform::from_xyz(-50.0 * 9.5, 0.0, 0.0).with_scale(Vec3::new(1.0, 11.0, 1.0)), RigidBody::Static, Collider::rectangle(50.0, 50.0), )); // Right wall commands.spawn(( square_sprite, Transform::from_xyz(50.0 * 9.5, 0.0, 0.0).with_scale(Vec3::new(1.0, 11.0, 1.0)), RigidBody::Static, Collider::rectangle(50.0, 50.0), )); let sprite_handle = asset_server.load("branding/icon.png"); const SPRITE_SIZE: f32 = 100.0; const CELL_SIZE: f32 = 25.0; const ROWS: i32 = 2; const COLS: i32 = 2; for x in 0..COLS { for y in 0..ROWS { commands.spawn(( Sprite { image: sprite_handle.clone(), custom_size: Some(Vec2::splat(SPRITE_SIZE)), ..default() }, Transform::from_xyz(x as f32 * CELL_SIZE, y as f32 * CELL_SIZE, 0.0), Collider::circle(SPRITE_SIZE / 2.0), RigidBody::Dynamic, Friction::new(0.2), Controllable, )); } } } fn movement( time: Res