From 2f91ca4336ad5fe90701d700da181d17c39c6cc7 Mon Sep 17 00:00:00 2001 From: cxreiff Date: Fri, 26 Jul 2024 14:37:33 -0700 Subject: [PATCH] optimized executable size, balanced lifecycle --- Cargo.lock | 119 ++++++++---------------------------------- Cargo.toml | 20 ++----- src/draw.rs | 28 ++++------ src/fish/behavior.rs | 4 +- src/fish/lifecycle.rs | 31 ++++++----- src/fish/shared.rs | 9 ++-- src/fish/spawning.rs | 8 +-- src/input.rs | 1 - src/lib.rs | 2 + src/tank.rs | 12 ++--- 10 files changed, 75 insertions(+), 159 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b4fa4ef..18c6e33 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -461,7 +461,6 @@ dependencies = [ "futures-io", "futures-lite", "js-sys", - "notify-debouncer-full", "parking_lot", "ron", "serde", @@ -641,6 +640,19 @@ dependencies = [ "syn 2.0.72", ] +[[package]] +name = "bevy_embedded_assets" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3f3e7b41a9b01b767ba07c4ebb083b2d8d8055e57ddef7e55e502bbfc961505" +dependencies = [ + "bevy", + "cargo-emit", + "futures-io", + "futures-lite", + "thiserror", +] + [[package]] name = "bevy_encase_derive" version = "0.14.0" @@ -1430,6 +1442,12 @@ dependencies = [ "thiserror", ] +[[package]] +name = "cargo-emit" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1582e1c9e755dd6ad6b224dcffb135d199399a4568d454bd89fe515ca8425695" + [[package]] name = "cassowary" version = "0.3.0" @@ -2033,27 +2051,6 @@ dependencies = [ "simd-adler32", ] -[[package]] -name = "file-id" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6584280525fb2059cba3db2c04abf947a1a29a45ddae89f3870f8281704fafc9" -dependencies = [ - "windows-sys 0.48.0", -] - -[[package]] -name = "filetime" -version = "0.2.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ee447700ac8aa0b2f2bd7bc4462ad686ba06baa6727ac149a2d6277f0d240fd" -dependencies = [ - "cfg-if", - "libc", - "redox_syscall 0.4.1", - "windows-sys 0.52.0", -] - [[package]] name = "fixedbitset" version = "0.4.2" @@ -2118,15 +2115,6 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aa9a19cbb55df58761df49b23516a86d432839add4af60fc256da840f66ed35b" -[[package]] -name = "fsevent-sys" -version = "4.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76ee7a02da4d231650c7cea31349b889be2f45ddb3ef3032d2ec8185f6313fd2" -dependencies = [ - "libc", -] - [[package]] name = "futures-core" version = "0.3.30" @@ -2205,7 +2193,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbb5e8d912059b33b463831c16b838d15c4772d584ce332e4a80f6dffdae2bc1" dependencies = [ "core-foundation", - "inotify 0.10.2", + "inotify", "io-kit-sys", "js-sys", "libc", @@ -2529,17 +2517,6 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a257582fdcde896fd96463bf2d40eefea0580021c0712a0e2b028b60b47a837a" -[[package]] -name = "inotify" -version = "0.9.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8069d3ec154eb856955c1c0fbffefbf5f3c40a104ec912d4797314c1801abff" -dependencies = [ - "bitflags 1.3.2", - "inotify-sys", - "libc", -] - [[package]] name = "inotify" version = "0.10.2" @@ -2674,26 +2651,6 @@ version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc" -[[package]] -name = "kqueue" -version = "1.0.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7447f1ca1b7b563588a205fe93dea8df60fd981423a768bc1c0ded35ed147d0c" -dependencies = [ - "kqueue-sys", - "libc", -] - -[[package]] -name = "kqueue-sys" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed9625ffda8729b85e45cf04090035ac368927b8cebc34898e7c120f52e4838b" -dependencies = [ - "bitflags 1.3.2", - "libc", -] - [[package]] name = "ktx2" version = "0.3.0" @@ -2792,10 +2749,11 @@ dependencies = [ [[package]] name = "lifecycler" -version = "0.1.4" +version = "0.1.6" dependencies = [ "bevy", "bevy_atmosphere", + "bevy_embedded_assets", "bevy_hanabi", "bevy_ratatui", "bevy_ratatui_render", @@ -3071,39 +3029,6 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0676bb32a98c1a483ce53e500a81ad9c3d5b3f7c920c28c24e9cb0980d0b5bc8" -[[package]] -name = "notify" -version = "6.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6205bd8bb1e454ad2e27422015fb5e4f2bcc7e08fa8f27058670d208324a4d2d" -dependencies = [ - "bitflags 2.6.0", - "crossbeam-channel", - "filetime", - "fsevent-sys", - "inotify 0.9.6", - "kqueue", - "libc", - "log", - "mio", - "walkdir", - "windows-sys 0.48.0", -] - -[[package]] -name = "notify-debouncer-full" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49f5dab59c348b9b50cf7f261960a20e389feb2713636399cd9082cd4b536154" -dependencies = [ - "crossbeam-channel", - "file-id", - "log", - "notify", - "parking_lot", - "walkdir", -] - [[package]] name = "ntapi" version = "0.4.1" diff --git a/Cargo.toml b/Cargo.toml index 90d2add..d85bea7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,12 +1,13 @@ [package] name = "lifecycler" -version = "0.1.6" +version = "0.1.7" edition = "2021" license = "MIT OR Apache-2.0 OR CC0-1.0" [dependencies] bevy = "0.14.0" bevy_atmosphere = "0.10.0" +bevy_embedded_assets = "0.11.0" bevy_hanabi = "0.12.0" bevy_ratatui = "0.6.1" bevy_ratatui_render = "0.5.2" @@ -15,17 +16,6 @@ rand = "0.8.5" rand_chacha = "0.3.1" ratatui = "0.27.0" -[features] -default = [ - "dev_native", -] -dev = [] -dev_native = [ - "dev", - "bevy/file_watcher", - "bevy/embedded_watcher", -] - [lints.clippy] too_many_arguments = "allow" type_complexity = "allow" @@ -38,9 +28,9 @@ opt-level = 3 [profile.release] codegen-units = 1 -lto = "thin" -opt-level = 3 -strip = "none" +lto = true +strip = "debuginfo" +opt-level = 'z' [profile.release-native] inherits = "release" diff --git a/src/draw.rs b/src/draw.rs index 3ba3186..d44bb2e 100644 --- a/src/draw.rs +++ b/src/draw.rs @@ -28,33 +28,27 @@ fn draw_scene_system( frame.render_widget(widget, frame.size()); } - #[cfg(feature = "dev")] if flags.debug { if let Some(value) = diagnostics .get(&FrameTimeDiagnosticsPlugin::FPS) .and_then(|fps| fps.smoothed()) { - let msg = &flags.msg; - let position = Rect::new(0, 1, frame.size().width, 1); - let fps = Text::raw(format!("[msg: {msg}] [fps: {value:.0}]")) + let _msg = &flags.msg; + let position = Rect::new( + (frame.size().width / 2 + frame.size().width.min(frame.size().height * 2) / 2) + .saturating_sub(13), + 1, + 11, + 1, + ); + let fps = Text::raw(format!(" fps: {value:.0} ")) .alignment(Alignment::Center) - .bg(ratatui::style::Color::Black); + .bg(ratatui::style::Color::Black) + .fg(ratatui::style::Color::White); frame.render_widget(fps, position); } } - - // let position = Rect::new( - // (frame.size().width / 2).saturating_sub(15), - // frame.size().bottom().saturating_sub(2), - // 30, - // 1, - // ); - // let keys_info = Text::raw("[q to quit][? for information]") - // .alignment(Alignment::Center) - // .bg(ratatui::style::Color::Black); - - // frame.render_widget(keys_info, position); })?; Ok(()) diff --git a/src/fish/behavior.rs b/src/fish/behavior.rs index 12d4772..e6ca4be 100644 --- a/src/fish/behavior.rs +++ b/src/fish/behavior.rs @@ -148,7 +148,7 @@ impl<'a> FishOperations<'a> { commands: &mut Commands, ) { if let Ok((pellet_entity, pellet_transform)) = pellet { - if self.mortality.satiation == FISH_SATIATION_MAX { + if self.mortality.satiation >= FISH_SATIATION_MAX { self.start_seek_point(rng); return; } @@ -171,7 +171,7 @@ impl<'a> FishOperations<'a> { < 0.17 { if let Some(mut entity) = commands.get_entity(pellet_entity) { - self.mortality.satiation += 1; + self.mortality.satiation += 10; entity.insert(AttemptDespawn); } } diff --git a/src/fish/lifecycle.rs b/src/fish/lifecycle.rs index 6b75bb1..52c7cb9 100644 --- a/src/fish/lifecycle.rs +++ b/src/fish/lifecycle.rs @@ -1,8 +1,11 @@ use std::f32::consts::PI; use bevy::prelude::*; +use rand::RngCore; -use super::shared::{FISH_AGING_SPEED_SECONDS, FISH_BULK_MAX, FISH_LIFESPAN, FISH_SATIATION_MAX}; +use super::shared::{ + FishRng, FISH_AGING_INTERVAL_SECONDS, FISH_AVERAGE_LONGEVITY, FISH_BULK_MAX, FISH_SATIATION_MAX, +}; pub(super) fn plugin(app: &mut App) { app.add_systems(Startup, setup_lifecycle_system) @@ -15,15 +18,17 @@ pub struct FishMortality { age: u32, pub(super) satiation: u32, bulk: u32, + longevity: u32, } -impl Default for FishMortality { - fn default() -> Self { +impl FishMortality { + pub fn new(rng: &mut FishRng) -> Self { Self { - next_age_timer: Timer::from_seconds(FISH_AGING_SPEED_SECONDS, TimerMode::Repeating), + next_age_timer: Timer::from_seconds(FISH_AGING_INTERVAL_SECONDS, TimerMode::Repeating), age: 0, satiation: FISH_SATIATION_MAX / 2, bulk: 0, + longevity: FISH_AVERAGE_LONGEVITY + rng.next_u32() % 32 - 16, } } } @@ -36,7 +41,7 @@ pub struct FishSkeletonBundle(SceneBundle); fn setup_lifecycle_system(mut commands: Commands, asset_server: Res) { let fish_skeleton = SceneBundle { - scene: asset_server.load(GltfAssetLabel::Scene(0).from_asset("skeleton.glb")), + scene: asset_server.load(GltfAssetLabel::Scene(0).from_asset("embedded://skeleton.glb")), ..default() }; commands.insert_resource(FishSkeletonBundle(fish_skeleton)); @@ -61,20 +66,20 @@ fn age_the_living_system( mortality.bulk += 1; } - transform.scale = Vec3::new( - 0.1 + 0.2 * (mortality.bulk as f32 / FISH_BULK_MAX as f32), - 0.1 + 0.1 * (mortality.bulk as f32 / FISH_BULK_MAX as f32) - + 0.25 * (mortality.satiation as f32 / FISH_SATIATION_MAX as f32), - 0.1, - ); - - if mortality.satiation == 0 || mortality.age > FISH_LIFESPAN { + if mortality.satiation == 0 || mortality.age > mortality.longevity { commands.entity(entity).despawn(); let mut fish_skeleton = fish_skeleton_bundle.clone(); fish_skeleton.transform = transform.with_rotation(Quat::from_rotation_x(PI)); commands.spawn((FishSkeleton, fish_skeleton)); } } + + transform.scale = Vec3::new( + 0.1 + 0.15 * (mortality.bulk as f32 / FISH_BULK_MAX as f32), + 0.1, + 0.05 + 0.15 * (mortality.bulk as f32 / FISH_BULK_MAX as f32) + + 0.05 * (mortality.satiation as f32 / FISH_SATIATION_MAX as f32).min(1.), + ); } } diff --git a/src/fish/shared.rs b/src/fish/shared.rs index cd0e785..bb350d6 100644 --- a/src/fish/shared.rs +++ b/src/fish/shared.rs @@ -1,10 +1,11 @@ use bevy::prelude::*; use rand_chacha::ChaCha8Rng; -pub(super) const FISH_MAX: usize = 16; -pub(super) const FISH_AGING_SPEED_SECONDS: f32 = 1.; -pub(super) const FISH_SATIATION_MAX: u32 = 100; -pub(super) const FISH_LIFESPAN: u32 = 100; +pub(super) const FISH_MAX: usize = 12; +pub(super) const FISH_SPAWN_INTERVAL_SECONDS: f32 = 5.; +pub(super) const FISH_AGING_INTERVAL_SECONDS: f32 = 10.; +pub(super) const FISH_SATIATION_MAX: u32 = 64; +pub(super) const FISH_AVERAGE_LONGEVITY: u32 = 64; pub(super) const FISH_BULK_MAX: u32 = 32; #[derive(Component)] diff --git a/src/fish/spawning.rs b/src/fish/spawning.rs index d354b3d..455dda6 100644 --- a/src/fish/spawning.rs +++ b/src/fish/spawning.rs @@ -7,7 +7,7 @@ use rand_chacha::ChaCha8Rng; use super::behavior::FishBehavior; use super::lifecycle::{FishMortality, FishSkeleton}; -use super::shared::{Fish, FishRng, FISH_MAX}; +use super::shared::{Fish, FishRng, FISH_MAX, FISH_SPAWN_INTERVAL_SECONDS}; pub(super) fn plugin(app: &mut App) { app.add_systems(Startup, setup_fish_system) @@ -33,7 +33,7 @@ fn setup_fish_system( mesh: 0, primitive: 0, }) - .from_asset("fish.glb"), + .from_asset("embedded://fish.glb"), ); commands.insert_resource(FishMesh(fish_mesh)); @@ -54,7 +54,7 @@ fn setup_fish_system( commands.insert_resource(FishMaterials(fish_materials)); commands.insert_resource(FishRng(seeded_rng)); - let fish_timer = Timer::from_seconds(0.5, TimerMode::Repeating); + let fish_timer = Timer::from_seconds(FISH_SPAWN_INTERVAL_SECONDS, TimerMode::Repeating); commands.insert_resource(FishTimer(fish_timer)); } @@ -81,7 +81,7 @@ fn spawn_fish_system( commands.spawn(( Fish, FishBehavior::default(), - FishMortality::default(), + FishMortality::new(&mut fish_rng), PbrBundle { transform, mesh: fish_mesh.clone(), diff --git a/src/input.rs b/src/input.rs index 3cb78d0..3010953 100644 --- a/src/input.rs +++ b/src/input.rs @@ -20,7 +20,6 @@ pub fn handle_input_system( exit.send_default(); } - #[cfg(feature = "dev")] KeyCode::Char('d') => { flags.debug = !flags.debug; } diff --git a/src/lib.rs b/src/lib.rs index dcc9bda..6a5a5e0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ use bevy::{ window::ExitCondition, }; use bevy_atmosphere::plugin::AtmospherePlugin; +use bevy_embedded_assets::EmbeddedAssetPlugin; use bevy_hanabi::HanabiPlugin; use bevy_ratatui::RatatuiPlugins; use bevy_ratatui_render::RatatuiRenderPlugin; @@ -30,6 +31,7 @@ impl Plugin for AppPlugin { exit_condition: ExitCondition::DontExit, close_when_requested: false, }), + EmbeddedAssetPlugin::default(), // DefaultPlugins, ScheduleRunnerPlugin::run_loop(Duration::from_secs_f64(1. / 90.)), FrameTimeDiagnosticsPlugin, diff --git a/src/tank.rs b/src/tank.rs index 0fa70b6..7014ef6 100644 --- a/src/tank.rs +++ b/src/tank.rs @@ -19,14 +19,14 @@ fn setup_tank_system( SceneBundle { transform: Transform::from_scale(Vec3::new(2., 0.5, 2.)) .with_rotation(Quat::from_rotation_x(PI / 2.)), - scene: asset_server.load(GltfAssetLabel::Scene(0).from_asset("tank.glb")), + scene: asset_server.load(GltfAssetLabel::Scene(0).from_asset("embedded://tank.glb")), ..default() }, )); commands.spawn(SceneBundle { transform: Transform::from_scale(Vec3::new(1.8, 1., 0.5)) .with_translation(Vec3::new(0., -1.75, 0.)), - scene: asset_server.load(GltfAssetLabel::Scene(0).from_asset("gravel.glb")), + scene: asset_server.load(GltfAssetLabel::Scene(0).from_asset("embedded://gravel.glb")), ..default() }); @@ -34,7 +34,7 @@ fn setup_tank_system( transform: Transform::from_scale(Vec3::new(2., 0.6, 2.0)) .with_translation(Vec3::new(0., 0.0, 0.)) .with_rotation(Quat::from_rotation_x(PI / 2.)), - scene: asset_server.load(GltfAssetLabel::Scene(0).from_asset("base.glb")), + scene: asset_server.load(GltfAssetLabel::Scene(0).from_asset("embedded://base.glb")), ..default() }); @@ -60,7 +60,7 @@ fn setup_tank_system( mesh: 0, primitive: 0, }) - .from_asset("coral.glb"), + .from_asset("embedded://coral.glb"), ); commands.spawn(coral_bundle_1); @@ -74,12 +74,12 @@ fn setup_tank_system( mesh: 1, primitive: 0, }) - .from_asset("coral.glb"), + .from_asset("embedded://coral.glb"), ); commands.spawn(coral_bundle_2); let rock_bundle = SceneBundle { - scene: asset_server.load(GltfAssetLabel::Scene(0).from_asset("rocks.glb")), + scene: asset_server.load(GltfAssetLabel::Scene(0).from_asset("embedded://rocks.glb")), ..default() };