-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use dedicated component to control alpha color
- Loading branch information
Showing
5 changed files
with
101 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use std::iter; | ||
|
||
use bevy::prelude::*; | ||
|
||
use crate::game_world::{family::FamilyMode, WorldState}; | ||
|
||
pub(super) struct AlphaColorPlugin; | ||
|
||
impl Plugin for AlphaColorPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_systems( | ||
PostUpdate, | ||
Self::update_materials | ||
.run_if(in_state(WorldState::City).or_else(in_state(FamilyMode::Building))), | ||
); | ||
} | ||
} | ||
|
||
impl AlphaColorPlugin { | ||
pub(super) fn update_materials( | ||
mut materials: ResMut<Assets<StandardMaterial>>, | ||
ghosts: Query<(Entity, &AlphaColor), Changed<AlphaColor>>, | ||
children: Query<&Children>, | ||
mut material_handles: Query<&mut Handle<StandardMaterial>>, | ||
) { | ||
let Ok((entity, &alpha)) = ghosts.get_single() else { | ||
return; | ||
}; | ||
|
||
debug!("setting alpha to `{alpha:?}`"); | ||
let mut iter = material_handles | ||
.iter_many_mut(iter::once(entity).chain(children.iter_descendants(entity))); | ||
while let Some(mut material_handle) = iter.fetch_next() { | ||
let material = materials | ||
.get(&*material_handle) | ||
.expect("material handle should be valid"); | ||
|
||
// If color matches, assume that we don't need any update. | ||
if material.base_color == *alpha { | ||
return; | ||
} | ||
|
||
let mut material = material.clone(); | ||
material.base_color = *alpha; | ||
material.alpha_mode = AlphaMode::Add; | ||
*material_handle = materials.add(material); | ||
} | ||
} | ||
} | ||
|
||
/// Blends material texture with the given color. | ||
#[derive(Component, Clone, Copy, Debug, Deref, DerefMut)] | ||
pub(super) struct AlphaColor(pub(super) Color); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters