This commit is contained in:
Daniel Flanagan 2024-07-29 16:54:43 -05:00
parent 9242f39bb0
commit 4c7e259537
5 changed files with 112 additions and 56 deletions

View file

@ -16,7 +16,8 @@ pub fn update(
) { ) {
if let Ok(mut camera) = camera.get_single_mut() { if let Ok(mut camera) = camera.get_single_mut() {
if let Ok(watched) = watched.get_single() { 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 { } else {
camera.translation = Vec3::ZERO; camera.translation = Vec3::ZERO;
} }

View file

@ -1,4 +1,5 @@
use bevy::audio::{AudioPlugin, SpatialScale}; use bevy::audio::{AudioPlugin, SpatialScale};
use bevy::diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin};
use bevy::prelude::{default, *}; use bevy::prelude::{default, *};
use bevy::window::{PrimaryWindow, WindowMode}; use bevy::window::{PrimaryWindow, WindowMode};
@ -39,58 +40,106 @@ fn main() {
app.insert_state(View::default()); app.insert_state(View::default());
app.insert_state(Game::default()); app.insert_state(Game::default());
app.add_plugins((DefaultPlugins app.add_plugins((
.set(WindowPlugin { DefaultPlugins
primary_window: Some(Window { .set(WindowPlugin {
title: "Kodo Tag".into(), primary_window: Some(Window {
mode: bevy::window::WindowMode::BorderlessFullscreen, title: "Kodo Tag".into(),
// resolution: (640. * 2., 360. * 2.).into(), mode: bevy::window::WindowMode::BorderlessFullscreen,
// resolution: (640. * 2., 360. * 2.).into(),
..default()
}),
..default() ..default()
}), })
..default() .set(AudioPlugin {
}) default_spatial_scale: SpatialScale::new_2d(1.),
.set(AudioPlugin { global_volume: GlobalVolume::new(1.),
default_spatial_scale: SpatialScale::new_2d(1.), ..default()
global_volume: GlobalVolume::new(1.), })
..default() .set(ImagePlugin::default_nearest()),
}) FrameTimeDiagnosticsPlugin,
.set(ImagePlugin::default_nearest()),)) ))
.add_systems(OnEnter(View::MainMenu), main_menu::startup) .add_systems(OnEnter(View::MainMenu), main_menu::startup)
.add_systems(OnExit(View::MainMenu), main_menu::exit) .add_systems(OnExit(View::MainMenu), main_menu::exit)
.add_systems(OnEnter(View::InGame), (player::startup, statue::startup)) .add_systems(OnEnter(View::InGame), (player::startup, statue::startup))
.add_systems(OnExit(View::InGame), (player::exit, statue::exit)) .add_systems(OnExit(View::InGame), (player::exit, statue::exit))
.add_systems(Startup, (assets::startup, camera::startup)) .add_systems(Startup, (startup, assets::startup, camera::startup))
.add_systems( .add_systems(
Update, Update,
(
update,
( (
global_hotkeys, player::sprite_select,
( player::controls,
player::sprite_select, movement::update_velocity_by_heading,
player::controls, movement::resolve_velocity.after(movement::update_velocity_by_heading),
movement::update_velocity_by_heading, movement::ysort.after(movement::update_velocity_by_heading),
movement::resolve_velocity.after(movement::update_velocity_by_heading), camera::update
movement::ysort.after(movement::update_velocity_by_heading), .after(player::controls)
camera::update .after(movement::resolve_velocity),
.after(player::controls) )
.after(movement::resolve_velocity), .in_set(InGameSet)
) .run_if(in_state(View::InGame)),
.in_set(InGameSet) (main_menu::update)
.run_if(in_state(View::InGame)), .in_set(MainMenuSet)
(main_menu::update) .run_if(in_state(View::MainMenu)),
.in_set(MainMenuSet) ),
.run_if(in_state(View::MainMenu)), )
), .insert_resource(ClearColor(Color::rgb(0.3, 0.1, 0.5)))
) .insert_resource(AmbientLight {
.insert_resource(ClearColor(Color::rgb(0.3, 0.1, 0.5))) color: Color::rgb(1., 1., 1.),
.insert_resource(AmbientLight { brightness: 1.,
color: Color::rgb(1., 1., 1.), });
brightness: 1.,
});
app.run() 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>>, keyboard: Res<ButtonInput<KeyCode>>,
mut window: Query<&mut Window, With<PrimaryWindow>>, mut window: Query<&mut Window, With<PrimaryWindow>>,
) { ) {
@ -101,12 +150,11 @@ fn global_hotkeys(
toggle_fullscreen(window) toggle_fullscreen(window)
} }
} }
} for mut text in &mut query {
if let Some(fps) = diagnostics.get(&FrameTimeDiagnosticsPlugin::FPS) {
fn toggle_fullscreen(mut window: Mut<Window>) { if let Some(value) = fps.smoothed() {
if window.mode == WindowMode::Windowed { text.sections[1].value = format!("{value:.2}");
window.mode = WindowMode::BorderlessFullscreen }
} else { }
window.mode = WindowMode::Windowed
} }
} }

View file

@ -12,6 +12,9 @@ pub struct Heading(pub Vec2);
#[derive(Component, Deref, DerefMut, Debug, Default)] #[derive(Component, Deref, DerefMut, Debug, Default)]
pub struct Speed(pub f32); pub struct Speed(pub f32);
#[derive(Component, Debug)]
pub struct YSortable;
pub fn resolve_velocity(mut query: Query<(&Velocity, &mut Transform)>, time: Res<Time>) { pub fn resolve_velocity(mut query: Query<(&Velocity, &mut Transform)>, time: Res<Time>) {
for (velocity, mut transform) in query.iter_mut() { for (velocity, mut transform) in query.iter_mut() {
transform.translation += (velocity.0 * time.delta_seconds()).extend(0.); 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() q.iter_mut()
.for_each(|mut tf| tf.translation.z = -tf.translation.y) .for_each(|mut tf| tf.translation.z = -tf.translation.y)
} }

View file

@ -3,6 +3,7 @@ use bevy::sprite::Mesh2dHandle;
use bevy::{prelude::*, window::PrimaryWindow}; use bevy::{prelude::*, window::PrimaryWindow};
use crate::camera::Watched; use crate::camera::Watched;
use crate::movement::YSortable;
use crate::{ use crate::{
assets::AssetLoader, assets::AssetLoader,
movement::{Heading, Mover, Speed, Velocity}, movement::{Heading, Mover, Speed, Velocity},
@ -30,6 +31,7 @@ pub fn startup(
commands commands
.spawn(( .spawn((
Player, Player,
YSortable,
Watched, Watched,
SpriteSheetBundle { SpriteSheetBundle {
texture, texture,

View file

@ -1,4 +1,4 @@
use crate::assets::AssetLoader; use crate::{assets::AssetLoader, movement::YSortable};
use bevy::prelude::*; use bevy::prelude::*;
#[derive(Component, Debug)] #[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"); let texture = asset_server.load("img/Props.png");
commands.spawn(( commands.spawn((
Statue, Statue,
YSortable,
SpriteSheetBundle { SpriteSheetBundle {
texture: texture.clone(), texture: texture.clone(),
atlas: TextureAtlas { atlas: TextureAtlas {
@ -20,6 +21,7 @@ pub fn startup(mut commands: Commands, assets: Res<AssetLoader>, asset_server: R
)); ));
commands.spawn(( commands.spawn((
Statue, Statue,
YSortable,
SpriteSheetBundle { SpriteSheetBundle {
texture: texture.clone(), texture: texture.clone(),
atlas: TextureAtlas { atlas: TextureAtlas {