diff --git a/src/lib.rs b/src/lib.rs index 2904196..01bf227 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ mod asteroids; pub mod config; mod preparation_widget; +mod ship; mod title_screen; use crate::config::{BACKGROUND_COLOR, PLAYER_SHIP_COLOR, SHIP_ROTATION, SHIP_THRUST, WINDOW_SIZE}; @@ -16,6 +17,7 @@ use bevy_rapier2d::{ render::RapierDebugRenderPlugin, }; use config::{ASTEROID_SMALL_COLOR, SHIP_THRUSTER_COLOR_ACTIVE, SHIP_THRUSTER_COLOR_INACTIVE}; +use ship::Ship; pub struct AsteroidPlugin; @@ -38,7 +40,7 @@ impl Plugin for AsteroidPlugin { .insert_resource(AsteroidSpawner::new()) .init_resource::() .add_systems(Startup, spawn_camera) - .add_systems(OnEnter(GameState::Playing), (spawn_player, spawn_ui)) + .add_systems(OnEnter(GameState::Playing), (ship::spawn_player, spawn_ui)) .add_systems( FixedUpdate, ( @@ -85,9 +87,6 @@ struct Velocity(bevy::math::Vec2); #[derive(Component)] struct Rotation(f32); -#[derive(Component)] -struct Ship; - /// Marker for any entity that should wrap on screen edges #[derive(Component)] struct Wrapping; @@ -188,31 +187,6 @@ fn spawn_camera(mut commands: Commands) { commands.spawn(Camera2d); } -fn spawn_player(mut commands: Commands, game_assets: Res) { - commands - .spawn(( - Collider::ball(0.7), - Sensor, - ActiveEvents::COLLISION_EVENTS, - ActiveCollisionTypes::STATIC_STATIC, - Ship, - Wrapping, - Position(Vec2::default()), - Velocity(Vec2::ZERO), - Rotation(0.0), - Mesh2d(game_assets.ship().0), - MeshMaterial2d(game_assets.ship().1), - Transform::default().with_scale(Vec3::new(20.0, 20.0, 20.0)), - )) - .with_child(( - Mesh2d(game_assets.thruster_mesh()), - MeshMaterial2d(game_assets.thruster_mat_inactive()), - Transform::default() - .with_scale(Vec3::splat(0.5)) - .with_translation(Vec3::new(-0.5, 0.0, -0.1)), - )); -} - /* Checks if "W" is pressed and increases velocity accordingly. */ diff --git a/src/ship.rs b/src/ship.rs new file mode 100644 index 0000000..09131bb --- /dev/null +++ b/src/ship.rs @@ -0,0 +1,32 @@ +use bevy::prelude::*; +use bevy_rapier2d::prelude::*; + +use crate::{GameAssets, Position, Rotation, Velocity, Wrapping}; + +#[derive(Component)] +pub struct Ship; + +pub fn spawn_player(mut commands: Commands, game_assets: Res) { + commands + .spawn(( + Collider::ball(0.7), + Sensor, + ActiveEvents::COLLISION_EVENTS, + ActiveCollisionTypes::STATIC_STATIC, + Ship, + Wrapping, + Position(Vec2::default()), + Velocity(Vec2::ZERO), + Rotation(0.0), + Mesh2d(game_assets.ship().0), + MeshMaterial2d(game_assets.ship().1), + Transform::default().with_scale(Vec3::new(20.0, 20.0, 20.0)), + )) + .with_child(( + Mesh2d(game_assets.thruster_mesh()), + MeshMaterial2d(game_assets.thruster_mat_inactive()), + Transform::default() + .with_scale(Vec3::splat(0.5)) + .with_translation(Vec3::new(-0.5, 0.0, -0.1)), + )); +}