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:
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "asteroids"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
|
||||
58
src/lib.rs
58
src/lib.rs
@@ -9,12 +9,17 @@ pub struct AsteroidPlugin;
|
||||
|
||||
impl Plugin for AsteroidPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Startup, (spawn_camera, spawn_player))
|
||||
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),
|
||||
@@ -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()
|
||||
},
|
||||
)
|
||||
]));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user