diff --git a/Cargo.toml b/Cargo.toml index 73ea095..ef4ddb5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,3 +16,7 @@ opt-level = 1 # Enable high optimizations for dependencies (incl. Bevy), but not for our code: [profile.dev.package."*"] opt-level = 3 + +[profile.release] +opt-level = 'z' +lto = "thin" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 82c5949..a414eff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,18 +3,33 @@ //! It displays the current FPS in the top left corner, as well as text that changes color //! in the bottom right. For text within a scene, please see the text2d example. +use std::time::Duration; + use bevy::{ diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin}, prelude::*, + utils::Instant, + window::WindowMode, }; fn main() { + let window_plugin = WindowPlugin { + primary_window: Some(Window { + title: "Fullscreen Test".into(), + position: WindowPosition::Centered(MonitorSelection::Index(1)), + mode: WindowMode::Fullscreen, + present_mode: bevy::window::PresentMode::AutoNoVsync, + ..default() + }), + ..default() + }; App::new() - .add_plugins(DefaultPlugins) + .add_plugins(DefaultPlugins.set(window_plugin)) .add_plugin(FrameTimeDiagnosticsPlugin::default()) + .init_resource::() .add_startup_system(setup) - .add_system(text_update_system) - .add_system(text_color_system) + .add_system(bevy::window::close_on_esc) + .add_system(update_timer) .run(); } @@ -26,77 +41,112 @@ struct FpsText; #[derive(Component)] struct ColorText; +#[derive(Component)] +struct ShowTimer; + +#[derive(Debug, Resource)] +struct ForgeTimer { + /// The actual ON/PAUSE timer + time_current: bevy::prelude::Time, + // time_current: TimeCurrent, + /// The target to reach + time_target: isize, + // time_target: TimeTarget, + /// The time offset, usually negative + time_offset: isize, + // /// The calculated speedup/slowdown + // time_warp: f64, + // time_offset: TimeOffset, + /// Wether it is started + started: bool, +} + +impl FromWorld for ForgeTimer { + fn from_world(_world: &mut World) -> Self { + let time = Time::new(Instant::now()); + time.startup(); + ForgeTimer { + time_current: time, + time_target: 60 * 15 + 30, + time_offset: 60 * 14, + started: false, + } + } +} + +impl ForgeTimer { + fn to_str(&mut self) -> String { + let t = self.time_current.elapsed_seconds_f64() as isize; + self.time_current.update(); + let remaining = self.time_target - t; + let actual_remaining = self.time_target - self.time_offset - t; + // let scaled_remaining = remaining* + // let time_warp = (self.time_target - self.time_offset) * remaining / self.time_target; + let percent = actual_remaining * 100 / self.time_target; + // sec_to_str(remaining) + format!("{}", percent) + } +} + +fn sec_to_str(time: isize) -> String { + // let time = time.as_secs(); + let sign = if time < 0 { "-" } else { "" }; + let s = time % 60; + let m = (time / 60) % 3600; + let h = time / 3600; + format!("{}{:02}:{:02}:{:02}", sign, h, m, s) +} + fn setup(mut commands: Commands, asset_server: Res) { // UI camera + + // let mut forge_time = bevy::prelude::Time::default(); commands.spawn(Camera2dBundle::default()); - // Text with one section commands.spawn(( - // Create a TextBundle that has a Text with a single section. - TextBundle::from_section( - // Accepts a `String` or any type that converts into a `String`, such as `&str` - "hello\nbevy!", - TextStyle { - font: asset_server.load("fonts/digital-7.ttf"), - font_size: 100.0, - color: Color::WHITE, - }, - ) // Set the alignment of the Text - .with_text_alignment(TextAlignment::Center) - // Set the style of the TextBundle itself. - .with_style(Style { - position_type: PositionType::Absolute, - position: UiRect { - bottom: Val::Px(5.0), - right: Val::Px(15.0), - ..default() - }, - ..default() - }), - ColorText, - )); - // Text with multiple sections - commands.spawn(( - // Create a TextBundle that has a Text with a list of sections. TextBundle::from_sections([ TextSection::new( - "FPS: ", + "REMAINING TIME: ", TextStyle { font: asset_server.load("fonts/digital-7.ttf"), - font_size: 60.0, - color: Color::WHITE, + font_size: 60., + color: Color::GREEN, }, ), TextSection::from_style(TextStyle { font: asset_server.load("fonts/digital-7.ttf"), - font_size: 60.0, - color: Color::GOLD, + font_size: 60., + color: Color::GREEN, }), - ]), - FpsText, + ]) + .with_style(Style { + position_type: PositionType::Absolute, + position: UiRect { + bottom: Val::Px(500.0), + right: Val::Px(150.0), + ..default() + }, + ..default() + }) + .with_text_alignment(TextAlignment::Center), + ShowTimer, )); } -fn text_color_system(time: Res