Implement rotation on player ship
The rotation works, although it isn't visually indicated. I'll have to swap out the circle for a triangle or something.
This commit is contained in:
@@ -8,5 +8,5 @@ use bevy::color::Color;
|
|||||||
pub(crate) const BACKGROUND_COLOR: Color = Color::srgb(0.3, 0.3, 0.3);
|
pub(crate) const BACKGROUND_COLOR: Color = Color::srgb(0.3, 0.3, 0.3);
|
||||||
pub(crate) const PLAYER_SHIP_COLOR: Color = Color::srgb(1.0, 1.0, 1.0);
|
pub(crate) const PLAYER_SHIP_COLOR: Color = Color::srgb(1.0, 1.0, 1.0);
|
||||||
|
|
||||||
pub(crate) const SHIP_THRUST_LIMIT: f32 = 10.0;
|
pub(crate) const SHIP_THRUST: f32 = 1.0;
|
||||||
pub(crate) const SHIP_ROTATION_LIMIT: f32 = 5.0; // +/- rotation speed in... uunniittss
|
pub(crate) const SHIP_ROTATION: f32 = 0.1; // +/- rotation speed in... radians per frame
|
||||||
|
|||||||
26
src/lib.rs
26
src/lib.rs
@@ -1,6 +1,6 @@
|
|||||||
mod config;
|
mod config;
|
||||||
|
|
||||||
use crate::config::{BACKGROUND_COLOR, PLAYER_SHIP_COLOR};
|
use crate::config::{BACKGROUND_COLOR, PLAYER_SHIP_COLOR, SHIP_THRUST, SHIP_ROTATION};
|
||||||
|
|
||||||
use bevy::{prelude::*, sprite::MaterialMesh2dBundle};
|
use bevy::{prelude::*, sprite::MaterialMesh2dBundle};
|
||||||
|
|
||||||
@@ -10,7 +10,7 @@ impl Plugin for AsteroidPlugin {
|
|||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_systems(Startup, (spawn_camera, spawn_player))
|
app.add_systems(Startup, (spawn_camera, spawn_player))
|
||||||
.insert_resource(ClearColor(BACKGROUND_COLOR))
|
.insert_resource(ClearColor(BACKGROUND_COLOR))
|
||||||
.add_systems(FixedUpdate, input_ship_thruster)
|
.add_systems(FixedUpdate, (input_ship_thruster, input_ship_rotation))
|
||||||
// .add_systems(FixedUpdate, input_ship_rotation);
|
// .add_systems(FixedUpdate, input_ship_rotation);
|
||||||
.add_systems(FixedPostUpdate, (integrate_velocity, update_positions));
|
.add_systems(FixedPostUpdate, (integrate_velocity, update_positions));
|
||||||
}
|
}
|
||||||
@@ -23,7 +23,7 @@ struct Position(bevy::math::Vec2);
|
|||||||
struct Velocity(bevy::math::Vec2);
|
struct Velocity(bevy::math::Vec2);
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
struct Rotation(bevy::prelude::Quat);
|
struct Rotation(f32);
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
struct Ship;
|
struct Ship;
|
||||||
@@ -41,6 +41,7 @@ fn spawn_player(
|
|||||||
Ship,
|
Ship,
|
||||||
Position(Vec2::default()),
|
Position(Vec2::default()),
|
||||||
Velocity(Vec2::ZERO),
|
Velocity(Vec2::ZERO),
|
||||||
|
Rotation(0.0),
|
||||||
MaterialMesh2dBundle {
|
MaterialMesh2dBundle {
|
||||||
mesh: meshes.add(Circle::new(10.0)).into(),
|
mesh: meshes.add(Circle::new(10.0)).into(),
|
||||||
material: materials.add(PLAYER_SHIP_COLOR),
|
material: materials.add(PLAYER_SHIP_COLOR),
|
||||||
@@ -55,15 +56,15 @@ fn spawn_player(
|
|||||||
*/
|
*/
|
||||||
fn input_ship_thruster(
|
fn input_ship_thruster(
|
||||||
keyboard_input: Res<ButtonInput<KeyCode>>,
|
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||||
mut query: Query<&mut Velocity, With<Ship>>,
|
mut query: Query<(&mut Velocity, &Rotation), With<Ship>>,
|
||||||
) {
|
) {
|
||||||
let Ok(mut velocity) = query.get_single_mut() else {
|
let Ok((mut velocity, rotation)) = query.get_single_mut() else {
|
||||||
let count = query.iter().count();
|
let count = query.iter().count();
|
||||||
panic!("There should be exactly one player ship! Instead, there seem to be {count}.");
|
panic!("There should be exactly one player ship! Instead, there seems to be {count}.");
|
||||||
};
|
};
|
||||||
|
|
||||||
if keyboard_input.pressed(KeyCode::KeyW) {
|
if keyboard_input.pressed(KeyCode::KeyW) {
|
||||||
velocity.0 += Vec2::new(10.0, 0.0);
|
velocity.0 += Vec2::from_angle(rotation.0) * SHIP_THRUST;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,7 +75,16 @@ fn input_ship_rotation(
|
|||||||
keyboard_input: Res<ButtonInput<KeyCode>>,
|
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||||
mut query: Query<&mut Rotation, With<Ship>>,
|
mut query: Query<&mut Rotation, With<Ship>>,
|
||||||
) {
|
) {
|
||||||
todo!();
|
let Ok(mut rotation) = query.get_single_mut() else {
|
||||||
|
let count = query.iter().count();
|
||||||
|
panic!("There should be exactly one player ship! Instead, there seems to be {count}.");
|
||||||
|
};
|
||||||
|
|
||||||
|
if keyboard_input.pressed(KeyCode::KeyA) {
|
||||||
|
rotation.0 += SHIP_ROTATION;
|
||||||
|
} else if keyboard_input.pressed(KeyCode::KeyD) {
|
||||||
|
rotation.0 -= SHIP_ROTATION;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
Reference in New Issue
Block a user