Skip to content

Commit e8a9018

Browse files
authored
refactor: modify assets to cross reference each other (#87)
1 parent 766cb0f commit e8a9018

File tree

20 files changed

+269
-172
lines changed

20 files changed

+269
-172
lines changed

src/assets/loaders/toml.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
use std::marker::PhantomData;
22

3-
use bevy::{asset::AssetLoader, prelude::*};
3+
use bevy::{
4+
asset::{AssetLoader, LoadContext},
5+
prelude::*,
6+
};
47
use serde::Deserialize;
58
use thiserror::Error;
69

10+
#[derive(Debug, Error)]
11+
pub enum TomlLoaderError {
12+
#[error("Could not load asset: {0}")]
13+
Io(#[from] std::io::Error),
14+
#[error("Could not parse TOML: {0}")]
15+
TomlError(#[from] toml::de::Error),
16+
}
17+
18+
pub trait FromToml {
19+
type Raw: for<'de> Deserialize<'de>;
20+
21+
fn from_toml(raw: Self::Raw, load_context: &mut LoadContext) -> Self;
22+
}
23+
724
pub struct TomlAssetPlugin<T> {
825
extensions: Vec<&'static str>,
926
_marker: PhantomData<T>,
@@ -20,7 +37,7 @@ impl<T> TomlAssetPlugin<T> {
2037

2138
impl<T> Plugin for TomlAssetPlugin<T>
2239
where
23-
T: Asset + for<'de> Deserialize<'de>,
40+
T: Asset + FromToml,
2441
{
2542
fn build(&self, app: &mut App) {
2643
app.init_asset::<T>();
@@ -37,17 +54,9 @@ pub struct TomlAssetLoader<T> {
3754
_marker: PhantomData<T>,
3855
}
3956

40-
#[derive(Debug, Error)]
41-
pub enum TomlLoaderError {
42-
#[error("Could not load asset: {0}")]
43-
Io(#[from] std::io::Error),
44-
#[error("Could not parse TOML: {0}")]
45-
TomlError(#[from] toml::de::Error),
46-
}
47-
4857
impl<T> AssetLoader for TomlAssetLoader<T>
4958
where
50-
T: Asset + for<'de> Deserialize<'de>,
59+
T: Asset + FromToml,
5160
{
5261
type Asset = T;
5362
type Settings = ();
@@ -57,12 +66,12 @@ where
5766
&self,
5867
reader: &mut dyn bevy::asset::io::Reader,
5968
_settings: &Self::Settings,
60-
_load_context: &mut bevy::asset::LoadContext<'_>,
69+
load_context: &mut bevy::asset::LoadContext<'_>,
6170
) -> Result<Self::Asset, Self::Error> {
6271
let mut bytes = Vec::new();
6372
reader.read_to_end(&mut bytes).await?;
64-
let asset: T = toml::from_slice(&bytes)?;
65-
Ok(asset)
73+
let raw: T::Raw = toml::from_slice(&bytes)?;
74+
Ok(T::from_toml(raw, load_context))
6675
}
6776

6877
fn extensions(&self) -> &[&str] {

src/gameplay/hud/tome/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use bevy::{
77
use crate::{
88
gameplay::{
99
hud::tome::widgets::{TAB_COLOR_CHECKED, TAB_COLOR_DEFAULT},
10-
recipe::assets::RecipeDef,
10+
recipe::assets::Recipe,
1111
},
1212
input::input_map::{Action, action_just_pressed},
1313
screens::Screen,
@@ -74,7 +74,7 @@ pub enum TomeFocused {
7474
None,
7575
Item(Entity),
7676
People(Entity),
77-
Recipes(AssetId<RecipeDef>),
77+
Recipes(AssetId<Recipe>),
7878
}
7979

8080
#[derive(Component, Reflect, Debug, Default)]

src/gameplay/hud/tome/tab_inspect.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
hud::tome::{TomeOpen, TomeTab, UITomeLeftPageRoot, UITomeRightPageRoot},
66
item::inventory::Inventory,
77
recipe::{
8-
assets::RecipeDef,
8+
assets::Recipe,
99
select::{RecipeChanged, SelectRecipe},
1010
},
1111
},
@@ -40,7 +40,7 @@ pub struct Inspect {
4040

4141
#[derive(Component, Reflect, Debug)]
4242
#[reflect(Component)]
43-
pub struct Recipe(pub AssetId<RecipeDef>);
43+
pub struct RecipeButton(pub AssetId<Recipe>);
4444

4545
fn on_inspect(
4646
inspect: On<Inspect>,
@@ -56,7 +56,7 @@ fn on_inspect(
5656
fn spawn_inspect_recipes(
5757
mut commands: Commands,
5858
left_page: Single<Entity, With<UITomeLeftPageRoot>>,
59-
recipes: Res<Assets<RecipeDef>>,
59+
recipes: Res<Assets<Recipe>>,
6060
) {
6161
let id = commands
6262
.spawn((
@@ -70,7 +70,7 @@ fn spawn_inspect_recipes(
7070
commands.spawn((
7171
widgets::recipe_plate(asset_id),
7272
ChildOf(id),
73-
Recipe(asset_id),
73+
RecipeButton(asset_id),
7474
observe(on_recipe_select),
7575
));
7676
}
@@ -80,7 +80,7 @@ fn on_recipe_select(
8080
click: On<Pointer<Click>>,
8181
mut commands: Commands,
8282
inspected: Res<Inspected>,
83-
recipe_badges: Query<&Recipe>,
83+
recipe_badges: Query<&RecipeButton>,
8484
) {
8585
let Ok(recipe) = recipe_badges.get(click.entity) else {
8686
return;

src/gameplay/hud/tome/tab_recipes.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use bevy::prelude::*;
33
use crate::{
44
gameplay::{
55
hud::tome::{TomeTab, UITomeLeftPageRoot},
6-
recipe::assets::RecipeDef,
6+
recipe::assets::Recipe,
77
},
88
widgets,
99
};
@@ -15,7 +15,7 @@ pub(super) fn plugin(app: &mut App) {
1515
fn spawn_recipe_list(
1616
mut commands: Commands,
1717
left_page: Single<Entity, With<UITomeLeftPageRoot>>,
18-
recipe_defs: Res<Assets<RecipeDef>>,
18+
recipes: Res<Assets<Recipe>>,
1919
) {
2020
let recipe_list = commands
2121
.spawn((
@@ -25,7 +25,7 @@ fn spawn_recipe_list(
2525
))
2626
.id();
2727

28-
for (asset_id, _) in recipe_defs.iter() {
28+
for (asset_id, _) in recipes.iter() {
2929
commands.spawn((widgets::recipe_plate(asset_id), ChildOf(recipe_list)));
3030
}
3131
}

src/gameplay/item/assets.rs

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use serde::Deserialize;
33

44
use crate::assets::{
55
indexing::{AssetIndexPlugin, Indexable},
6-
loaders::toml::TomlAssetPlugin,
6+
loaders::toml::{FromToml, TomlAssetPlugin},
77
tracking::LoadResource,
88
};
99

@@ -17,7 +17,7 @@ pub fn plugin(app: &mut App) {
1717
}
1818

1919
#[derive(Asset, Clone, Debug, Deserialize, Reflect)]
20-
pub struct ItemDef {
20+
pub struct ItemRaw {
2121
pub id: String,
2222
pub name: String,
2323
pub description: String,
@@ -28,6 +28,43 @@ pub struct ItemDef {
2828
pub transport: Transport,
2929
}
3030

31+
fn placeholder_sprite() -> String {
32+
String::from("sprites/items/placeholder.png")
33+
}
34+
35+
#[derive(Asset, Reflect, Debug)]
36+
pub struct ItemDef {
37+
pub id: String,
38+
pub name: String,
39+
pub description: String,
40+
pub sprite: AssetId<Image>,
41+
pub stack_size: u32,
42+
pub taxonomy: Taxonomy,
43+
pub transport: Transport,
44+
}
45+
46+
impl FromToml for ItemDef {
47+
type Raw = ItemRaw;
48+
49+
fn from_toml(raw: Self::Raw, load_context: &mut bevy::asset::LoadContext) -> Self {
50+
Self {
51+
id: raw.id,
52+
name: raw.name,
53+
description: raw.description,
54+
sprite: load_context.load(raw.sprite).id(),
55+
stack_size: raw.stack_size,
56+
taxonomy: raw.taxonomy,
57+
transport: raw.transport,
58+
}
59+
}
60+
}
61+
62+
impl Indexable for ItemDef {
63+
fn index(&self) -> &String {
64+
&self.id
65+
}
66+
}
67+
3168
#[derive(Component, Clone, Debug, Deserialize, Reflect, PartialEq, Eq)]
3269
#[reflect(Component)]
3370
pub enum Taxonomy {
@@ -42,16 +79,6 @@ pub enum Transport {
4279
Bag,
4380
}
4481

45-
fn placeholder_sprite() -> String {
46-
String::from("sprites/items/placeholder.png")
47-
}
48-
49-
impl Indexable for ItemDef {
50-
fn index(&self) -> &String {
51-
&self.id
52-
}
53-
}
54-
5582
#[derive(Asset, Clone, Resource, Reflect)]
5683
#[reflect(Resource)]
5784
pub struct ItemAssets {

src/gameplay/item/stack.rs

Whitespace-only changes.

src/gameplay/logistics/pathfinding.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::gameplay::{
1212
porter::{PorterArrival, PorterLost},
1313
},
1414
recipe::{
15-
assets::RecipeDef,
15+
assets::Recipe,
1616
select::{RecipeChanged, SelectedRecipe},
1717
},
1818
world::{
@@ -41,7 +41,7 @@ pub struct WalkPath(pub Vec<Entity>);
4141

4242
fn pathfind(
4343
structures: Query<(Entity, &SelectedRecipe)>,
44-
recipes: Res<Assets<RecipeDef>>,
44+
recipes: Res<Assets<Recipe>>,
4545
pathable_query: Query<&Pathable>,
4646
coordinates: Query<&Coord>,
4747
mut commands: Commands,

src/gameplay/logistics/porter.rs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
use bevy::{prelude::*, sprite::Anchor};
22
use bevy_aseprite_ultra::prelude::{Animation, AseAnimation};
33

4-
use crate::{
5-
assets::indexing::IndexMap,
6-
gameplay::{
7-
FactorySystems,
8-
item::{
9-
assets::{ItemDef, Transport},
10-
inventory::Inventory,
11-
},
12-
logistics::pathfinding::{PorterPaths, WalkPath},
13-
recipe::{assets::RecipeDef, select::SelectedRecipe},
14-
sprite_sort::{YSortSprite, ZIndexSprite},
4+
use crate::gameplay::{
5+
FactorySystems,
6+
item::{
7+
assets::{ItemDef, Transport},
8+
inventory::Inventory,
159
},
10+
logistics::pathfinding::{PorterPaths, WalkPath},
11+
recipe::{assets::Recipe, select::SelectedRecipe},
12+
sprite_sort::{YSortSprite, ZIndexSprite},
1613
};
1714

1815
pub(super) fn plugin(app: &mut App) {
@@ -69,9 +66,8 @@ fn spawn_porter(
6966
&mut PorterPaths,
7067
)>,
7168
mut commands: Commands,
72-
item_definitions: Res<Assets<ItemDef>>,
73-
item_index: Res<IndexMap<ItemDef>>,
74-
recipe_defs: Res<Assets<RecipeDef>>,
69+
item_defs: Res<Assets<ItemDef>>,
70+
recipes: Res<Assets<Recipe>>,
7571
asset_server: Res<AssetServer>,
7672
time: Res<Time>,
7773
) {
@@ -89,16 +85,11 @@ fn spawn_porter(
8985
continue;
9086
}
9187

92-
let Some(recipe) = recipe_defs.get(&selected_recipe.0) else {
88+
let Some(recipe) = recipes.get(&selected_recipe.0) else {
9389
continue;
9490
};
9591

96-
let Some(item_id) = recipe
97-
.output
98-
.iter()
99-
.nth(index.0)
100-
.and_then(|(raw_item_id, _)| item_index.get(raw_item_id))
101-
else {
92+
let Some((item_id, _)) = recipe.output.iter().nth(index.0) else {
10293
continue;
10394
};
10495

@@ -114,7 +105,7 @@ fn spawn_porter(
114105
continue;
115106
};
116107

117-
let Some(item_def) = item_definitions.get(*item_id) else {
108+
let Some(item_def) = item_defs.get(*item_id) else {
118109
continue;
119110
};
120111

src/gameplay/people/naming.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ use rand::seq::SliceRandom;
33
use serde::Deserialize;
44

55
use crate::{
6-
assets::{loaders::toml::TomlAssetPlugin, tracking::LoadResource},
6+
assets::{
7+
loaders::toml::{FromToml, TomlAssetPlugin},
8+
tracking::LoadResource,
9+
},
710
gameplay::random::Seed,
811
screens::Screen,
912
};
@@ -25,11 +28,26 @@ pub(super) fn plugin(app: &mut App) {
2528
);
2629
}
2730

28-
#[derive(Asset, Reflect, Deserialize)]
31+
#[derive(Deserialize)]
32+
pub struct NamesRaw {
33+
pub neutral_names: Vec<String>,
34+
}
35+
36+
#[derive(Asset, Reflect, Debug)]
2937
pub struct Names {
3038
pub neutral_names: Vec<String>,
3139
}
3240

41+
impl FromToml for Names {
42+
type Raw = NamesRaw;
43+
44+
fn from_toml(raw: Self::Raw, _load_context: &mut bevy::asset::LoadContext) -> Self {
45+
Self {
46+
neutral_names: raw.neutral_names,
47+
}
48+
}
49+
}
50+
3351
#[derive(Asset, Resource, Reflect, Debug, Clone)]
3452
#[reflect(Resource)]
3553
pub struct NameAssets {

src/gameplay/player.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ pub(super) fn plugin(app: &mut App) {
2626
#[reflect(Component)]
2727
pub struct Player;
2828

29-
fn spawn_player(mut commands: Commands, assets: Res<Assets<ItemDef>>, mut seed: ResMut<Seed>) {
29+
fn spawn_player(mut commands: Commands, item_defs: Res<Assets<ItemDef>>, mut seed: ResMut<Seed>) {
3030
let mut inventory = Inventory::default();
3131

32-
for (item_id, _) in assets.iter() {
32+
for (item_id, _) in item_defs.iter() {
3333
inventory.items.insert(item_id, seed.random_range(0..100));
3434
}
3535

0 commit comments

Comments
 (0)