Skip to content

Commit

Permalink
optimized executable size, balanced lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
cxreiff committed Jul 26, 2024
1 parent 26aa65d commit 2f91ca4
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 159 deletions.
119 changes: 22 additions & 97 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 5 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down
28 changes: 11 additions & 17 deletions src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
4 changes: 2 additions & 2 deletions src/fish/behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
}
}
Expand Down
31 changes: 18 additions & 13 deletions src/fish/lifecycle.rs
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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,
}
}
}
Expand All @@ -36,7 +41,7 @@ pub struct FishSkeletonBundle(SceneBundle);

fn setup_lifecycle_system(mut commands: Commands, asset_server: Res<AssetServer>) {
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));
Expand All @@ -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.),
);
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/fish/shared.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
Loading

0 comments on commit 2f91ca4

Please sign in to comment.