Bound player

This commit is contained in:
2026-09-17 19:53:09 +02:00
parent 08aca8c4da
commit f2d8e8b8ef
2 changed files with 28 additions and 0 deletions
+25
View File
@@ -0,0 +1,25 @@
use bevy::prelude::*;
use crate::{collision::Collider, player::Player};
fn constrain_player(
mut player: Single<(&Collider, &mut Transform), With<Player>>,
camera: Single<&Camera, With<Camera2d>>,
) {
let viewport = camera
.logical_viewport_size()
.unwrap_or(Vec2::new(1920.0, 1080.0));
let half_width = player.0.size.x / 2.0;
let border = viewport.x / 2.0 - half_width;
// Restrict player position
player.1.translation.x = player.1.translation.x.clamp(-border, border);
}
pub struct BoundsPlugin;
impl Plugin for BoundsPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, constrain_player);
}
}