Skip to content

Commit

Permalink
Refactor applying sprite pack to all entities
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrankel committed Dec 9, 2023
1 parent 04705bf commit 21cde7d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 37 deletions.
1 change: 1 addition & 0 deletions src/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::physics::Velocity;
pub use crate::simulation::sprite_pack::SkinSet;
pub use crate::simulation::sprite_pack::SpritePack;
pub use crate::simulation::sprite_pack::SpritePackAssets;
pub use crate::simulation::sprite_pack::SpritePackEvent;
use crate::state::editor_screen::SceneViewBounds;
use crate::state::editor_screen::WrapWithinSceneView;
use crate::state::AppState;
Expand Down
64 changes: 55 additions & 9 deletions src/simulation/sprite_pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,30 @@ use bevy::prelude::*;
use bevy_asset_loader::prelude::*;
use rand::seq::IteratorRandom;
use rand::seq::SliceRandom;
use rand::thread_rng;
use rand::Rng;

use crate::simulation::Simulation;
use crate::AppRoot;
use crate::AppSet;

pub struct SpritePackPlugin;

impl Plugin for SpritePackPlugin {
fn build(&self, app: &mut App) {
app.register_type::<SpritePackAssets>()
.init_collection::<SpritePackAssets>();
.register_type::<SpritePackEvent>()
.add_event::<SpritePackEvent>()
.init_collection::<SpritePackAssets>()
.add_systems(
Update,
apply_sprite_pack
.in_set(AppSet::End)
.run_if(on_event::<SpritePackEvent>()),
);
}
}

fn random_color(mut rng: impl Rng) -> Color {
Color::rgb(
rng.gen_range(0.0..=1.0),
rng.gen_range(0.0..=1.0),
rng.gen_range(0.0..=1.0),
)
}

#[derive(AssetCollection, Resource, Reflect, Default)]
#[reflect(Resource)]
pub struct SpritePackAssets {
Expand Down Expand Up @@ -146,6 +151,47 @@ impl SpritePack {
commands.entity(entity).insert(skin.bundle(assets, size));
};
}

pub fn replace_skin_set(&mut self, skin_set: SkinSet, rng: impl Rng) {
*self = SpritePack::new(skin_set, self.skins.len(), rng);
}
}

fn random_color(mut rng: impl Rng) -> Color {
Color::rgb(
rng.gen_range(0.0..=1.0),
rng.gen_range(0.0..=1.0),
rng.gen_range(0.0..=1.0),
)
}

/// Sent when the sprite pack changes and should be re-applied
#[derive(Event, Reflect)]
pub struct SpritePackEvent;

fn apply_sprite_pack(
mut commands: Commands,
root: Res<AppRoot>,
simulation: Res<Simulation>,
assets: Res<SpritePackAssets>,
children_query: Query<&Children>,
sprite_query: Query<&TextureAtlasSprite>,
) {
let mut rng = thread_rng();

for &entity in children_query.get(root.world).ok().into_iter().flatten() {
let size = sprite_query
.get(entity)
.ok()
.and_then(|sprite| sprite.custom_size)
.unwrap_or_else(|| {
Vec2::splat(rng.gen_range(simulation.entity_size_min..=simulation.entity_size_max))
});

simulation
.sprite_pack
.apply(&mut commands, entity, &assets, size, &mut rng);
}
}

const DEFAULT_SKINS: [Skin; 2] = [
Expand Down
39 changes: 11 additions & 28 deletions src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use bevy::prelude::*;
use bevy_kira_audio::prelude::*;
use rand::seq::SliceRandom;
use rand::thread_rng;
use rand::Rng;
use strum::EnumCount;

use crate::audio::AudioAssets;
Expand All @@ -19,8 +18,7 @@ use crate::simulation::PassiveEntitySpawner;
use crate::simulation::Simulation;
use crate::simulation::SkinSet;
use crate::simulation::SpawnEvent;
use crate::simulation::SpritePack;
use crate::simulation::SpritePackAssets;
use crate::simulation::SpritePackEvent;
use crate::state::editor_screen::spawn_editor_screen;
use crate::state::editor_screen::SceneView;
use crate::state::editor_screen::SceneViewBounds;
Expand Down Expand Up @@ -403,32 +401,17 @@ generate_upgrade_list!(
presentation_score: 10.0,
base_cost: 25.0,
install: Some(world.register_system(|
mut commands: Commands,
mut events: EventWriter<SpritePackEvent>,
mut simulation: ResMut<Simulation>,
root: Res<AppRoot>,
assets: Res<SpritePackAssets>,
children_query: Query<&Children>,
sprite_query: Query<&TextureAtlasSprite>,
| {
// Set the new sprite pack
let num_skins = simulation.sprite_pack.skins.len();
simulation.sprite_pack = SpritePack::new(SkinSet::OneBit, num_skins, &mut thread_rng());

// Apply the new sprite pack to all existing entities
// TODO: Would be better to send an event and do this in a separate system
let mut rng = thread_rng();
for &entity in children_query.get(root.world).ok().into_iter().flatten() {
let size = sprite_query.get(entity).ok().and_then(|sprite| sprite.custom_size).unwrap_or_else(|| {
Vec2::splat(rng.gen_range(simulation.entity_size_min..=simulation.entity_size_max))
});
simulation.sprite_pack.apply(&mut commands, entity, &assets, size, &mut rng);
}
simulation.sprite_pack.replace_skin_set(SkinSet::OneBit, &mut thread_rng());
events.send(SpritePackEvent);
})),
..default()
},

EntitySkinPlugin: Upgrade {
name: "EntitySkinPlugin".to_string(),
SkinPlugin: Upgrade {
name: "SkinPlugin".to_string(),
desc: "Introduces a new entity skin with a random color. Makes your game prettier.".to_string(),
tech_debt: 1.0,
presentation_score: 4.0,
Expand All @@ -442,8 +425,8 @@ generate_upgrade_list!(
..default()
},

EntitySizePlugin: Upgrade {
name: "EntitySizePlugin".to_string(),
ScalePlugin: Upgrade {
name: "ScalePlugin".to_string(),
desc: "Increases the maximum entity size. Makes your game prettier.".to_string(),
tech_debt: 1.0,
presentation_score: 2.0,
Expand Down Expand Up @@ -473,8 +456,8 @@ generate_upgrade_list!(
..default()
},

SpeedupPlugin: Upgrade {
name: "SpeedupPlugin".to_string(),
SpeedPlugin: Upgrade {
name: "SpeedPlugin".to_string(),
desc: "Increases the entity movement speed. Makes your game more fun.".to_string(),
tech_debt: 1.0,
fun_score: 10.0,
Expand Down Expand Up @@ -633,7 +616,7 @@ generate_upgrade_list!(
..default()
},

// Themes
// Editor themes

DarkModeDracula: Upgrade {
name: "Dark Mode (Dracula)".to_string(),
Expand Down

0 comments on commit 21cde7d

Please sign in to comment.