Created
June 18, 2025 19:12
-
-
Save naxmefy/0f7ca89bbf1c8d0b4380683013c37e1b to your computer and use it in GitHub Desktop.
moveable square with bevy engine (0.16)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use bevy::prelude::*; | |
fn main() { | |
let mut app = App::new(); | |
app.add_plugins(DefaultPlugins); | |
app.add_systems(Startup, (spawn_camera, spawn_square)); | |
app.add_systems(Update, move_square); | |
app.run(); | |
} | |
#[derive(Component)] | |
struct Square; | |
#[derive(Component)] | |
struct Velocity(Vec2); | |
fn spawn_camera(mut commands: Commands) { | |
commands.spawn(Camera2d); | |
} | |
fn spawn_square(mut commands: Commands) { | |
commands.spawn(( | |
Sprite { | |
color: Color::srgb(0.3, 0.7, 0.9), | |
custom_size: Some(Vec2::new(100.0, 100.0)), | |
..default() | |
}, | |
Transform::from_xyz(0.0, 0.0, 0.0), // Center | |
Square {}, | |
Velocity(Vec2::new(200.0, 0.0)), | |
)); | |
} | |
fn move_square( | |
mut transform: Single<&mut Transform, With<Square>>, | |
mut velocity: Single<&mut Velocity, With<Square>>, | |
sprite: Single<&Sprite, With<Square>>, | |
windows: Query<&Window>, | |
time: Res<Time>, | |
) { | |
let window = windows.single().expect("No Window? Really?"); | |
let size = sprite.custom_size.expect("No Single Square found"); | |
let half_width = window.width() / 2.0; | |
let square_half_size = size.x / 2.0; | |
// Move the square | |
transform.translation += velocity.0.extend(0.0) * time.delta_secs(); | |
// Check for collision with borders | |
if transform.translation.x + square_half_size >= half_width { | |
transform.translation.x = half_width - square_half_size; | |
velocity.0.x *= -1.0; | |
} else if transform.translation.x - square_half_size <= -half_width { | |
transform.translation.x = -half_width + square_half_size; | |
velocity.0.x *= -1.0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment