use bevy::core_pipeline::{
bloom::{BloomCompositeMode, BloomSettings},
tonemapping::Tonemapping,
};
use bevy::diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin};
use bevy::sprite::MaterialMesh2dBundle;
use bevy::text::scale_value;
use bevy::window::{CursorGrabMode, PresentMode, WindowLevel, WindowTheme};
use bevy::{prelude::*, time};
use std::f32::consts::PI;
fn main() {
App::new()
.insert_resource(ClearColor(Color::rgb(1.0, 0.0, 0.0)))
.add_plugins((
DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "I am a window!".into(),
resolution: (1024., 576.).into(),
present_mode: PresentMode::AutoVsync,
canvas: Some("#bevy-portal".to_string()),
// Tells wasm to resize the window according to the
//available canvas
fit_canvas_to_parent: true,
// Tells wasm not to override default event handling,
like F5, Ctrl+R etc.
prevent_default_event_handling: false,
window_theme: Some(WindowTheme::Dark),
enabled_buttons: bevy::window::EnabledButtons {
maximize: false,
..Default::default()
},
// This will spawn an invisible window
// The window will be made visible in the make_visible()
//system after 3 frames.
// This is useful when you want to avoid the white window
that shows up before the GPU is ready to render the app.
visible: true,
..default()
}),
..default()
}),
LogDiagnosticsPlugin::default(),
FrameTimeDiagnosticsPlugin,
))
.add_systems(Startup, setup)
//.add_systems(Update, move_coin)
.add_systems(Update, move_cube)
//.add_systems(Update, intersections)
.add_systems(Update, text_update_system)
.run();
}
#[derive(Component)]
struct FpsText;
#[derive(Component)]
struct CameraState {}
#[derive(Component)]
struct CoinState {}
#[derive(Component)]
struct CubeState {
bounds_lower: (f32, f32),
bounds_upper: (f32, f32),
}
#[derive(Component)]
struct Score {
score: u32,
}
fn setup(
mut commands: Commands,
mut meshes: ResMut>,
asset_server: Res,
mut materials: ResMut>,
mut standard_materials: ResMut>,
) {
commands.spawn(Score { score: 0 });
// transform: Transform::from_xyz(0.0, 10.0, 20.0).looking_at(Vec3::ZERO, Vec3::Y),
// Spawn a camera looking at the entities to show what's happening in this example.
commands.spawn((
Camera3dBundle {
camera: Camera {
hdr: true, // 1. HDR is required for bloom
..default()
},
// 2. Using a tonemapper that desaturates to white is recommended
tonemapping: Tonemapping::TonyMcMapface,
transform: Transform::from_xyz(0.0, 10.0, 20.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
},
BloomSettings::default(), // 3. Enable bloom for the camera
));
// Text with multiple sections
commands.spawn((
// Create a TextBundle that has a Text with a list of sections.
TextBundle::from_sections([TextSection::new(
"FPS: ",
TextStyle {
// This font is loaded and will be used instead of the default font.
font_size: 20.0,
..default()
},
)])
.with_text_alignment(TextAlignment::Right)
.with_style(Style {
position_type: PositionType::Absolute,
bottom: Val::Px(5.0),
right: Val::Px(5.0),
..default()
}),
FpsText,
));
let material_emissive1 = standard_materials.add(StandardMaterial {
// 4. Put something bright in a dark environment to see the effect
emissive: Color::rgb_linear(13.99, 5.32, 2.0),
..default()
});
// light
commands.spawn(PointLightBundle {
point_light: PointLight {
intensity: 1500.0,
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(4.0, 8.0, -4.0),
..default()
});
// ground plane
commands.spawn(PbrBundle {
mesh: meshes.add(shape::Plane::from_size(50.0).into()),
material: standard_materials.add(Color::SILVER.into()),
..default()
});
commands.spawn((
PbrBundle {
mesh: meshes.add(shape::Cube::new(5.0).into()),
transform: Transform::from_xyz(12.0, 2.5, 0.0),
material: standard_materials.add(Color::SILVER.into()),
..default()
},
CubeState {
bounds_lower: (-2.5, -2.5),
bounds_upper: (2.5, 2.5),
},
));
}
fn text_update_system(
diagnostics: Res,
mut query: Query<&mut Text, With>,
mut commands: Commands,
mut spheres: ResMut>,
mut materials: ResMut>,
mut score: Query<&mut Score>,
) {
for mut text in &mut query {
if let Some(fps) = diagnostics.get(FrameTimeDiagnosticsPlugin::FPS) {
if let Some(value) = fps.smoothed() {
// Update the value of the second section
for mut a in &mut score {
let value = a.score;
text.sections[0].value = format!("Player Score: {value:.2}");
}
//println!("{:?}", text.sections);
}
}
}
}
fn move_coin(
input: Res>,
mut coins: Query<(&mut Transform, &mut CoinState)>,
timer: Res