diff --git a/Cargo.toml b/Cargo.toml index 2922330..0625f2a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "asteroids" -version = "0.1.0" +version = "0.2.0" edition = "2021" [dependencies] diff --git a/src/lib.rs b/src/lib.rs index e32387b..85d96f4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,20 +9,25 @@ pub struct AsteroidPlugin; impl Plugin for AsteroidPlugin { fn build(&self, app: &mut App) { - app.add_systems(Startup, (spawn_camera, spawn_player)) - .insert_resource(ClearColor(BACKGROUND_COLOR)) - .insert_resource(WorldSize { - width: WINDOW_SIZE.x, - height: WINDOW_SIZE.y, - }) - .add_systems( - FixedUpdate, - (input_ship_thruster, input_ship_rotation, wrap_entities), - ) - .add_systems( - FixedPostUpdate, - (integrate_velocity, update_positions, apply_rotation_to_mesh), - ); + app.add_systems( + Startup, + (spawn_camera, spawn_player, spawn_ui), + ) + .insert_resource(ClearColor(BACKGROUND_COLOR)) + .insert_resource(WorldSize { + width: WINDOW_SIZE.x, + height: WINDOW_SIZE.y, + }) + .insert_resource(Lives(3)) + .insert_resource(Score(0)) + .add_systems( + FixedUpdate, + (input_ship_thruster, input_ship_rotation, wrap_entities), + ) + .add_systems( + FixedPostUpdate, + (integrate_velocity, update_positions, apply_rotation_to_mesh), + ); } } @@ -44,6 +49,24 @@ struct Ship; #[derive(Component)] struct ThrusterColors(Handle, Handle); +#[derive(Resource, Debug, Deref, Clone, Copy)] +struct Score(i32); + +impl From for String { + fn from(value: Score) -> Self { + value.to_string() + } +} + +#[derive(Resource, Debug, Deref, Clone, Copy)] +struct Lives(i32); + +impl From for String { + fn from(value: Lives) -> Self { + value.to_string() + } +} + #[derive(Resource)] struct WorldSize { width: f32, @@ -187,3 +210,36 @@ fn wrap_entities(mut query: Query<&mut Position>, world_size: Res) { } } } + +fn spawn_ui(mut commands: Commands, score: Res, lives: Res) { + commands.spawn(TextBundle::from_sections([ + TextSection::new( + "Score: ", + TextStyle { + font_size: 25.0, + ..default() + }, + ), + TextSection::new( + *score, + TextStyle { + font_size: 25.0, + ..default() + }, + ), + TextSection::new( + " | Lives: ", + TextStyle { + font_size: 25.0, + ..default() + }, + ), + TextSection::new( + *lives, + TextStyle { + font_size: 25.0, + ..default() + }, + ) + ])); +}