From a61d400c64bcc5ade1cd93b6a174938e5b4b372f Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Thu, 28 Nov 2024 10:14:55 -0600 Subject: [PATCH] Add mob wrap-around system --- src/lib.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 16a4995..e32387b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,7 +15,10 @@ impl Plugin for AsteroidPlugin { width: WINDOW_SIZE.x, height: WINDOW_SIZE.y, }) - .add_systems(FixedUpdate, (input_ship_thruster, input_ship_rotation)) + .add_systems( + FixedUpdate, + (input_ship_thruster, input_ship_rotation, wrap_entities), + ) .add_systems( FixedPostUpdate, (integrate_velocity, update_positions, apply_rotation_to_mesh), @@ -163,3 +166,24 @@ fn apply_rotation_to_mesh(mut query: Query<(&mut Transform, &Rotation)>) { transform.rotation = Quat::from_rotation_z(rotation.0); } } + +fn wrap_entities(mut query: Query<&mut Position>, world_size: Res) { + let right = world_size.width / 2.0; + let left = -right; + let top = world_size.height / 2.0; + let bottom = -top; + + for mut pos in query.iter_mut() { + if pos.0.x > right { + pos.0.x = left; + } else if pos.0.x < left { + pos.0.x = right; + } + + if pos.0.y > top { + pos.0.y = bottom; + } else if pos.0.y < bottom { + pos.0.y = top; + } + } +}