diff --git a/src/lib.rs b/src/lib.rs index f487306..2627bd8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ mod events; mod objects; mod physics; mod preparation_widget; +mod resources; mod title_screen; use crate::asteroids::AsteroidSpawner; @@ -16,13 +17,12 @@ use crate::objects::{Bullet, Ship}; use crate::physics::AngularVelocity; use bevy::prelude::*; -use bevy_inspector_egui::InspectorOptions; -use bevy_inspector_egui::prelude::ReflectInspectorOptions; use bevy_rapier2d::{ plugin::{NoUserData, RapierPhysicsPlugin}, prelude::*, render::RapierDebugRenderPlugin, }; +use resources::{GameAssets, Lives, Score, WorldSize}; pub struct AsteroidPlugin; @@ -98,107 +98,9 @@ pub enum GameState { GameOver, // Game has ended. Present game over dialogue and await user restart } -#[derive(Resource, Debug, Deref, Clone, Copy)] -struct Score(i32); - -impl From for String { - fn from(value: Score) -> Self { - value.to_string() - } -} - #[derive(Component)] struct Lifetime(Timer); -#[derive(InspectorOptions, Reflect, Resource, Debug, Deref, Clone, Copy)] -#[reflect(Resource, InspectorOptions)] -struct Lives(i32); - -impl From for String { - fn from(value: Lives) -> Self { - value.to_string() - } -} - -#[derive(Resource)] -struct WorldSize { - width: f32, - height: f32, -} - -#[derive(Resource)] -struct GameAssets { - meshes: [Handle; 5], - materials: [Handle; 7], -} - -impl GameAssets { - fn ship(&self) -> (Handle, Handle) { - (self.meshes[0].clone(), self.materials[0].clone()) - } - - // The thruster mesh is actually just the ship mesh - fn thruster_mesh(&self) -> Handle { - self.meshes[0].clone() - } - - // TODO: Look into parameterizing the material - // A shader uniform should be able to do this, but I don't know how to - // load those in Bevy. - fn thruster_mat_inactive(&self) -> Handle { - self.materials[1].clone() - } - - fn thruster_mat_active(&self) -> Handle { - self.materials[2].clone() - } - - fn asteroid_small(&self) -> (Handle, Handle) { - (self.meshes[1].clone(), self.materials[1].clone()) - } - - fn asteroid_medium(&self) -> (Handle, Handle) { - (self.meshes[2].clone(), self.materials[2].clone()) - } - - fn asteroid_large(&self) -> (Handle, Handle) { - (self.meshes[3].clone(), self.materials[3].clone()) - } - - fn bullet(&self) -> (Handle, Handle) { - (self.meshes[4].clone(), self.materials[6].clone()) - } -} - -impl FromWorld for GameAssets { - fn from_world(world: &mut World) -> Self { - let mut world_meshes = world.resource_mut::>(); - let meshes = [ - world_meshes.add(Triangle2d::new( - Vec2::new(0.5, 0.0), - Vec2::new(-0.5, 0.45), - Vec2::new(-0.5, -0.45), - )), - world_meshes.add(Circle::new(10.0)), - world_meshes.add(Circle::new(20.0)), - world_meshes.add(Circle::new(40.0)), - world_meshes.add(Circle::new(0.2)), - ]; - let mut world_materials = world.resource_mut::>(); - let materials = [ - world_materials.add(PLAYER_SHIP_COLOR), - world_materials.add(SHIP_THRUSTER_COLOR_INACTIVE), - world_materials.add(SHIP_THRUSTER_COLOR_ACTIVE), - world_materials.add(ASTEROID_SMALL_COLOR), - // TODO: asteroid medium and large colors - world_materials.add(ASTEROID_SMALL_COLOR), - world_materials.add(ASTEROID_SMALL_COLOR), - world_materials.add(BULLET_COLOR), - ]; - GameAssets { meshes, materials } - } -} - fn spawn_camera(mut commands: Commands) { commands.spawn(Camera2d); } diff --git a/src/resources.rs b/src/resources.rs new file mode 100644 index 0000000..dfff0d0 --- /dev/null +++ b/src/resources.rs @@ -0,0 +1,121 @@ +//! All the resources for the game + +use bevy::{ + asset::{Assets, Handle}, + ecs::{ + resource::Resource, + world::{FromWorld, World}, + }, + math::{ + Vec2, + primitives::{Circle, Triangle2d}, + }, + prelude::{Deref, Reflect, ReflectResource}, + render::mesh::Mesh, + sprite::ColorMaterial, +}; +use bevy_inspector_egui::InspectorOptions; +use bevy_inspector_egui::inspector_options::ReflectInspectorOptions; + +use crate::{ + ASTEROID_SMALL_COLOR, BULLET_COLOR, PLAYER_SHIP_COLOR, SHIP_THRUSTER_COLOR_ACTIVE, + SHIP_THRUSTER_COLOR_INACTIVE, +}; + +#[derive(Resource, Debug, Deref, Clone, Copy)] +pub struct Score(pub i32); + +impl From for String { + fn from(value: Score) -> Self { + value.to_string() + } +} + +#[derive(InspectorOptions, Reflect, Resource, Debug, Deref, Clone, Copy)] +#[reflect(Resource, InspectorOptions)] +pub struct Lives(pub i32); + +impl From for String { + fn from(value: Lives) -> Self { + value.to_string() + } +} + +#[derive(Resource)] +pub struct WorldSize { + pub width: f32, + pub height: f32, +} + +#[derive(Resource)] +pub struct GameAssets { + meshes: [Handle; 5], + materials: [Handle; 7], +} + +impl GameAssets { + pub fn ship(&self) -> (Handle, Handle) { + (self.meshes[0].clone(), self.materials[0].clone()) + } + + // The thruster mesh is actually just the ship mesh + pub fn thruster_mesh(&self) -> Handle { + self.meshes[0].clone() + } + + // TODO: Look into parameterizing the material + // A shader uniform should be able to do this, but I don't know how to + // load those in Bevy. + pub fn thruster_mat_inactive(&self) -> Handle { + self.materials[1].clone() + } + + pub fn thruster_mat_active(&self) -> Handle { + self.materials[2].clone() + } + + pub fn asteroid_small(&self) -> (Handle, Handle) { + (self.meshes[1].clone(), self.materials[1].clone()) + } + + pub fn asteroid_medium(&self) -> (Handle, Handle) { + (self.meshes[2].clone(), self.materials[2].clone()) + } + + pub fn asteroid_large(&self) -> (Handle, Handle) { + (self.meshes[3].clone(), self.materials[3].clone()) + } + + pub fn bullet(&self) -> (Handle, Handle) { + (self.meshes[4].clone(), self.materials[6].clone()) + } +} + +impl FromWorld for GameAssets { + fn from_world(world: &mut World) -> Self { + let mut world_meshes = world.resource_mut::>(); + let meshes = [ + world_meshes.add(Triangle2d::new( + Vec2::new(0.5, 0.0), + Vec2::new(-0.5, 0.45), + Vec2::new(-0.5, -0.45), + )), + world_meshes.add(Circle::new(10.0)), + world_meshes.add(Circle::new(20.0)), + world_meshes.add(Circle::new(40.0)), + world_meshes.add(Circle::new(0.2)), + ]; + let mut world_materials = world.resource_mut::>(); + let materials = [ + world_materials.add(PLAYER_SHIP_COLOR), + world_materials.add(SHIP_THRUSTER_COLOR_INACTIVE), + world_materials.add(SHIP_THRUSTER_COLOR_ACTIVE), + world_materials.add(ASTEROID_SMALL_COLOR), + // TODO: asteroid medium and large colors + world_materials.add(ASTEROID_SMALL_COLOR), + world_materials.add(ASTEROID_SMALL_COLOR), + world_materials.add(BULLET_COLOR), + ]; + GameAssets { meshes, materials } + } +}