Skip to content

Commit

Permalink
refactor property entity stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
sim82 committed May 8, 2022
1 parent 883d69b commit 3d25262
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 28 deletions.
5 changes: 4 additions & 1 deletion src/auto_collider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ pub fn attach_collider_system(
};

for entity in v.iter() {
commands.entity(*entity).insert(collider.clone());
commands
.entity(*entity)
.insert(collider.clone())
.remove::<AttachCollider>();
}
}
}
Expand Down
15 changes: 8 additions & 7 deletions src/debug_hud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use bevy_egui::{
EguiContext,
};

use crate::property::{PropertyName, PropertyRegistry, PropertyUpdateEvent, PropertyValue};
use crate::property::{PropertyRegistry, PropertyUpdateEvent, PropertyValue};

fn mag_to_str(mag: i32) -> &'static str {
match mag {
Expand Down Expand Up @@ -101,7 +101,7 @@ pub fn hud_egui_system(
mut egui_context: ResMut<EguiContext>,
property_registry: Res<PropertyRegistry>,
mut property_update_events: EventWriter<PropertyUpdateEvent>,
property_query: Query<(&PropertyValue, &PropertyName)>,
property_query: Query<(&PropertyValue, &Name)>,
diagnostics: Res<Diagnostics>,
// render_status: Res<RenderStatus>,
hud_elements_query: Query<(Entity, &HudOrder, &HudElement)>,
Expand Down Expand Up @@ -173,9 +173,9 @@ pub fn hud_egui_system(
Ok((property_value, property_name)) => {
match property_value {
PropertyValue::Bool(v) => {
if ui.button(format!("{}:{:?}", property_name.0, v)).clicked() {
if ui.button(format!("{}:{:?}", property_name, v)).clicked() {
property_update_events.send(PropertyUpdateEvent::new(
property_name.0.clone(),
property_name.to_string(),
PropertyValue::Bool(!v),
));
}
Expand All @@ -189,15 +189,15 @@ pub fn hud_egui_system(
{
commands.entity(entity).remove::<StringEdit>();
property_update_events.send(PropertyUpdateEvent::new(
property_name.0.clone(),
property_name.to_string(),
PropertyValue::String(
string_edit.current_string.clone(),
),
));
}
}
_ => {
if ui.button(&property_name.0).clicked() {
if ui.button(property_name.as_str()).clicked() {
commands.entity(entity).insert(StringEdit {
current_string: s.to_string(),
});
Expand All @@ -208,7 +208,7 @@ pub fn hud_egui_system(
let mut color = [color.x, color.y, color.z];
if ui.color_edit_button_rgb(&mut color).changed() {
property_update_events.send(PropertyUpdateEvent::new(
property_name.0.clone(),
property_name.to_string(),
PropertyValue::Color(color.into()),
));
}
Expand Down Expand Up @@ -250,6 +250,7 @@ pub fn hud_egui_system(

egui::Window::new("plots").show(egui_context.ctx_mut(), |ui| {
egui::plot::Plot::new("diag")
.legend(egui::plot::Legend::default())
.view_aspect(2.0)
.show(ui, |plot_ui| {
for points in plot_lines {
Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use game2::{
auto_collider::AttachCollider,
fx::{DoRotate, PlayerExplosion},
hex::HexCube,
property::PropertyValue,
};

use bevy::{
Expand Down Expand Up @@ -128,6 +129,11 @@ fn setup(
// let camera_pos = Vec3::new(-20.0, 2.0, -20.0);
// let camera_look = Vec3::new(2.0, -1.0, 2.0);

commands
.spawn()
.insert(Name::new("blub"))
.insert(PropertyValue::String("x".into()));

commands
.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_translation(camera_pos)
Expand Down
60 changes: 40 additions & 20 deletions src/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ use std::{
sync::Mutex,
};

#[derive(Debug, Clone, Component)]
pub struct PropertyName(pub String);

#[derive(Clone, Debug, PartialEq, Component)]
#[derive(Clone, Debug, PartialEq, Component, Reflect)]
#[reflect(Component)]
pub enum PropertyValue {
None,
Bool(bool),
String(String),
Color(Vec3),
}

impl Default for PropertyValue {
fn default() -> Self {
Self::None
}
}

#[derive(Debug)]
pub struct PropertyUpdateEvent {
name: String,
Expand All @@ -29,12 +33,14 @@ impl PropertyUpdateEvent {

#[derive(Component)]
pub struct PropertyAccess {
pub name: String,
pub cache: PropertyValue,
}

impl Default for PropertyAccess {
fn default() -> Self {
PropertyAccess {
name: default(),
cache: PropertyValue::None,
}
}
Expand All @@ -44,6 +50,7 @@ impl Default for PropertyAccess {
pub struct PropertyRegistry {
pub(crate) name_cache: HashMap<String, Option<Entity>>,
pending_create: Mutex<HashSet<String>>,
root_entity: Option<Entity>,
}

impl PropertyRegistry {
Expand All @@ -67,32 +74,44 @@ fn create_pending(mut commands: Commands, mut property_registry: ResMut<Property
for pending in std::mem::take(pending_create).drain() {
info!("spawn pending property entity: {}", pending);
property_registry.name_cache.insert(pending.clone(), None); // placeholder, will be filled by detect_change system
commands
.spawn()
.insert(PropertyName(pending))
.insert(PropertyValue::None);

let root = property_registry
.root_entity
.get_or_insert_with(|| commands.spawn().insert(Name::new("properties")).id());
commands.entity(*root).with_children(|commands| {
commands
.spawn()
.insert(Name::new(pending))
.insert(PropertyValue::None);
});
}
}
}
fn detect_change(
mut commands: Commands,
mut property_registry: ResMut<PropertyRegistry>,
query: Query<&PropertyValue>,
query_changed: Query<(Entity, &PropertyName, &PropertyValue), Changed<PropertyName>>,
mut query_access: Query<(Entity, &PropertyName, &mut PropertyAccess), Changed<PropertyName>>,
query_changed: Query<(Entity, &Name, &PropertyValue), Added<PropertyValue>>,
mut query_access: Query<(Entity, &mut PropertyAccess), Changed<PropertyAccess>>,
) {
for (ent, name, value) in query_changed.iter() {
info!("new: {:?} {:?} {:?}", ent, name, value);
property_registry
.name_cache
.insert(name.0.clone(), Some(ent));
.insert(name.to_string(), Some(ent));

let root = property_registry
.root_entity
.get_or_insert_with(|| commands.spawn().insert(Name::new("properties")).id());
commands.entity(*root).add_child(ent);
}

for (ent, name, mut access) in query_access.iter_mut() {
info!("new access. initial propagate: {:?} {:?}", ent, name);
for (ent, mut access) in query_access.iter_mut() {
info!("new access. initial propagate: {:?} {:?}", ent, access.name);
let value = query
.get(
property_registry
.get(&name.0)
.get(&access.name)
.expect("failed to get ent for property"),
)
.expect("missing property value for access");
Expand All @@ -103,22 +122,22 @@ fn detect_change(

fn update_event_listener(
mut events: EventReader<PropertyUpdateEvent>,
mut query: Query<(Entity, &PropertyName, &mut PropertyValue)>,
mut query2: Query<(Entity, &PropertyName, &mut PropertyAccess)>,
mut query: Query<(Entity, &Name, &mut PropertyValue)>,
mut query2: Query<(Entity, &Name, &mut PropertyAccess)>,
) {
let mut updates = HashMap::new();
for event in events.iter() {
info!("update: {:?}", event);
updates.insert(&event.name, &event.value);
updates.insert(event.name.as_str(), &event.value);
}
for (ent, name, mut value) in query.iter_mut() {
if let Some(new_value) = updates.get(&name.0) {
if let Some(new_value) = updates.get(name.as_str()) {
info!("propagate update to prop {:?}", ent);
*value = (**new_value).clone();
}
}
for (ent, name, mut access) in query2.iter_mut() {
if let Some(new_value) = updates.get(&name.0) {
if let Some(new_value) = updates.get(name.as_str()) {
info!("propagate update to access {:?}", ent);
access.cache = (**new_value).clone();
}
Expand All @@ -131,7 +150,8 @@ pub struct PropertyPlugin;
impl Plugin for PropertyPlugin {
fn build(&self, app: &mut App) {
info!("property entity plugin");
app.init_resource::<PropertyRegistry>()
app.register_type::<PropertyValue>()
.init_resource::<PropertyRegistry>()
.add_system(create_pending)
.add_system(detect_change)
.add_system(update_event_listener)
Expand Down

0 comments on commit 3d25262

Please sign in to comment.