diff --git a/Cargo.lock b/Cargo.lock index 41854c47c..16e48e636 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "ab_glyph" -version = "0.2.20" +version = "0.2.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe21446ad43aa56417a767f3e2f3d7c4ca522904de1dd640529a76e9c5c3b33c" +checksum = "5110f1c78cf582855d895ecd0746b653db010cec6d9f5575293f27934d980a39" dependencies = [ "ab_glyph_rasterizer", "owned_ttf_parser", @@ -110,9 +110,9 @@ dependencies = [ [[package]] name = "aho-corasick" -version = "0.7.20" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" +checksum = "67fc08ce920c31afb70f013dcce1bfc3a3195de6a228474e45e1f145b36f8d04" dependencies = [ "memchr", ] @@ -276,9 +276,9 @@ checksum = "ecc7ab41815b3c653ccd2978ec3255c81349336702dfdf62ee6f7069b12a3aae" [[package]] name = "atomic_refcell" -version = "0.1.9" +version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "857253367827bd9d0fd973f0ef15506a96e79e41b0ad7aa691203a4e3214f6c8" +checksum = "79d6dc922a2792b006573f60b2648076355daeae5ce9cb59507e5908c9625d31" [[package]] name = "atty" @@ -554,9 +554,9 @@ dependencies = [ [[package]] name = "bevy_egui" -version = "0.20.2" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3c40ec476443b97d5d9c6f40668da7ded2d83b13955a7c94c062ad2b6f98120" +checksum = "e5e269226c4a8fb659aa3161c19925650ebe2c23021ddf4dd9ed191cf708364f" dependencies = [ "arboard", "bevy", @@ -1170,9 +1170,9 @@ dependencies = [ [[package]] name = "bumpalo" -version = "3.12.0" +version = "3.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535" +checksum = "9b1ce199063694f33ffb7dd4e0ee620741495c32833cde5aa08f02a0bf96f0c8" [[package]] name = "bytemuck" @@ -1296,9 +1296,9 @@ dependencies = [ [[package]] name = "clap" -version = "3.2.23" +version = "3.2.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "71655c45cb9845d3270c9d6df84ebe72b4dad3c2ba3f7023ad47c144e4e473a5" +checksum = "eef2b3ded6a26dfaec672a742c93c8cf6b689220324da509ec5caa20de55dc83" dependencies = [ "bitflags", "clap_lex", @@ -1949,9 +1949,9 @@ dependencies = [ [[package]] name = "gilrs" -version = "0.10.1" +version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d0342acdc7b591d171212e17c9350ca02383b86d5f9af33c6e3598e03a6c57e" +checksum = "62fd19844d0eb919aca41d3e4ea0e0b6bf60e1e827558b101c269015b8f5f27a" dependencies = [ "fnv", "gilrs-core", @@ -1962,9 +1962,9 @@ dependencies = [ [[package]] name = "gilrs-core" -version = "0.5.4" +version = "0.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "373b55d596e5a84fff61c559de54351a69f9ff481593ac78c35d9dc625df8d8f" +checksum = "fd745b0cb1a207756e8fabacf5623066ad6aa543ea0be4bab34e897e6bbe24f9" dependencies = [ "core-foundation", "io-kit-sys", @@ -2491,9 +2491,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.141" +version = "0.2.142" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5" +checksum = "6a987beff54b60ffa6d51982e1aa1146bc42f19bd26be28b0586f252fccf5317" [[package]] name = "libloading" @@ -2756,9 +2756,9 @@ dependencies = [ [[package]] name = "ntapi" -version = "0.4.0" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc51db7b362b205941f71232e56c625156eb9a929f8cf74a428fd5bc094a4afc" +checksum = "e8a3895c6391c39d7fe7ebc444a87eb2991b2a0bc718fdabd071eec617fc68e4" dependencies = [ "winapi", ] @@ -2989,9 +2989,9 @@ checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" [[package]] name = "owned_ttf_parser" -version = "0.18.1" +version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e25e9fb15717794fae58ab55c26e044103aad13186fbb625893f9a3bbcc24228" +checksum = "706de7e2214113d63a8238d1910463cfce781129a6f263d13fdb09ff64355ba4" dependencies = [ "ttf-parser", ] @@ -3277,13 +3277,13 @@ dependencies = [ [[package]] name = "regex" -version = "1.7.3" +version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b1f693b24f6ac912f4893ef08244d70b6067480d2f1a46e950c9691e6749d1d" +checksum = "af83e617f331cc6ae2da5443c602dfa5af81e517212d9d611a5b3ba1777b5370" dependencies = [ "aho-corasick", "memchr", - "regex-syntax", + "regex-syntax 0.7.1", ] [[package]] @@ -3292,7 +3292,7 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" dependencies = [ - "regex-syntax", + "regex-syntax 0.6.29", ] [[package]] @@ -3301,6 +3301,12 @@ version = "0.6.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" +[[package]] +name = "regex-syntax" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5996294f19bd3aae0453a862ad728f60e6600695733dd5df01da90c54363a3c" + [[package]] name = "renderdoc-sys" version = "0.7.1" @@ -3541,9 +3547,9 @@ dependencies = [ [[package]] name = "taffy" -version = "0.3.11" +version = "0.3.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fab62c50c3d17993e7f0c72932e51ceeac5ec2b51c225fda8529d606159c99d8" +checksum = "3540ec65df399929a04a485feb50144475735920cc47eaf8eba09c70b1df4055" dependencies = [ "arrayvec", "num-traits", @@ -3650,11 +3656,10 @@ dependencies = [ [[package]] name = "tracing" -version = "0.1.37" +version = "0.1.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8" +checksum = "cf9cf6a813d3f40c88b0b6b6f29a5c95c6cdbf97c1f9cc53fb820200f5ad814d" dependencies = [ - "cfg-if", "pin-project-lite", "tracing-attributes", "tracing-core", @@ -3662,13 +3667,13 @@ dependencies = [ [[package]] name = "tracing-attributes" -version = "0.1.23" +version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4017f8f45139870ca7e672686113917c71c7a6e02d4924eda67186083c03081a" +checksum = "0f57e3ca2a01450b1a921183a9c9cbfda207fd822cef4ccb00a65402cbba7a74" dependencies = [ "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.15", ] [[package]] @@ -3694,9 +3699,9 @@ dependencies = [ [[package]] name = "tracing-subscriber" -version = "0.3.16" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6176eae26dd70d0c919749377897b54a9276bd7061339665dd68777926b5a70" +checksum = "30a651bc37f915e81f087d86e62a18eec5f79550c7faff886f7090b4ea757c77" dependencies = [ "matchers", "nu-ansi-term", @@ -3723,9 +3728,9 @@ dependencies = [ [[package]] name = "ttf-parser" -version = "0.18.1" +version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0609f771ad9c6155384897e1df4d948e692667cc0588548b68eb44d052b27633" +checksum = "44dcf002ae3b32cd25400d6df128c5babec3927cd1eb7ce813cfff20eb6c3746" [[package]] name = "twox-hash" diff --git a/emergence_lib/src/lib.rs b/emergence_lib/src/lib.rs index eef2566e2..242af806d 100644 --- a/emergence_lib/src/lib.rs +++ b/emergence_lib/src/lib.rs @@ -11,7 +11,6 @@ pub mod asset_management; pub mod construction; pub mod crafting; -pub mod curves; pub mod enum_iter; pub mod filtered_array_iter; pub mod graphics; @@ -25,6 +24,7 @@ pub mod structures; pub mod terrain; pub mod ui; pub mod units; +pub mod utils; pub mod world_gen; /// Various app configurations, used for testing. diff --git a/emergence_lib/src/ui/status.rs b/emergence_lib/src/ui/status.rs index 7288a358d..194183d05 100644 --- a/emergence_lib/src/ui/status.rs +++ b/emergence_lib/src/ui/status.rs @@ -16,6 +16,7 @@ use crate::{ goals::{Goal, GoalKind}, unit_manifest::Unit, }, + utils::fallible_commands::FallibleEntityCommandExt, }; use super::ui_assets::Icons; @@ -185,10 +186,10 @@ fn add_status_displays( // - it will be hidden when the parent entity is hidden commands .entity(parent_entity) - .insert(StatusParent { + .try_insert(StatusParent { entity: status_entity, }) - .add_child(status_entity); + .try_add_child(status_entity); } } diff --git a/emergence_lib/src/curves.rs b/emergence_lib/src/utils/curves.rs similarity index 100% rename from emergence_lib/src/curves.rs rename to emergence_lib/src/utils/curves.rs diff --git a/emergence_lib/src/utils/fallible_commands.rs b/emergence_lib/src/utils/fallible_commands.rs new file mode 100644 index 000000000..f7946397e --- /dev/null +++ b/emergence_lib/src/utils/fallible_commands.rs @@ -0,0 +1,58 @@ +//! Contains [`FallibleEntityCommandExt`] and related code. + +use bevy::{ecs::system::EntityCommands, prelude::*}; + +/// An extension trait for [`EntityCommands`] that has fallible versions of +/// the most commonly used commands. + +pub trait FallibleEntityCommandExt<'w, 's, 'a> { + /// Attempts to add a component or bundle to the entity. + /// + /// Fails silently (rather than panicking) if the entity does not exist. + /// + /// Fallible version of [`EntityCommands::insert`]. + fn try_insert(&mut self, bundle: impl Bundle) -> &mut EntityCommands<'w, 's, 'a>; + + /// Attempts to remove a component or bundle from the entity. + /// + /// Fails silently (rather than panicking) if the entity does not exist. + /// + /// Fallible version of [`EntityCommands::remove`]. + fn try_remove(&mut self) -> &mut EntityCommands<'w, 's, 'a>; + + /// Attempts to add a child entity to the entity. + /// + /// Fails silently (rather than panicking) if the entity does not exist. + /// + /// Fallible version of [`BuildChildren::add_child`]. + fn try_add_child(&mut self, child: Entity) -> &mut EntityCommands<'w, 's, 'a>; +} + +impl<'w, 's, 'a> FallibleEntityCommandExt<'w, 's, 'a> for EntityCommands<'w, 's, 'a> { + fn try_insert(&mut self, bundle: impl Bundle) -> &mut Self { + self.add(|entity, world: &mut World| { + if let Some(mut entity_mut) = world.get_entity_mut(entity) { + entity_mut.insert(bundle); + } + }); + self + } + + fn try_remove(&mut self) -> &mut Self { + self.add(|entity, world: &mut World| { + if let Some(mut entity_mut) = world.get_entity_mut(entity) { + entity_mut.remove::(); + } + }); + self + } + + fn try_add_child(&mut self, child: Entity) -> &mut EntityCommands<'w, 's, 'a> { + self.add(move |entity, world: &mut World| { + if let Some(mut entity_mut) = world.get_entity_mut(entity) { + entity_mut.add_child(child); + } + }); + self + } +} diff --git a/emergence_lib/src/utils/mod.rs b/emergence_lib/src/utils/mod.rs new file mode 100644 index 000000000..651086d79 --- /dev/null +++ b/emergence_lib/src/utils/mod.rs @@ -0,0 +1,4 @@ +//! Simple gameplay-agnostic utilities. + +pub mod curves; +pub mod fallible_commands;