Skip to content

Commit 4cd76b7

Browse files
authored
fix: new items n sprites (#23)
1 parent 2457775 commit 4cd76b7

9 files changed

Lines changed: 133 additions & 79 deletions

File tree

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
assets/**/*.png filter=lfs diff=lfs merge=lfs -text
2+
assets/**/*.aseprite filter=lfs diff=lfs merge=lfs -text
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
id = "quintessence"
2+
name = "Quintessence"
3+
sprite = "sprites/items/quintessence.png"
4+
stack_size = 10

assets/items/ritual_doll.item.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
id = "ritual_doll"
2+
name = "Ritual Doll"
3+
sprite = "sprites/items/ritual_doll.png"
4+
stack_size = 5
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 3 additions & 0 deletions
Loading

src/simulation/hud/item_slot.rs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ fn on_slot_drag_and_drop(
118118
mut commands: Commands,
119119
item_query: Query<&InSlot>,
120120
slot_query: Query<&SlotOccupant>,
121-
// names: Query<&Name>,
122121
) {
123122
let destination_slot = trigger.target();
124123
let source_item = trigger.dropped;
@@ -131,18 +130,6 @@ fn on_slot_drag_and_drop(
131130
return;
132131
}
133132

134-
// info!(
135-
// "moving {} to {}",
136-
// names
137-
// .get(source_item)
138-
// .map(|a| a.to_string())
139-
// .unwrap_or("unknown".into()),
140-
// names
141-
// .get(destination_slot)
142-
// .map(|a| a.to_string())
143-
// .unwrap_or("unknown".into()),
144-
// );
145-
146133
let destination_item = slot_query
147134
.get(destination_slot)
148135
.map(|slotted_item| slotted_item.0);
@@ -182,6 +169,7 @@ fn setup(
182169
mut commands: Commands,
183170
item_assets: Res<ItemAssets>,
184171
mut item_defs: ResMut<Assets<ItemDef>>,
172+
asset_server: Res<AssetServer>,
185173
) {
186174
let items = item_defs
187175
.iter()
@@ -233,21 +221,33 @@ fn setup(
233221
.id();
234222

235223
if let Some((asset_id, item_def)) = items_iter.next() {
236-
commands.spawn((
237-
Name::new(item_def.name.clone()),
238-
InSlot(slot_id),
239-
Item(item_defs.get_strong_handle(*asset_id).unwrap()),
240-
Node {
241-
width: Val::Percent(80.0),
242-
height: Val::Percent(80.0),
224+
let id = commands
225+
.spawn((
226+
Name::new(item_def.name.clone()),
227+
InSlot(slot_id),
228+
Item(item_defs.get_strong_handle(*asset_id).unwrap()),
229+
Node {
230+
width: Val::Percent(80.0),
231+
height: Val::Percent(80.0),
232+
..default()
233+
},
234+
))
235+
.id();
236+
237+
if let Some(sprite_path) = &item_def.sprite {
238+
commands.entity(id).insert(ImageNode {
239+
image: asset_server.load(sprite_path.clone()),
243240
..default()
244-
},
245-
ImageNode::default(),
246-
AseSlice {
247-
aseprite: item_assets.aseprite.clone(),
248-
name: item_def.id.clone(),
249-
},
250-
));
241+
});
242+
} else {
243+
commands.entity(id).insert((
244+
ImageNode::default(),
245+
AseSlice {
246+
aseprite: item_assets.aseprite.clone(),
247+
name: item_def.id.clone(),
248+
},
249+
));
250+
}
251251
}
252252
}
253253
}

src/simulation/item/assets.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub fn plugin(app: &mut App) {
2323
pub struct ItemDef {
2424
pub id: String,
2525
pub name: String,
26+
pub sprite: Option<String>,
2627
pub stack_size: u32,
2728
}
2829

@@ -37,6 +38,7 @@ impl Indexable for ItemDef {
3738
pub struct ItemAssets {
3839
pub aseprite: Handle<Aseprite>,
3940
pub item_definitions: Handle<LoadedFolder>,
41+
pub item_sprites: Handle<LoadedFolder>,
4042
}
4143

4244
impl FromWorld for ItemAssets {
@@ -46,6 +48,7 @@ impl FromWorld for ItemAssets {
4648
Self {
4749
aseprite: asset_server.load("items.aseprite"),
4850
item_definitions: asset_server.load_folder("items"),
51+
item_sprites: asset_server.load_folder("sprites/items"),
4952
}
5053
}
5154
}

src/simulation/item/compendium.rs

Lines changed: 59 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ fn spawn_item_compendium(
3737
mut commands: Commands,
3838
items: Res<Assets<ItemDef>>,
3939
item_assets: Res<ItemAssets>,
40+
asset_server: Res<AssetServer>,
4041
) {
4142
let mut items_sorted: Vec<_> = items.iter().map(|(_, a)| a).collect();
4243
items_sorted.sort_by(|a, b| a.name.cmp(&b.name));
@@ -71,44 +72,65 @@ fn spawn_item_compendium(
7172
.id();
7273

7374
for item_def in items_sorted.iter() {
74-
commands.spawn((
75-
Node {
76-
display: Display::Grid,
77-
grid_template_columns: vec![
78-
RepeatedGridTrack::px(1, 64.0),
79-
RepeatedGridTrack::px(1, 256.0),
80-
],
81-
grid_template_rows: vec![RepeatedGridTrack::px(2, 32.0)],
82-
justify_items: JustifyItems::Start,
83-
align_items: AlignItems::Center,
84-
column_gap: Val::Px(16.0),
85-
padding: UiRect::all(Val::Px(4.0)),
75+
let item_box = commands
76+
.spawn((
77+
Node {
78+
display: Display::Grid,
79+
grid_template_columns: vec![
80+
RepeatedGridTrack::px(1, 64.0),
81+
RepeatedGridTrack::px(1, 256.0),
82+
],
83+
grid_template_rows: vec![RepeatedGridTrack::px(2, 32.0)],
84+
justify_items: JustifyItems::Start,
85+
align_items: AlignItems::Center,
86+
column_gap: Val::Px(16.0),
87+
padding: UiRect::all(Val::Px(4.0)),
88+
..default()
89+
},
90+
ChildOf(item_list),
91+
BackgroundColor(Color::BLACK),
92+
))
93+
.id();
94+
95+
let image = commands
96+
.spawn((
97+
Node {
98+
justify_self: JustifySelf::Center,
99+
grid_column: GridPlacement::span(1),
100+
grid_row: GridPlacement::span(2),
101+
width: Val::Px(56.0),
102+
height: Val::Px(56.0),
103+
..default()
104+
},
105+
ChildOf(item_box),
106+
))
107+
.id();
108+
109+
if let Some(sprite) = &item_def.sprite {
110+
commands.entity(image).insert(ImageNode {
111+
image: asset_server.load(sprite.clone()),
86112
..default()
87-
},
88-
ChildOf(item_list),
89-
BackgroundColor(Color::BLACK),
90-
children![
91-
(
92-
Node {
93-
justify_self: JustifySelf::Center,
94-
grid_column: GridPlacement::span(1),
95-
grid_row: GridPlacement::span(2),
96-
width: Val::Px(56.0),
97-
height: Val::Px(56.0),
98-
..default()
99-
},
100-
ImageNode::default(),
101-
AseSlice {
102-
aseprite: item_assets.aseprite.clone(),
103-
name: item_def.id.to_owned(),
104-
}
105-
),
106-
(Text::new(item_def.name.to_owned()), TextColor(Color::WHITE),),
107-
(
108-
Text::new(item_def.stack_size.to_string()),
109-
TextColor(Color::WHITE),
110-
)
111-
],
113+
});
114+
} else {
115+
commands.entity(image).insert((
116+
ImageNode::default(),
117+
AseSlice {
118+
aseprite: item_assets.aseprite.clone(),
119+
name: item_def.id.to_owned(),
120+
},
121+
));
122+
}
123+
124+
commands.spawn((
125+
Text::new(item_def.name.to_owned()),
126+
TextColor(Color::WHITE),
127+
ChildOf(item_box),
128+
));
129+
130+
commands.spawn((
131+
Text::new(item_def.stack_size.to_string()),
132+
TextColor(Color::WHITE),
133+
ChildOf(item_box),
112134
));
113135
}
114136
}

src/ui/inspect/info.rs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub fn open_recipe_menu(
4040
recipe_index: Res<IndexMap<RecipeDef>>,
4141
held_relics: Query<&HeldRelic>,
4242
item_defs: Res<Assets<ItemDef>>,
43+
asset_server: Res<AssetServer>,
4344
) {
4445
let Ok(selected_recipe) = selected_recipes.get(inspected_entity.0) else {
4546
return;
@@ -188,22 +189,34 @@ pub fn open_recipe_menu(
188189
.get(&relic.0)
189190
.expect("Item referred to by HeldRelic component is not loaded");
190191

191-
commands.spawn((
192-
ChildOf(relic_slot_id),
193-
Name::new(item_def.name.clone()),
194-
InSlot(relic_slot_id),
195-
Item(relic.0.clone()),
196-
Node {
197-
width: Val::Percent(80.0),
198-
height: Val::Percent(80.0),
192+
let relic_image = commands
193+
.spawn((
194+
ChildOf(relic_slot_id),
195+
Name::new(item_def.name.clone()),
196+
InSlot(relic_slot_id),
197+
Item(relic.0.clone()),
198+
Node {
199+
width: Val::Percent(80.0),
200+
height: Val::Percent(80.0),
201+
..default()
202+
},
203+
))
204+
.id();
205+
206+
if let Some(item_sprite) = &item_def.sprite {
207+
commands.entity(relic_image).insert(ImageNode {
208+
image: asset_server.load(item_sprite.clone()),
199209
..default()
200-
},
201-
ImageNode::default(),
202-
AseSlice {
203-
aseprite: item_assets.aseprite.clone(),
204-
name: item_def.id.clone(),
205-
},
206-
));
210+
});
211+
} else {
212+
commands.entity(relic_image).insert((
213+
ImageNode::default(),
214+
AseSlice {
215+
aseprite: item_assets.aseprite.clone(),
216+
name: item_def.id.clone(),
217+
},
218+
));
219+
}
207220
}
208221

209222
commands

0 commit comments

Comments
 (0)