Remove Rotation component, update usage sites
This commit is contained in:
@@ -7,7 +7,7 @@ use std::time::Duration;
|
|||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
use crate::{GameAssets, Rotation, WorldSize, physics::Velocity};
|
use crate::{GameAssets, WorldSize, physics::Velocity};
|
||||||
|
|
||||||
#[derive(Component, Deref, DerefMut)]
|
#[derive(Component, Deref, DerefMut)]
|
||||||
pub struct Asteroid(AsteroidSize);
|
pub struct Asteroid(AsteroidSize);
|
||||||
@@ -122,7 +122,6 @@ pub fn spawn_asteroid(
|
|||||||
Sensor,
|
Sensor,
|
||||||
Transform::from_translation(spawn.pos.extend(0.0)),
|
Transform::from_translation(spawn.pos.extend(0.0)),
|
||||||
Velocity(spawn.vel),
|
Velocity(spawn.vel),
|
||||||
Rotation(0.0),
|
|
||||||
Mesh2d(mesh),
|
Mesh2d(mesh),
|
||||||
MeshMaterial2d(material),
|
MeshMaterial2d(material),
|
||||||
));
|
));
|
||||||
|
|||||||
19
src/lib.rs
19
src/lib.rs
@@ -11,7 +11,7 @@ use crate::config::{
|
|||||||
ASTEROID_SMALL_COLOR, BACKGROUND_COLOR, PLAYER_SHIP_COLOR, SHIP_ROTATION, SHIP_THRUST,
|
ASTEROID_SMALL_COLOR, BACKGROUND_COLOR, PLAYER_SHIP_COLOR, SHIP_ROTATION, SHIP_THRUST,
|
||||||
SHIP_THRUSTER_COLOR_ACTIVE, SHIP_THRUSTER_COLOR_INACTIVE, WINDOW_SIZE,
|
SHIP_THRUSTER_COLOR_ACTIVE, SHIP_THRUSTER_COLOR_INACTIVE, WINDOW_SIZE,
|
||||||
};
|
};
|
||||||
use crate::physics::Rotation;
|
use crate::physics::AngularVelocity;
|
||||||
use crate::ship::Ship;
|
use crate::ship::Ship;
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
@@ -70,7 +70,7 @@ impl Plugin for AsteroidPlugin {
|
|||||||
.add_event::<asteroids::SpawnAsteroid>()
|
.add_event::<asteroids::SpawnAsteroid>()
|
||||||
.add_event::<event::AsteroidDestroy>()
|
.add_event::<event::AsteroidDestroy>()
|
||||||
.add_event::<event::ShipDestroy>();
|
.add_event::<event::ShipDestroy>();
|
||||||
app.insert_state(GameState::TitleScreen);
|
app.insert_state(GameState::Playing);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,24 +237,25 @@ fn spawn_camera(mut commands: Commands) {
|
|||||||
*/
|
*/
|
||||||
fn input_ship_thruster(
|
fn input_ship_thruster(
|
||||||
keyboard_input: Res<ButtonInput<KeyCode>>,
|
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||||
mut query: Query<(&mut physics::Velocity, &Rotation, &mut Children), With<Ship>>,
|
mut query: Query<(&mut physics::Velocity, &Transform, &mut Children), With<Ship>>,
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
game_assets: Res<GameAssets>,
|
game_assets: Res<GameAssets>,
|
||||||
) {
|
) {
|
||||||
// TODO: Maybe change for a Single<Ship>> so this only runs for the one ship
|
// TODO: Maybe change for a Single<Ship>> so this only runs for the one ship
|
||||||
// buuut... that would silently do nothing if there are 0 or >1 ships, and
|
// buuut... that would silently do nothing if there are 0 or >1 ships, and
|
||||||
// I might want to crash on purpose in that case.
|
// I might want to crash on purpose in that case.
|
||||||
let Ok((mut velocity, rotation, children)) = query.single_mut() else {
|
let Ok((mut velocity, transform, children)) = query.single_mut() else {
|
||||||
let count = query.iter().count();
|
let count = query.iter().count();
|
||||||
panic!("There should be exactly one player ship! Instead, there seems to be {count}.");
|
panic!("There should be exactly one player ship! Instead, there seems to be {count}.");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let rotation = transform.rotation;
|
||||||
let thrusters = children
|
let thrusters = children
|
||||||
.first()
|
.first()
|
||||||
.expect("Couldn't find first child, which should be the thruster");
|
.expect("Couldn't find first child, which should be the thruster");
|
||||||
|
|
||||||
if keyboard_input.pressed(KeyCode::KeyW) {
|
if keyboard_input.pressed(KeyCode::KeyW) {
|
||||||
velocity.0 += Vec2::from_angle(rotation.0) * SHIP_THRUST;
|
velocity.0 += Vec2::from_angle(rotation.z) * SHIP_THRUST;
|
||||||
commands
|
commands
|
||||||
.entity(*thrusters)
|
.entity(*thrusters)
|
||||||
.insert(MeshMaterial2d(game_assets.thruster_mat_active()));
|
.insert(MeshMaterial2d(game_assets.thruster_mat_active()));
|
||||||
@@ -271,17 +272,17 @@ fn input_ship_thruster(
|
|||||||
*/
|
*/
|
||||||
fn input_ship_rotation(
|
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 AngularVelocity, With<Ship>>,
|
||||||
) {
|
) {
|
||||||
let Ok(mut rotation) = query.single_mut() else {
|
let Ok(mut angular_vel) = query.single_mut() else {
|
||||||
let count = query.iter().count();
|
let count = query.iter().count();
|
||||||
panic!("There should be exactly one player ship! Instead, there seems to be {count}.");
|
panic!("There should be exactly one player ship! Instead, there seems to be {count}.");
|
||||||
};
|
};
|
||||||
|
|
||||||
if keyboard_input.pressed(KeyCode::KeyA) {
|
if keyboard_input.pressed(KeyCode::KeyA) {
|
||||||
rotation.0 += SHIP_ROTATION;
|
angular_vel.0 += SHIP_ROTATION;
|
||||||
} else if keyboard_input.pressed(KeyCode::KeyD) {
|
} else if keyboard_input.pressed(KeyCode::KeyD) {
|
||||||
rotation.0 -= SHIP_ROTATION;
|
angular_vel.0 -= SHIP_ROTATION;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,9 +11,6 @@ pub(crate) struct Velocity(pub(crate) bevy::math::Vec2);
|
|||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub(crate) struct AngularVelocity(pub(crate) f32);
|
pub(crate) struct AngularVelocity(pub(crate) f32);
|
||||||
|
|
||||||
#[derive(Component)]
|
|
||||||
pub(crate) struct Rotation(pub(crate) f32);
|
|
||||||
|
|
||||||
/// Marker for any entity that should wrap on screen edges
|
/// Marker for any entity that should wrap on screen edges
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub(crate) struct Wrapping;
|
pub(crate) struct Wrapping;
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
GameAssets, Rotation,
|
physics::{Velocity, Wrapping}, AngularVelocity, GameAssets
|
||||||
physics::{Velocity, Wrapping},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
@@ -19,7 +18,7 @@ pub fn spawn_player(mut commands: Commands, game_assets: Res<GameAssets>) {
|
|||||||
Ship,
|
Ship,
|
||||||
Wrapping,
|
Wrapping,
|
||||||
Velocity(Vec2::ZERO),
|
Velocity(Vec2::ZERO),
|
||||||
Rotation(0.0),
|
AngularVelocity(0.0),
|
||||||
Mesh2d(game_assets.ship().0),
|
Mesh2d(game_assets.ship().0),
|
||||||
MeshMaterial2d(game_assets.ship().1),
|
MeshMaterial2d(game_assets.ship().1),
|
||||||
Transform::default().with_scale(Vec3::new(20.0, 20.0, 20.0)),
|
Transform::default().with_scale(Vec3::new(20.0, 20.0, 20.0)),
|
||||||
|
|||||||
Reference in New Issue
Block a user