initial commit: bevy setup, background, score counter

This commit is contained in:
YK committed 2024-07-13 21:08:45 +03:00
commit 138476111c
4 files changed
+4430

No files matched your search

+82
View File
@@ -0,0 +1,82 @@
use bevy::prelude::*;
// This resource tracks the game's score
#[derive(Resource, Deref, DerefMut)]
struct Score (usize);
#[derive(Component)]
struct ScoreboardUi;
const BG_COLOR: Color = Color::srgb(0.949, 0.937, 0.769);
const SCOREBOARD_FONT_SIZE: f32 = 44.0;
const TEXT_COLOR: Color = Color::BLACK;
const SCORE_COLOR: Color = Color::srgb(0.4, 0.05, 0.04);
const SCOREBOARD_TEXT_PADDING: Val = Val::Px(22.0);
pub struct SqPlugin;
impl Plugin for SqPlugin {
fn build (&self, app: &mut App) {
// add things to your app here
app
.insert_resource(Score(0))
.insert_resource(ClearColor(BG_COLOR))
;
}
}
fn setup (
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
asset_server: Res<AssetServer>,
) {
commands.spawn(Camera2dBundle::default());
commands.spawn((
ScoreboardUi,
TextBundle::from_sections([
TextSection::new(
"Score: ",
TextStyle {
font_size: SCOREBOARD_FONT_SIZE,
color: TEXT_COLOR,
..default()
},
),
TextSection::from_style(TextStyle {
font_size: SCOREBOARD_FONT_SIZE,
color: SCORE_COLOR,
..default()
}),
])
.with_style(Style {
position_type: PositionType::Absolute,
top: SCOREBOARD_TEXT_PADDING,
left: SCOREBOARD_TEXT_PADDING,
..default()
}),
));
}
fn update_scoreboard (score: Res<Score>, mut query: Query<&mut Text, With<ScoreboardUi>>) {
let mut text = query.single_mut();
text.sections[1].value = score.to_string();
}
fn main () {
App::new()
.add_plugins((DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: String::from("4096GAME"),
..Default::default()
}),
..Default::default()
}), SqPlugin))
.add_systems(Startup, setup)
.add_systems(Update, update_scoreboard)
.run();
}