diff --git a/src/asteroids.rs b/src/asteroids.rs index 148d6f0..f37d909 100644 --- a/src/asteroids.rs +++ b/src/asteroids.rs @@ -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); diff --git a/src/lib.rs b/src/lib.rs index 5f51fed..584af16 100644 --- a/src/lib.rs +++ b/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::() @@ -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>, - mut query: Query<(&mut Velocity, &Rotation, &mut Children), With>, + mut query: Query<(&mut physics::Velocity, &Rotation, &mut Children), With>, mut commands: Commands, game_assets: Res, ) { @@ -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