diff --git a/src/dashboard.rs b/src/dashboard.rs index eeb9caa..7ad7d70 100644 --- a/src/dashboard.rs +++ b/src/dashboard.rs @@ -3,7 +3,7 @@ #![allow(clippy::type_complexity)] -use super::{Player, Velocity}; +use super::{physics, Player}; use bevy::prelude::*; #[derive(Component, Debug)] @@ -91,7 +91,7 @@ fn spawn_dashboard(mut commands: Commands, asset_server: Res) { } fn update_speedo( - player: Query<(&Velocity, With)>, + player: Query<(&physics::Velocity, With)>, mut speedo: Query<(&mut Transform, With)>, ) { let (vp, _) = player.single(); diff --git a/src/main.rs b/src/main.rs index c970717..ef3f61d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,26 +3,19 @@ #![allow(clippy::type_complexity)] -use bevy::{ - math::{vec2, vec3}, - prelude::*, - render::camera::ScalingMode, - window, -}; +use bevy::{math::vec3, prelude::*, render::camera::ScalingMode, window}; use bevy_ecs_tilemap::prelude as ecs_tilemap; use clap::Parser; -use slicetools::*; use std::f32::consts::PI; mod assets; mod dashboard; mod editor; mod mapping; +mod physics; mod tilemap; mod util; -use util::IteratorToArrayExt; - #[derive(Clone, Debug, Parser, Resource)] #[command(author, version, about, long_about = None)] struct Preferences { @@ -90,13 +83,13 @@ fn main() { ( handle_keyboard, handle_ai_players, - apply_velocity + physics::apply_velocity .after(handle_ai_players) .after(handle_keyboard), - apply_friction.after(apply_velocity), - track_player.after(apply_velocity), - collision_detection - .after(apply_velocity) + physics::apply_friction.after(physics::apply_velocity), + track_player.after(physics::apply_velocity), + physics::collision_detection + .after(physics::apply_velocity) .after(handle_keyboard) .after(handle_ai_players), ), @@ -116,27 +109,6 @@ struct Racer { #[derive(Component, Default, Debug)] struct Track; -#[derive(Component, Debug, Reflect)] -struct Velocity(Vec2); - -#[derive(Component, Clone, Debug, Reflect)] -struct Angle(f32); - -impl Angle { - fn normalize(&mut self) { - while self.0 > PI { - self.0 -= 2.0 * PI; - } - while self.0 < -PI { - self.0 += 2.0 * PI; - } - } - - fn to_quat(&self) -> Quat { - Quat::from_rotation_z(self.0 - PI / 2.0) - } -} - fn spawn_camera(mut commands: Commands) { let mut camera = Camera2dBundle::default(); // Request a constant width projection. 24 is the width in world units. @@ -171,8 +143,8 @@ fn spawn_player( commands.spawn(( Player, Racer::default(), - Angle(0.0), - Velocity(Vec2::new(0.0, 20.0)), + physics::Angle(0.0), + physics::Velocity(Vec2::new(0.0, 20.0)), SpriteSheetBundle { texture_atlas: texture_atlas.add(atlas), transform: Transform { @@ -196,8 +168,8 @@ fn spawn_ai_players( commands.spawn(( Racer::default(), - Angle(PI / 12.0), - Velocity(Vec2::new(0.0, 20.0)), + physics::Angle(PI / 12.0), + physics::Velocity(Vec2::new(0.0, 20.0)), SpriteSheetBundle { texture_atlas: texture_atlas.add(atlas), transform: Transform { @@ -214,8 +186,8 @@ fn spawn_ai_players( let atlas = TextureAtlas::from_grid(handle, Vec2::new(70., 121.), 1, 1, None, None); commands.spawn(( Racer::default(), - Angle(PI / 12.0), - Velocity(Vec2::new(0.0, 20.0)), + physics::Angle(PI / 12.0), + physics::Velocity(Vec2::new(0.0, 20.0)), SpriteSheetBundle { texture_atlas: texture_atlas.add(atlas), transform: Transform { @@ -232,8 +204,8 @@ fn spawn_ai_players( let atlas = TextureAtlas::from_grid(handle, Vec2::new(70., 121.), 1, 1, None, None); commands.spawn(( Racer::default(), - Angle(PI / 12.0), - Velocity(Vec2::new(0.0, 20.0)), + physics::Angle(PI / 12.0), + physics::Velocity(Vec2::new(0.0, 20.0)), SpriteSheetBundle { texture_atlas: texture_atlas.add(atlas), transform: Transform { @@ -246,160 +218,10 @@ fn spawn_ai_players( )); } -fn apply_friction( - mut query: Query<(&mut Velocity, &mut Transform)>, - time: Res