From 696e00003057fbf021d405d7b7ef18cc27e91b7c Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Tue, 26 Nov 2024 14:49:23 -0600 Subject: [PATCH] Apply ship rotation to visual widget --- src/lib.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c9833d3..ba18120 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,8 +11,11 @@ impl Plugin for AsteroidPlugin { app.add_systems(Startup, (spawn_camera, spawn_player)) .insert_resource(ClearColor(BACKGROUND_COLOR)) .add_systems(FixedUpdate, (input_ship_thruster, input_ship_rotation)) - // .add_systems(FixedUpdate, input_ship_rotation); - .add_systems(FixedPostUpdate, (integrate_velocity, update_positions)); + .add_systems(FixedPostUpdate, ( + integrate_velocity, + update_positions, + apply_rotation_to_mesh, + )); } } @@ -43,7 +46,11 @@ fn spawn_player( Velocity(Vec2::ZERO), Rotation(0.0), MaterialMesh2dBundle { - mesh: meshes.add(Circle::new(10.0)).into(), + mesh: meshes.add(Triangle2d::new( + Vec2::new( 0.5, 0.0) * 20.0, + Vec2::new(-0.5, 0.25) * 20.0, + Vec2::new(-0.5, -0.25) * 20.0 + )).into(), material: materials.add(PLAYER_SHIP_COLOR), transform: Transform::default(), ..default() @@ -69,7 +76,8 @@ fn input_ship_thruster( } /* - Checks if "A" or "D" is pressed and rotates the ship accordingly + Checks if "A" or "D" is pressed and updates the player's Rotation component accordingly + Does *not* rotate the graphical widget! (that's done by the `apply_rotation_to_mesh` system) */ fn input_ship_rotation( keyboard_input: Res>, @@ -102,3 +110,12 @@ fn update_positions(mut query: Query<(&mut Transform, &Position)>) { transform.translation.y = position.0.y; } } + +/* + Assigns the rotation to the transform by copying it from the Rotation component. +*/ +fn apply_rotation_to_mesh(mut query: Query<(&mut Transform, &Rotation)>) { + for (mut transform, rotation) in &mut query { + transform.rotation = Quat::from_rotation_z(rotation.0); + } +}