Skip to content

Commit

Permalink
Support sprite packs
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrankel committed Dec 8, 2023
1 parent 180b8c5 commit 7ad0517
Show file tree
Hide file tree
Showing 18 changed files with 144 additions and 46 deletions.
Binary file added assets/image/entity/1-bit/Clothing.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/image/entity/1-bit/Creatures.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/image/entity/1-bit/Food.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/image/entity/1-bit/Gems-Jewels-and-Money.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/image/entity/1-bit/Instruments.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/image/entity/1-bit/Jewelry.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/image/entity/1-bit/Misc-Future.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/image/entity/1-bit/Misc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/image/entity/1-bit/People.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/image/entity/1-bit/Potions.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/image/entity/1-bit/Tools.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/image/entity/1-bit/UI.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/image/entity/1-bit/Weapons.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 23 additions & 15 deletions src/simulation.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
mod sprite_pack;

use std::f32::consts::TAU;

use bevy::math::vec2;
use bevy::prelude::*;
use rand::seq::SliceRandom;
use rand::Rng;

use crate::physics::Velocity;
pub use crate::simulation::sprite_pack::SpritePack;
pub use crate::simulation::sprite_pack::SpritePackAssets;
use crate::state::editor_screen::SceneViewBounds;
use crate::state::editor_screen::WrapWithinSceneView;
use crate::state::AppState;
Expand All @@ -22,6 +24,7 @@ impl Plugin for SimulationPlugin {
fn build(&self, app: &mut App) {
app.register_type::<SpawnEvent>()
.register_type::<IsEntityCap>()
.add_plugins(sprite_pack::SpritePackPlugin)
.add_event::<SpawnEvent>()
.init_resource::<Simulation>()
.init_resource::<PassiveCodeTyper>()
Expand All @@ -46,10 +49,9 @@ pub struct Simulation {
pub lines: f64,
pub entities: f64,
pub tech_debt: f64,

/// Fun factor, determines the score.
/// Fun score, affects the submission's results.
pub fun_score: f64,
/// Presentation factor, determines the score.
/// Presentation factor, affects the submissions' results.
pub presentation_score: f64,

/// Minimum size for new entities.
Expand All @@ -58,6 +60,8 @@ pub struct Simulation {
pub entity_size_max: f32,
/// List of colors that the new entities can have.
pub entity_colors: Vec<Color>,
/// The sprite pack to use.
pub sprite_pack: SpritePack,

/// Minimum offset distance for entities on spawn.
pub spawn_offset_min: f32,
Expand All @@ -74,12 +78,15 @@ impl Default for Simulation {
tech_debt: 0.0,
fun_score: 0.0,
presentation_score: 0.0,

entity_size_min: 8.0,
entity_size_max: 8.0,
entity_colors: vec![
Color::rgba(0.0, 0.0, 0.0, 1.0),
Color::rgba(1.0, 1.0, 1.0, 1.0),
],
sprite_pack: default(),

spawn_offset_min: 0.0,
spawn_offset_max: 2.0,
}
Expand All @@ -99,8 +106,9 @@ fn spawn_entities(
mut commands: Commands,
mut events: EventReader<SpawnEvent>,
root: Res<AppRoot>,
mut entity_cap_query: Query<&mut OverflowDespawnQueue, With<IsEntityCap>>,
mut simulation: ResMut<Simulation>,
sprite_pack_assets: Res<SpritePackAssets>,
mut entity_cap_query: Query<&mut OverflowDespawnQueue, With<IsEntityCap>>,
) {
let mut rng = rand::thread_rng();
for event in events.read() {
Expand All @@ -119,24 +127,24 @@ fn spawn_entities(
let position = (event.position + offset).extend(0.0);

let size = rng.gen_range(simulation.entity_size_min..=simulation.entity_size_max);
let size = Vec2::splat(size);

let entity = commands
.spawn((
Name::new("Entity"),
SpriteBundle {
sprite: Sprite {
color: *simulation.entity_colors.choose(&mut rng).unwrap(),
custom_size: Some(vec2(size, size)),
..default()
},
transform: Transform::from_translation(position),
..default()
},
SpatialBundle::from_transform(Transform::from_translation(position)),
Velocity(velocity),
WrapWithinSceneView,
))
.set_parent(root.world)
.id();
simulation.sprite_pack.apply(
&mut commands,
entity,
&sprite_pack_assets,
size,
&mut rng,
);

for mut despawn_queue in &mut entity_cap_query {
despawn_queue.push(entity);
Expand Down
89 changes: 89 additions & 0 deletions src/simulation/sprite_pack.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use bevy::prelude::*;
use bevy_asset_loader::prelude::*;
use rand::seq::SliceRandom;
use rand::Rng;

pub struct SpritePackPlugin;

impl Plugin for SpritePackPlugin {
fn build(&self, app: &mut App) {
app.register_type::<SpritePackAssets>()
.init_collection::<SpritePackAssets>();
}
}

pub enum SpritePack {
None(Vec<Color>),
OneBit(Vec<(Color, usize)>),
}

impl Default for SpritePack {
fn default() -> Self {
Self::None(vec![Color::BLACK, Color::WHITE])
}
}

impl SpritePack {
pub fn add_skin(&mut self, mut rng: impl Rng) {
let color = Color::Rgba {
red: rng.gen_range(0.0..1.0),
green: rng.gen_range(0.0..1.0),
blue: rng.gen_range(0.0..1.0),
alpha: 1.0,
};
match self {
Self::None(colors) => {
colors.push(color);
},
// TODO: Curate the tiles.
// FIXME: Prevent duplicates.
Self::OneBit(tiles) => tiles.push((color, rng.gen_range(0..=5))),
}
}

pub fn apply(
&self,
commands: &mut Commands,
entity: Entity,
assets: &SpritePackAssets,
size: Vec2,
mut rng: impl Rng,
) {
let (color, index) = match self {
Self::None(colors) => {
commands.entity(entity).insert(Sprite {
color: *colors.choose(&mut rng).unwrap(),
custom_size: Some(size),
..default()
});
return;
},
Self::OneBit(tiles) => *tiles.choose(&mut rng).unwrap(),
};
let handle = match self {
Self::None(..) => unreachable!(),
Self::OneBit(..) => assets.one_bit_food.clone(),
};

commands.entity(entity).insert((
TextureAtlasSprite {
color,
index,
custom_size: Some(size),
..default()
},
handle,
));
}
}

#[derive(AssetCollection, Resource, Reflect, Default)]
#[reflect(Resource)]
pub struct SpritePackAssets {
#[asset(texture_atlas(tile_size_x = 10.0, tile_size_y = 10.0, rows = 5, columns = 17))]
#[asset(path = "image/entity/1-bit/Food.png")]
pub one_bit_food: Handle<TextureAtlas>,
#[asset(texture_atlas(tile_size_x = 10.0, tile_size_y = 10.0, rows = 11, columns = 13))]
#[asset(path = "image/entity/1-bit/Weapons.png")]
pub one_bit_weapons: Handle<TextureAtlas>,
}
2 changes: 2 additions & 0 deletions src/state/loading_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use serde::Deserialize;
use serde::Serialize;

use crate::config::Config;
use crate::simulation::SpritePackAssets;
use crate::state::editor_screen::EditorScreenAssets;
use crate::state::AppState::*;
use crate::ui::FontSize;
Expand All @@ -20,6 +21,7 @@ impl Plugin for LoadingScreenStatePlugin {
app.register_type::<IsLoadingBarFill>()
.add_loading_state(LoadingState::new(LoadingScreen))
.add_collection_to_loading_state::<_, EditorScreenAssets>(LoadingScreen)
.add_collection_to_loading_state::<_, SpritePackAssets>(LoadingScreen)
.add_plugins(ProgressPlugin::new(LoadingScreen).continue_to(EditorScreen))
.add_systems(OnEnter(LoadingScreen), enter_loading)
.add_systems(OnExit(LoadingScreen), exit_loading)
Expand Down
2 changes: 2 additions & 0 deletions src/state/title_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use serde::Deserialize;
use serde::Serialize;

use crate::config::Config;
use crate::simulation::SpritePackAssets;
use crate::state::editor_screen::EditorScreenAssets;
use crate::state::AppState::*;
use crate::ui::FontSize;
Expand All @@ -24,6 +25,7 @@ impl Plugin for TitleScreenStatePlugin {
.init_collection::<TitleScreenAssets>()
.add_loading_state(LoadingState::new(TitleScreen))
.add_collection_to_loading_state::<_, EditorScreenAssets>(TitleScreen)
.add_collection_to_loading_state::<_, SpritePackAssets>(TitleScreen)
.add_plugins(ProgressPlugin::new(TitleScreen))
.add_systems(OnEnter(TitleScreen), enter_title_screen)
.add_systems(OnExit(TitleScreen), exit_title_screen);
Expand Down
59 changes: 28 additions & 31 deletions src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use bevy::ecs::system::SystemId;
use bevy::prelude::*;
use rand::seq::SliceRandom;
use rand::thread_rng;
use rand::Rng;
use strum::EnumCount;

use crate::config::Config;
Expand All @@ -16,6 +15,7 @@ use crate::simulation::PassiveCodeTyper;
use crate::simulation::PassiveEntitySpawner;
use crate::simulation::Simulation;
use crate::simulation::SpawnEvent;
use crate::simulation::SpritePack;
use crate::state::editor_screen::spawn_editor_screen;
use crate::state::editor_screen::SceneView;
use crate::state::editor_screen::SceneViewBounds;
Expand Down Expand Up @@ -293,7 +293,7 @@ fn load_upgrade_sequence(mut commands: Commands) {
vec![Inspiration],
vec![VelocityPlugin],
vec![ImportLibrary, SplashOfLifePlugin],
vec![Coffee],
vec![Coffee, OneBitSpritePack],
vec![Brainstorm],
]));
}
Expand Down Expand Up @@ -339,27 +339,30 @@ generate_upgrade_list!(

// Presentation score

OneBitSpritePack: Upgrade {
name: "1-bit Sprite Pack".to_string(),
desc: "Downloads a 1-bit sprite pack for your entities. Makes your game prettier.".to_string(),
presentation_score: 10.0,
base_cost: 25.0,
install: Some(world.register_system(|
mut simulation: ResMut<Simulation>
| {
simulation.sprite_pack = SpritePack::OneBit(vec![]);
simulation.sprite_pack.add_skin(&mut thread_rng());
})),
..default()
},
EntitySkinPlugin: Upgrade {
name: "EntitySkinPlugin".to_string(),
desc: "Introduces a new entity skin with a random color. Makes your game prettier.".to_string(),
presentation_score: 5.0,
presentation_score: 4.0,
base_cost: 10.0,
cost_scale_factor: 1.2,
weight: 1.0,
remaining: 5,
install: Some(
world.register_system(|mut simulation: ResMut<Simulation>| {
let mut rng = rand::thread_rng();
simulation.entity_colors.push(
Color::Rgba {
red: rng.gen_range(0.0..1.0),
green: rng.gen_range(0.0..1.0),
blue: rng.gen_range(0.0..1.0),
alpha: 1.0,
}
);
}),
),
install: Some(world.register_system(|mut simulation: ResMut<Simulation>| {
simulation.sprite_pack.add_skin(&mut thread_rng());
})),
..default()
},
EntitySizePlugin: Upgrade {
Expand All @@ -370,11 +373,9 @@ generate_upgrade_list!(
cost_scale_factor: 1.2,
weight: 1.0,
remaining: 2,
install: Some(
world.register_system(|mut simulation: ResMut<Simulation>| {
simulation.entity_size_max += 4.0;
}),
),
install: Some(world.register_system(|mut simulation: ResMut<Simulation>| {
simulation.entity_size_max += 4.0;
})),
..default()
},

Expand Down Expand Up @@ -674,11 +675,9 @@ generate_upgrade_list!(
name: "Brainstorm".to_string(),
desc: "Adds 1 extra upgrade slot.".to_string(),
tech_debt: 0.0,
install: Some(
world.register_system(|mut sequence: ResMut<UpgradeSequence>| {
sequence.slots += 1;
}),
),
install: Some(world.register_system(|mut sequence: ResMut<UpgradeSequence>| {
sequence.slots += 1;
})),
..default()
},
DesignDocument: Upgrade {
Expand All @@ -688,11 +687,9 @@ generate_upgrade_list!(
base_cost: 20.0,
upgrade_min: 7,
weight: 2.5,
install: Some(
world.register_system(|mut sequence: ResMut<UpgradeSequence>| {
sequence.slots += 1;
}),
),
install: Some(world.register_system(|mut sequence: ResMut<UpgradeSequence>| {
sequence.slots += 1;
})),
..default()
},
);

0 comments on commit 7ad0517

Please sign in to comment.