Something went wrong. Try again.
Creative coding by sneakycrow
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105use 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<AssetServer>) { 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<Time>, keyboard_input: Res<ButtonInput<KeyCode>>, mut marbles: Query<&mut LinearVelocity, With<Controllable>>,) { let delta_time = time.delta_secs_f64().adjust_precision();
for mut linear_velocity in &mut marbles { if keyboard_input.any_pressed([KeyCode::KeyW, KeyCode::ArrowUp]) { linear_velocity.y += 2500.0 * delta_time; } if keyboard_input.any_pressed([KeyCode::KeyS, KeyCode::ArrowDown]) { linear_velocity.y -= 500.0 * delta_time; } if keyboard_input.any_pressed([KeyCode::KeyA, KeyCode::ArrowLeft]) { linear_velocity.x -= 500.0 * delta_time; } if keyboard_input.any_pressed([KeyCode::KeyD, KeyCode::ArrowRight]) { linear_velocity.x += 500.0 * delta_time; } }}