Split physics parts into a submodule
The physics sim bits (that aren't Rapier2d) are now in their own submodule. I've included the `Wrapping` marker component because it doesn't really have anywhere else to live, and it's kinda sorta related to the physics. It controls the motion of objects... that's physics. :p There are name collisions between Rapier2d's `Velocity` and my own, but that's a problem for later. I've used more specific type paths where necessary to maintain the previous behavior. The `physics::Position` component can go away completely. It's just an extra copy of some of the built-in `Transform` data. I'm pretty sure it only exists because I didn't realize I could rely on directly manipulating the transform when I started this project.
This commit is contained in:
@@ -6,7 +6,7 @@ use std::time::Duration;
|
||||
/// not... not the whole game.
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::{GameAssets, Position, Rotation, Velocity, WorldSize};
|
||||
use crate::{GameAssets, Rotation, WorldSize, physics::Position, physics::Velocity};
|
||||
|
||||
#[derive(Component, Deref, DerefMut)]
|
||||
pub struct Asteroid(AsteroidSize);
|
||||
|
||||
75
src/lib.rs
75
src/lib.rs
@@ -1,6 +1,7 @@
|
||||
mod asteroids;
|
||||
pub mod config;
|
||||
mod event;
|
||||
mod physics;
|
||||
mod preparation_widget;
|
||||
mod ship;
|
||||
mod title_screen;
|
||||
@@ -18,6 +19,7 @@ use bevy_rapier2d::{
|
||||
render::RapierDebugRenderPlugin,
|
||||
};
|
||||
use config::{ASTEROID_SMALL_COLOR, SHIP_THRUSTER_COLOR_ACTIVE, SHIP_THRUSTER_COLOR_INACTIVE};
|
||||
use physics::Rotation;
|
||||
use ship::Ship;
|
||||
|
||||
pub struct AsteroidPlugin;
|
||||
@@ -47,7 +49,7 @@ impl Plugin for AsteroidPlugin {
|
||||
(
|
||||
input_ship_thruster,
|
||||
input_ship_rotation,
|
||||
wrap_entities,
|
||||
physics::wrap_entities,
|
||||
asteroids::tick_asteroid_manager,
|
||||
asteroids::spawn_asteroid.after(asteroids::tick_asteroid_manager),
|
||||
collision_listener,
|
||||
@@ -58,7 +60,11 @@ impl Plugin for AsteroidPlugin {
|
||||
)
|
||||
.add_systems(
|
||||
FixedPostUpdate,
|
||||
(integrate_velocity, update_positions, apply_rotation_to_mesh)
|
||||
(
|
||||
physics::integrate_velocity,
|
||||
physics::update_positions,
|
||||
physics::apply_rotation_to_mesh,
|
||||
)
|
||||
.run_if(in_state(GameState::Playing)),
|
||||
)
|
||||
.add_event::<asteroids::SpawnAsteroid>()
|
||||
@@ -130,19 +136,6 @@ pub enum GameState {
|
||||
GameOver, // Game has ended. Present game over dialogue and await user restart
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
struct Position(bevy::math::Vec2);
|
||||
|
||||
#[derive(Component)]
|
||||
struct Velocity(bevy::math::Vec2);
|
||||
|
||||
#[derive(Component)]
|
||||
struct Rotation(f32);
|
||||
|
||||
/// Marker for any entity that should wrap on screen edges
|
||||
#[derive(Component)]
|
||||
struct Wrapping;
|
||||
|
||||
#[derive(Resource, Debug, Deref, Clone, Copy)]
|
||||
struct Score(i32);
|
||||
|
||||
@@ -244,7 +237,7 @@ fn spawn_camera(mut commands: Commands) {
|
||||
*/
|
||||
fn input_ship_thruster(
|
||||
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||
mut query: Query<(&mut Velocity, &Rotation, &mut Children), With<Ship>>,
|
||||
mut query: Query<(&mut physics::Velocity, &Rotation, &mut Children), With<Ship>>,
|
||||
mut commands: Commands,
|
||||
game_assets: Res<GameAssets>,
|
||||
) {
|
||||
@@ -292,56 +285,6 @@ fn input_ship_rotation(
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Combine movement integration steps into one function
|
||||
// They need to be ordered so the physics is deterministic. Bevy can enforce
|
||||
// order, but it makes more sense to cut out the extra machinery and have one
|
||||
// single function. Probably better for cache locality or whatever, too.
|
||||
/*
|
||||
Add velocity to position
|
||||
*/
|
||||
fn integrate_velocity(mut query: Query<(&mut Position, &Velocity)>, time: Res<Time>) {
|
||||
for (mut position, velocity) in &mut query {
|
||||
position.0 += velocity.0 * time.delta_secs();
|
||||
}
|
||||
}
|
||||
|
||||
fn update_positions(mut query: Query<(&mut Transform, &Position)>) {
|
||||
for (mut transform, position) in &mut query {
|
||||
transform.translation.x = position.0.x;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
fn wrap_entities(mut query: Query<&mut Position, With<Wrapping>>, world_size: Res<WorldSize>) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn spawn_ui(mut commands: Commands, score: Res<Score>, lives: Res<Lives>) {
|
||||
commands.spawn((
|
||||
Text::new(format!("Score: {score:?} | Lives: {lives:?}")),
|
||||
|
||||
72
src/physics.rs
Normal file
72
src/physics.rs
Normal file
@@ -0,0 +1,72 @@
|
||||
//! Custom physics items
|
||||
//! TODO: Refactor in terms of Rapier2D, *or* implement colliders and remove it.
|
||||
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::WorldSize;
|
||||
|
||||
#[derive(Component)]
|
||||
pub(crate) struct Position(pub(crate) bevy::math::Vec2);
|
||||
|
||||
#[derive(Component)]
|
||||
pub(crate) struct Velocity(pub(crate) bevy::math::Vec2);
|
||||
|
||||
#[derive(Component)]
|
||||
pub(crate) struct Rotation(pub(crate) f32);
|
||||
|
||||
/// Marker for any entity that should wrap on screen edges
|
||||
#[derive(Component)]
|
||||
pub(crate) struct Wrapping;
|
||||
|
||||
// TODO: Combine movement integration steps into one function
|
||||
// They need to be ordered so the physics is deterministic. Bevy can enforce
|
||||
// order, but it makes more sense to cut out the extra machinery and have one
|
||||
// single function. Probably better for cache locality or whatever, too.
|
||||
/*
|
||||
Add velocity to position
|
||||
*/
|
||||
pub(crate) fn integrate_velocity(mut query: Query<(&mut Position, &Velocity)>, time: Res<Time>) {
|
||||
for (mut position, velocity) in &mut query {
|
||||
position.0 += velocity.0 * time.delta_secs();
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn update_positions(mut query: Query<(&mut Transform, &Position)>) {
|
||||
for (mut transform, position) in &mut query {
|
||||
transform.translation.x = position.0.x;
|
||||
transform.translation.y = position.0.y;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Assigns the rotation to the transform by copying it from the Rotation component.
|
||||
*/
|
||||
pub(crate) 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);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn wrap_entities(
|
||||
mut query: Query<&mut Position, With<Wrapping>>,
|
||||
world_size: Res<WorldSize>,
|
||||
) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
use bevy::prelude::*;
|
||||
use bevy_rapier2d::prelude::*;
|
||||
|
||||
use crate::{GameAssets, Position, Rotation, Velocity, Wrapping};
|
||||
use crate::{GameAssets, Rotation, physics::Position, physics::Velocity, physics::Wrapping};
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Ship;
|
||||
|
||||
Reference in New Issue
Block a user