Spawning score and lives UI elements, no logic

I have basic UI elements! They can't be updated, yet, and there's still
no game logic to allow the player to affect it on their own.
This commit is contained in:
2024-11-28 11:56:42 -06:00
parent a61d400c64
commit 37d7c1db42
2 changed files with 71 additions and 15 deletions

View File

@@ -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<ColorMaterial>, Handle<ColorMaterial>);
#[derive(Resource, Debug, Deref, Clone, Copy)]
struct Score(i32);
impl From<Score> for String {
fn from(value: Score) -> Self {
value.to_string()
}
}
#[derive(Resource, Debug, Deref, Clone, Copy)]
struct Lives(i32);
impl From<Lives> 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<WorldSize>) {
}
}
}
fn spawn_ui(mut commands: Commands, score: Res<Score>, lives: Res<Lives>) {
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()
},
)
]));
}