diff --git a/src/config.rs b/src/config.rs index a318092..fd585af 100644 --- a/src/config.rs +++ b/src/config.rs @@ -11,6 +11,8 @@ pub(crate) const BACKGROUND_COLOR: Color = Color::srgb(0.3, 0.3, 0.3); pub(crate) const PLAYER_SHIP_COLOR: Color = Color::srgb(1.0, 1.0, 1.0); pub(crate) const SHIP_THRUSTER_COLOR_ACTIVE: Color = Color::srgb(1.0, 0.2, 0.2); pub(crate) const SHIP_THRUSTER_COLOR_INACTIVE: Color = Color::srgb(0.5, 0.5, 0.5); +pub(crate) const ASTEROID_SMALL_COLOR: Color = Color::srgb(1.0, 0., 0.); +// TODO: asteroid medium & large pub(crate) const SHIP_THRUST: f32 = 1.0; pub(crate) const SHIP_ROTATION: f32 = 0.1; // +/- rotation speed in... radians per frame diff --git a/src/lib.rs b/src/lib.rs index f980211..10251c4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,7 +10,7 @@ use bevy::{color::palettes::css::GRAY, prelude::*}; use bevy_inspector_egui::prelude::ReflectInspectorOptions; use bevy_inspector_egui::InspectorOptions; -use config::{SHIP_THRUSTER_COLOR_ACTIVE, SHIP_THRUSTER_COLOR_INACTIVE}; +use config::{ASTEROID_SMALL_COLOR, SHIP_THRUSTER_COLOR_ACTIVE, SHIP_THRUSTER_COLOR_INACTIVE}; pub struct AsteroidPlugin; @@ -31,6 +31,7 @@ impl Plugin for AsteroidPlugin { .insert_resource(AsteroidSpawner { timer: Timer::new(Duration::from_secs(3), TimerMode::Repeating), }) + .init_resource::() .add_systems(Startup, spawn_camera) .add_systems(OnEnter(GameState::Playing), (spawn_player, spawn_ui)) .add_systems( @@ -126,6 +127,73 @@ struct WorldSize { height: f32, } +#[derive(Resource)] +struct GameAssets { + meshes: [Handle; 4], + materials: [Handle; 6], +} + +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()) + } +} + +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)), + ]; + 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), + ]; + GameAssets { meshes, materials } + } +} + fn spawn_camera(mut commands: Commands) { commands.spawn(Camera2d); }