Show FPS
This commit is contained in:
parent
9242f39bb0
commit
4c7e259537
5 changed files with 112 additions and 56 deletions
|
@ -16,7 +16,8 @@ pub fn update(
|
|||
) {
|
||||
if let Ok(mut camera) = camera.get_single_mut() {
|
||||
if let Ok(watched) = watched.get_single() {
|
||||
camera.translation.x = watched.translation.x
|
||||
camera.translation.x = watched.translation.x;
|
||||
camera.translation.y = watched.translation.y;
|
||||
} else {
|
||||
camera.translation = Vec3::ZERO;
|
||||
}
|
||||
|
|
154
src/main.rs
154
src/main.rs
|
@ -1,4 +1,5 @@
|
|||
use bevy::audio::{AudioPlugin, SpatialScale};
|
||||
use bevy::diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin};
|
||||
use bevy::prelude::{default, *};
|
||||
use bevy::window::{PrimaryWindow, WindowMode};
|
||||
|
||||
|
@ -39,58 +40,106 @@ fn main() {
|
|||
app.insert_state(View::default());
|
||||
app.insert_state(Game::default());
|
||||
|
||||
app.add_plugins((DefaultPlugins
|
||||
.set(WindowPlugin {
|
||||
primary_window: Some(Window {
|
||||
title: "Kodo Tag".into(),
|
||||
mode: bevy::window::WindowMode::BorderlessFullscreen,
|
||||
// resolution: (640. * 2., 360. * 2.).into(),
|
||||
app.add_plugins((
|
||||
DefaultPlugins
|
||||
.set(WindowPlugin {
|
||||
primary_window: Some(Window {
|
||||
title: "Kodo Tag".into(),
|
||||
mode: bevy::window::WindowMode::BorderlessFullscreen,
|
||||
// resolution: (640. * 2., 360. * 2.).into(),
|
||||
..default()
|
||||
}),
|
||||
..default()
|
||||
}),
|
||||
..default()
|
||||
})
|
||||
.set(AudioPlugin {
|
||||
default_spatial_scale: SpatialScale::new_2d(1.),
|
||||
global_volume: GlobalVolume::new(1.),
|
||||
..default()
|
||||
})
|
||||
.set(ImagePlugin::default_nearest()),))
|
||||
.add_systems(OnEnter(View::MainMenu), main_menu::startup)
|
||||
.add_systems(OnExit(View::MainMenu), main_menu::exit)
|
||||
.add_systems(OnEnter(View::InGame), (player::startup, statue::startup))
|
||||
.add_systems(OnExit(View::InGame), (player::exit, statue::exit))
|
||||
.add_systems(Startup, (assets::startup, camera::startup))
|
||||
.add_systems(
|
||||
Update,
|
||||
})
|
||||
.set(AudioPlugin {
|
||||
default_spatial_scale: SpatialScale::new_2d(1.),
|
||||
global_volume: GlobalVolume::new(1.),
|
||||
..default()
|
||||
})
|
||||
.set(ImagePlugin::default_nearest()),
|
||||
FrameTimeDiagnosticsPlugin,
|
||||
))
|
||||
.add_systems(OnEnter(View::MainMenu), main_menu::startup)
|
||||
.add_systems(OnExit(View::MainMenu), main_menu::exit)
|
||||
.add_systems(OnEnter(View::InGame), (player::startup, statue::startup))
|
||||
.add_systems(OnExit(View::InGame), (player::exit, statue::exit))
|
||||
.add_systems(Startup, (startup, assets::startup, camera::startup))
|
||||
.add_systems(
|
||||
Update,
|
||||
(
|
||||
update,
|
||||
(
|
||||
global_hotkeys,
|
||||
(
|
||||
player::sprite_select,
|
||||
player::controls,
|
||||
movement::update_velocity_by_heading,
|
||||
movement::resolve_velocity.after(movement::update_velocity_by_heading),
|
||||
movement::ysort.after(movement::update_velocity_by_heading),
|
||||
camera::update
|
||||
.after(player::controls)
|
||||
.after(movement::resolve_velocity),
|
||||
)
|
||||
.in_set(InGameSet)
|
||||
.run_if(in_state(View::InGame)),
|
||||
(main_menu::update)
|
||||
.in_set(MainMenuSet)
|
||||
.run_if(in_state(View::MainMenu)),
|
||||
),
|
||||
)
|
||||
.insert_resource(ClearColor(Color::rgb(0.3, 0.1, 0.5)))
|
||||
.insert_resource(AmbientLight {
|
||||
color: Color::rgb(1., 1., 1.),
|
||||
brightness: 1.,
|
||||
});
|
||||
player::sprite_select,
|
||||
player::controls,
|
||||
movement::update_velocity_by_heading,
|
||||
movement::resolve_velocity.after(movement::update_velocity_by_heading),
|
||||
movement::ysort.after(movement::update_velocity_by_heading),
|
||||
camera::update
|
||||
.after(player::controls)
|
||||
.after(movement::resolve_velocity),
|
||||
)
|
||||
.in_set(InGameSet)
|
||||
.run_if(in_state(View::InGame)),
|
||||
(main_menu::update)
|
||||
.in_set(MainMenuSet)
|
||||
.run_if(in_state(View::MainMenu)),
|
||||
),
|
||||
)
|
||||
.insert_resource(ClearColor(Color::rgb(0.3, 0.1, 0.5)))
|
||||
.insert_resource(AmbientLight {
|
||||
color: Color::rgb(1., 1., 1.),
|
||||
brightness: 1.,
|
||||
});
|
||||
|
||||
app.run()
|
||||
}
|
||||
|
||||
fn global_hotkeys(
|
||||
fn toggle_fullscreen(mut window: Mut<Window>) {
|
||||
if window.mode == WindowMode::Windowed {
|
||||
window.mode = WindowMode::BorderlessFullscreen
|
||||
} else {
|
||||
window.mode = WindowMode::Windowed
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
struct FpsText;
|
||||
|
||||
fn startup(mut commands: Commands, assets: Res<assets::AssetLoader>) {
|
||||
commands.spawn((
|
||||
FpsText,
|
||||
TextBundle::from_sections([
|
||||
TextSection::new(
|
||||
"FPS: ",
|
||||
TextStyle {
|
||||
font: assets.fonts.iosevkalytemin.clone(),
|
||||
font_size: 60.,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
TextSection::from_style(TextStyle {
|
||||
font: assets.fonts.iosevkalytemin.clone(),
|
||||
font_size: 60.,
|
||||
color: Color::Hsla {
|
||||
hue: 0.5,
|
||||
saturation: 0.5,
|
||||
lightness: 0.5,
|
||||
alpha: 0.5,
|
||||
},
|
||||
}),
|
||||
])
|
||||
.with_style(Style {
|
||||
position_type: PositionType::Absolute,
|
||||
bottom: Val::Px(5.0),
|
||||
right: Val::Px(5.0),
|
||||
..default()
|
||||
}),
|
||||
));
|
||||
}
|
||||
|
||||
fn update(
|
||||
diagnostics: Res<DiagnosticsStore>,
|
||||
mut query: Query<&mut Text, With<FpsText>>,
|
||||
keyboard: Res<ButtonInput<KeyCode>>,
|
||||
mut window: Query<&mut Window, With<PrimaryWindow>>,
|
||||
) {
|
||||
|
@ -101,12 +150,11 @@ fn global_hotkeys(
|
|||
toggle_fullscreen(window)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn toggle_fullscreen(mut window: Mut<Window>) {
|
||||
if window.mode == WindowMode::Windowed {
|
||||
window.mode = WindowMode::BorderlessFullscreen
|
||||
} else {
|
||||
window.mode = WindowMode::Windowed
|
||||
for mut text in &mut query {
|
||||
if let Some(fps) = diagnostics.get(&FrameTimeDiagnosticsPlugin::FPS) {
|
||||
if let Some(value) = fps.smoothed() {
|
||||
text.sections[1].value = format!("{value:.2}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,6 +12,9 @@ pub struct Heading(pub Vec2);
|
|||
#[derive(Component, Deref, DerefMut, Debug, Default)]
|
||||
pub struct Speed(pub f32);
|
||||
|
||||
#[derive(Component, Debug)]
|
||||
pub struct YSortable;
|
||||
|
||||
pub fn resolve_velocity(mut query: Query<(&Velocity, &mut Transform)>, time: Res<Time>) {
|
||||
for (velocity, mut transform) in query.iter_mut() {
|
||||
transform.translation += (velocity.0 * time.delta_seconds()).extend(0.);
|
||||
|
@ -24,7 +27,7 @@ pub fn update_velocity_by_heading(mut query: Query<(&mut Velocity, &Speed, &Head
|
|||
}
|
||||
}
|
||||
|
||||
pub fn ysort(mut q: Query<&mut Transform>) {
|
||||
pub fn ysort(mut q: Query<&mut Transform, With<YSortable>>) {
|
||||
q.iter_mut()
|
||||
.for_each(|mut tf| tf.translation.z = -tf.translation.y)
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ use bevy::sprite::Mesh2dHandle;
|
|||
use bevy::{prelude::*, window::PrimaryWindow};
|
||||
|
||||
use crate::camera::Watched;
|
||||
use crate::movement::YSortable;
|
||||
use crate::{
|
||||
assets::AssetLoader,
|
||||
movement::{Heading, Mover, Speed, Velocity},
|
||||
|
@ -30,6 +31,7 @@ pub fn startup(
|
|||
commands
|
||||
.spawn((
|
||||
Player,
|
||||
YSortable,
|
||||
Watched,
|
||||
SpriteSheetBundle {
|
||||
texture,
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::assets::AssetLoader;
|
||||
use crate::{assets::AssetLoader, movement::YSortable};
|
||||
use bevy::prelude::*;
|
||||
|
||||
#[derive(Component, Debug)]
|
||||
|
@ -8,6 +8,7 @@ pub fn startup(mut commands: Commands, assets: Res<AssetLoader>, asset_server: R
|
|||
let texture = asset_server.load("img/Props.png");
|
||||
commands.spawn((
|
||||
Statue,
|
||||
YSortable,
|
||||
SpriteSheetBundle {
|
||||
texture: texture.clone(),
|
||||
atlas: TextureAtlas {
|
||||
|
@ -20,6 +21,7 @@ pub fn startup(mut commands: Commands, assets: Res<AssetLoader>, asset_server: R
|
|||
));
|
||||
commands.spawn((
|
||||
Statue,
|
||||
YSortable,
|
||||
SpriteSheetBundle {
|
||||
texture: texture.clone(),
|
||||
atlas: TextureAtlas {
|
||||
|
|
Loading…
Reference in a new issue