Skip to content

Commit

Permalink
Rework hover disabling to make it more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
Shatur committed Sep 11, 2024
1 parent 1a672c0 commit e917cb8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 56 deletions.
57 changes: 22 additions & 35 deletions base/src/game_world/family/building/wall/placing_wall.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use bevy::{
color::palettes::css::{RED, WHITE},
ecs::component::{ComponentHooks, StorageType},
math::Vec3Swizzles,
prelude::*,
render::view::NoFrustumCulling,
Expand All @@ -14,7 +13,7 @@ use crate::{
city::lot::LotVertices,
commands_history::{CommandsHistory, PendingDespawn},
family::building::{wall::Apertures, BuildingMode},
hover::{HoverEnabled, Hovered},
hover::{HoverPlugin, Hovered},
player_camera::CameraCaster,
spline::{dynamic_mesh::DynamicMesh, PointKind, SplineSegment},
Layer,
Expand All @@ -28,25 +27,27 @@ pub(super) struct PlacingWallPlugin;

impl Plugin for PlacingWallPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
Update,
(
app.observe(HoverPlugin::enable_on_remove::<PlacingWall>)
.observe(HoverPlugin::disable_on_add::<PlacingWall>)
.add_systems(
Update,
(
Self::spawn.run_if(in_state(WallTool::Create)),
Self::pick.run_if(in_state(WallTool::Move)),
)
.run_if(action_just_pressed(Action::Confirm))
.run_if(not(any_with_component::<PlacingWall>)),
(
Self::update_end,
Self::update_material,
Self::confirm.run_if(action_just_pressed(Action::Confirm)),
Self::delete.run_if(action_just_pressed(Action::Delete)),
Self::cancel.run_if(action_just_pressed(Action::Cancel)),
)
.run_if(in_state(BuildingMode::Walls)),
),
);
(
Self::spawn.run_if(in_state(WallTool::Create)),
Self::pick.run_if(in_state(WallTool::Move)),
)
.run_if(action_just_pressed(Action::Confirm))
.run_if(not(any_with_component::<PlacingWall>)),
(
Self::update_end,
Self::update_material,
Self::confirm.run_if(action_just_pressed(Action::Confirm)),
Self::delete.run_if(action_just_pressed(Action::Delete)),
Self::cancel.run_if(action_just_pressed(Action::Cancel)),
)
.run_if(in_state(BuildingMode::Walls)),
),
);
}
}

Expand Down Expand Up @@ -289,26 +290,12 @@ impl PlacingWallBundle {
}
}

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, Component)]
pub enum PlacingWall {
Spawning,
MovingPoint { entity: Entity, kind: PointKind },
}

impl Component for PlacingWall {
const STORAGE_TYPE: StorageType = StorageType::Table;

fn register_component_hooks(hooks: &mut ComponentHooks) {
hooks
.on_add(|mut world, _targeted_entity, _component_id| {
**world.resource_mut::<HoverEnabled>() = false;
})
.on_remove(|mut world, _targeted_entity, _component_id| {
**world.resource_mut::<HoverEnabled>() = true;
});
}
}

impl PlacingWall {
/// Returns point kind that should be edited for this wall.
fn point_kind(self) -> PointKind {
Expand Down
22 changes: 21 additions & 1 deletion base/src/game_world/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,34 @@ impl HoverPlugin {
commands.entity(hovered_entity).remove::<Hovered>();
}
}

pub(super) fn enable_on_remove<C: Component>(
trigger: Trigger<OnRemove, C>,
mut hover_enabled: ResMut<HoverEnabled>,
other_compoents: Query<Entity, With<C>>,
) {
if other_compoents
.iter()
.all(|entity| entity == trigger.entity())
{
hover_enabled.0 = true
}
}

pub(super) fn disable_on_add<C: Component>(
_trigger: Trigger<OnAdd, C>,
mut hover_enabled: ResMut<HoverEnabled>,
) {
hover_enabled.0 = false
}
}

fn hover_enabled(hover_enabled: Res<HoverEnabled>) -> bool {
hover_enabled.0
}

#[derive(Resource, Deref, DerefMut)]
pub(super) struct HoverEnabled(pub(super) bool);
pub(super) struct HoverEnabled(bool);

impl Default for HoverEnabled {
fn default() -> Self {
Expand Down
25 changes: 5 additions & 20 deletions base/src/game_world/object/placing_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ use std::{

use bevy::{
color::palettes::css::{RED, WHITE},
ecs::{
component::{ComponentHooks, StorageType},
reflect::ReflectCommandExt,
},
ecs::reflect::ReflectCommandExt,
prelude::*,
scene,
};
Expand All @@ -25,7 +22,7 @@ use crate::{
city::CityMode,
commands_history::{CommandsHistory, PendingDespawn},
family::building::BuildingMode,
hover::{HoverEnabled, Hovered},
hover::{HoverPlugin, Hovered},
object::{Object, ObjectCommand},
player_camera::CameraCaster,
Layer,
Expand All @@ -42,6 +39,8 @@ impl Plugin for PlacingObjectPlugin {
fn build(&self, app: &mut App) {
app.add_plugins(WallSnapPlugin)
.add_plugins(SideSnapPlugin)
.observe(HoverPlugin::enable_on_remove::<PlacingObject>)
.observe(HoverPlugin::disable_on_add::<PlacingObject>)
.add_systems(
PreUpdate,
Self::init
Expand Down Expand Up @@ -333,7 +332,7 @@ impl PlacingObjectPlugin {
}

/// Marks an entity as an object that should be moved with cursor to preview spawn position.
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Component)]
pub struct PlacingObject {
kind: PlacingObjectKind,
rotation_limit: Option<f32>,
Expand All @@ -355,20 +354,6 @@ impl PlacingObject {
}
}

impl Component for PlacingObject {
const STORAGE_TYPE: StorageType = StorageType::Table;

fn register_component_hooks(hooks: &mut ComponentHooks) {
hooks
.on_add(|mut world, _targeted_entity, _component_id| {
**world.resource_mut::<HoverEnabled>() = false;
})
.on_remove(|mut world, _targeted_entity, _component_id| {
**world.resource_mut::<HoverEnabled>() = true;
});
}
}

#[derive(Debug, Clone, Copy)]
pub enum PlacingObjectKind {
Spawning(AssetId<ObjectInfo>),
Expand Down

0 comments on commit e917cb8

Please sign in to comment.