Skip to content

Commit 8541ace

Browse files
committed
made equipment slots struct
1 parent da4a708 commit 8541ace

File tree

5 files changed

+190
-37
lines changed

5 files changed

+190
-37
lines changed

pets-gd/assets/charmap.json

+150-15
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,16 @@
2424
"lambda": null,
2525
"max_mana": null
2626
},
27-
"equipment": []
27+
"equipment": {
28+
"head": null,
29+
"body": null,
30+
"weapon": null,
31+
"accessories": [
32+
null,
33+
null,
34+
null
35+
]
36+
}
2837
}
2938
},
3039
{
@@ -52,7 +61,16 @@
5261
"lambda": null,
5362
"max_mana": null
5463
},
55-
"equipment": []
64+
"equipment": {
65+
"head": null,
66+
"body": null,
67+
"weapon": null,
68+
"accessories": [
69+
null,
70+
null,
71+
null
72+
]
73+
}
5674
}
5775
},
5876
{
@@ -80,7 +98,16 @@
8098
"lambda": null,
8199
"max_mana": null
82100
},
83-
"equipment": []
101+
"equipment": {
102+
"head": null,
103+
"body": null,
104+
"weapon": "cool_stick",
105+
"accessories": [
106+
null,
107+
null,
108+
null
109+
]
110+
}
84111
}
85112
},
86113
{
@@ -108,7 +135,16 @@
108135
"lambda": null,
109136
"max_mana": null
110137
},
111-
"equipment": []
138+
"equipment": {
139+
"head": null,
140+
"body": null,
141+
"weapon": null,
142+
"accessories": [
143+
null,
144+
null,
145+
null
146+
]
147+
}
112148
}
113149
},
114150
{
@@ -136,7 +172,16 @@
136172
"lambda": null,
137173
"max_mana": null
138174
},
139-
"equipment": []
175+
"equipment": {
176+
"head": null,
177+
"body": null,
178+
"weapon": null,
179+
"accessories": [
180+
null,
181+
null,
182+
null
183+
]
184+
}
140185
}
141186
},
142187
{
@@ -164,7 +209,16 @@
164209
"lambda": null,
165210
"max_mana": null
166211
},
167-
"equipment": []
212+
"equipment": {
213+
"head": null,
214+
"body": null,
215+
"weapon": null,
216+
"accessories": [
217+
null,
218+
null,
219+
null
220+
]
221+
}
168222
}
169223
},
170224
{
@@ -192,7 +246,16 @@
192246
"lambda": null,
193247
"max_mana": null
194248
},
195-
"equipment": []
249+
"equipment": {
250+
"head": null,
251+
"body": null,
252+
"weapon": null,
253+
"accessories": [
254+
null,
255+
null,
256+
null
257+
]
258+
}
196259
}
197260
},
198261
{
@@ -220,7 +283,16 @@
220283
"lambda": null,
221284
"max_mana": null
222285
},
223-
"equipment": []
286+
"equipment": {
287+
"head": null,
288+
"body": null,
289+
"weapon": null,
290+
"accessories": [
291+
null,
292+
null,
293+
null
294+
]
295+
}
224296
}
225297
},
226298
{
@@ -248,7 +320,16 @@
248320
"lambda": null,
249321
"max_mana": null
250322
},
251-
"equipment": []
323+
"equipment": {
324+
"head": null,
325+
"body": null,
326+
"weapon": null,
327+
"accessories": [
328+
null,
329+
null,
330+
null
331+
]
332+
}
252333
}
253334
},
254335
{
@@ -276,7 +357,16 @@
276357
"lambda": null,
277358
"max_mana": null
278359
},
279-
"equipment": []
360+
"equipment": {
361+
"head": null,
362+
"body": null,
363+
"weapon": null,
364+
"accessories": [
365+
null,
366+
null,
367+
null
368+
]
369+
}
280370
}
281371
},
282372
{
@@ -304,7 +394,16 @@
304394
"lambda": null,
305395
"max_mana": null
306396
},
307-
"equipment": []
397+
"equipment": {
398+
"head": null,
399+
"body": null,
400+
"weapon": null,
401+
"accessories": [
402+
null,
403+
null,
404+
null
405+
]
406+
}
308407
}
309408
},
310409
{
@@ -332,7 +431,16 @@
332431
"lambda": null,
333432
"max_mana": null
334433
},
335-
"equipment": []
434+
"equipment": {
435+
"head": null,
436+
"body": null,
437+
"weapon": null,
438+
"accessories": [
439+
null,
440+
null,
441+
null
442+
]
443+
}
336444
}
337445
},
338446
{
@@ -360,7 +468,16 @@
360468
"lambda": null,
361469
"max_mana": null
362470
},
363-
"equipment": []
471+
"equipment": {
472+
"head": null,
473+
"body": null,
474+
"weapon": null,
475+
"accessories": [
476+
null,
477+
null,
478+
null
479+
]
480+
}
364481
}
365482
},
366483
{
@@ -388,7 +505,16 @@
388505
"lambda": null,
389506
"max_mana": null
390507
},
391-
"equipment": []
508+
"equipment": {
509+
"head": null,
510+
"body": null,
511+
"weapon": null,
512+
"accessories": [
513+
null,
514+
null,
515+
null
516+
]
517+
}
392518
}
393519
},
394520
{
@@ -416,7 +542,16 @@
416542
"lambda": null,
417543
"max_mana": null
418544
},
419-
"equipment": []
545+
"equipment": {
546+
"head": null,
547+
"body": null,
548+
"weapon": null,
549+
"accessories": [
550+
null,
551+
null,
552+
null
553+
]
554+
}
420555
}
421556
}
422557
]

pets-lib/src/consts.rs

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ pub mod battle {
2525
/// are the ones that you get to use in battle.
2626
pub const BATTLE_PARTY_SIZE: usize = 4;
2727

28+
/// Number of accessory slots per character
29+
pub const ACCESSORY_SLOTS: usize = 3;
30+
2831
pub const INTRO_FADE_PREDELAY: f64 = 0.5;
2932
pub const INTRO_COUNTDOWN_SEC: f64 = 3.0;
3033

pets-lib/src/items/inv.rs

+25-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
11
use super::*;
2+
use crate::consts::battle::ACCESSORY_SLOTS;
23

3-
pub trait ItemList {
4-
/// Every item that can be equipped
5-
fn offsets(&self) -> impl Iterator<Item = &InherentStats>;
4+
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
5+
pub struct Equipment {
6+
head: Option<String>,
7+
body: Option<String>,
8+
weapon: Option<String>,
9+
accessories: [Option<String>; ACCESSORY_SLOTS],
610
}
711

8-
impl ItemList for Vec<Item> {
9-
fn offsets(&self) -> impl Iterator<Item = &InherentStats> {
10-
use ItemCat::*;
12+
impl Equipment {
13+
pub fn iter(&self) -> impl Iterator<Item = &str> {
14+
self.head
15+
.iter()
16+
.chain(self.body.iter())
17+
.chain(self.weapon.iter())
18+
.chain(self.accessories.iter().filter_map(|s| s.as_ref()))
19+
.map(|s| s.as_str())
20+
}
21+
22+
pub fn offsets(&self) -> InherentStats {
23+
self.iter().fold(InherentStats::default(), |acc, item| {
24+
let ItemCat::Equipment { ref offsets, .. } =
25+
Item::from_registry(item).category
26+
else {
27+
panic!("item {} not equippable", item)
28+
};
1129

12-
self.iter().filter_map(|i| match &i.category {
13-
Equipment { offsets, .. } => Some(offsets),
14-
_ => None,
30+
acc + offsets.clone()
1531
})
1632
}
1733
}

pets-lib/src/items/mod.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::util::registry::*;
99
use std::sync::OnceLock;
1010

1111
mod inv;
12-
pub use inv::{Inventory, ItemList};
12+
pub use inv::{Equipment, Inventory};
1313

1414
pub static ITEM_REGISTRY: OnceLock<HashMap<String, Item>> = OnceLock::new();
1515

@@ -78,8 +78,12 @@ pub enum AmmoCat {
7878
}
7979

8080
impl Item {
81-
pub fn is_equipment(&self) -> bool {
82-
matches!(self.category, ItemCat::Equipment { .. })
81+
pub fn from_registry(id: &str) -> &Item {
82+
unwrap_fmt!(
83+
ITEM_REGISTRY.get().unwrap().get(id),
84+
"Item ID not found: {}",
85+
id
86+
)
8387
}
8488
}
8589

pets-lib/src/stats/battler.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ pub struct Battler {
77
pub buffs_list: Vec<InherentStats>,
88

99
pub inherent_stats: InherentStats,
10-
pub equipment: Vec<Item>,
10+
11+
/// The IDs of all equipped items
12+
pub equipment: Equipment,
1113
}
1214

1315
impl Default for Battler {
@@ -18,7 +20,7 @@ impl Default for Battler {
1820
buffs_list: Vec::new(),
1921

2022
inherent_stats: InherentStats::default(),
21-
equipment: Vec::new(),
23+
equipment: Equipment::default(),
2224
}
2325
}
2426
}
@@ -45,14 +47,7 @@ impl Battler {
4547
/// This should take armor, weapons, etc. into account for players.
4648
/// It should NOT consider in-battle buffs/debuffs.
4749
fn armored_stats(&self) -> InherentStats {
48-
let inherent = self.inherent_stats.clone();
49-
50-
// get all offsets from each item that has one
51-
let equips = &self.equipment;
52-
let offsets = equips.offsets();
53-
54-
// ... and sum them up
55-
inherent + offsets.cloned().sum()
50+
self.inherent_stats.clone() + self.equipment.offsets()
5651
}
5752

5853
/// The final "in practice" stats of the character.

0 commit comments

Comments
 (0)