1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use HashMap;
use Debug;
use Hash;
/// An `ItemDefinition` stores the different properties of a type of item.
/// It is a schema that contains the data which isn't changing between different item instances.
///
/// Generic Types:
/// * K: Type of the key. Usually an enum or a number (ie u32).
/// * T: Type of the different item types. For example, an armor-type item can not be placed into a
/// weapon-type inventory slot. If you don't need to make the distinction between different item
/// types, use the `()` type.
/// * D: The type of the custom user data. If you don't have any, use the `()` type. It can (and
/// probably should) be different than the custom user data used on `ItemInstance`s
/// An `ItemInstance` is a stack of item.
/// It refers to the `ItemDefinition`'s key. The associated `ItemDefinition` contains the data
/// describing that object type.
/// An item stack can be composed of one or many of the same item.
/// It can also have a durability, which decreases when the item is used using
/// `Inventory::use_item`.
///
/// Custom data can be added into the user_data field. This data is specific to each item instance.
///
/// Generic Types:
/// * K: Type of the key. Usually an enum or a number (ie u32).
/// * U: The type of the custom user data. If you don't have any, use the `()` type.
/// It can (and probably should) be different than the custom user data used on `ItemInstance`s
/// A simple repository mapping the key K to the corresponding `ItemDefinition`.
/// A trait defining which items can be inserted into each inventory slot type.
// TODO extra stuff
/*pub struct SingleEquippedItem<K> {
pub equipped_index: usize,
_phantom: PhantomData<K>,
}
impl<K: PartialEq> SingleEquippedItem<K> {
pub fn get_equipped(&self, inventory: &Inventory<K, D, S>) -> Option<&ItemInstance<K, U>> {
}
}
pub struct BaseRecipeDefinition<K: PartialEq> {
pub inputs: Vec<ItemInstance<K, U>>,
pub outputs: Vec<ItemInstance<K, U>>,
}
trait Recipe<K> {
fn craft(&mut self, inputs: Vec<ItemInstance<K, U>>) -> Vec<ItemInstance<K, U>>;
}*/
/*#[cfg(test)]
mod test {
use crate::*;
#[derive(new, Debug, Clone, Serialize, Deserialize, Default)]
struct CustomItemDefinitionData {
pub weight: f32,
}
#[derive(new, Debug, Clone, Serialize, Deserialize, Default)]
struct CustomItemInstanceData {
pub xp: f32,
}
#[derive(new, Debug, Clone, Serialize, Deserialize, PartialEq)]
enum ItemType {
}
#[derive(new, Debug, Clone, Serialize, Deserialize, PartialEq)]
enum ItemType {
Weapon,
Armor,
Other,
Consumable,
// slot types
Regular,
Equipment,
}
// Work around for broken derive_builder.
impl Default for ItemType {
fn default() -> Self {
ItemType::Other
}
}
impl SlotType for ItemType {
// slot type -> item type
fn can_insert_into(&self, other: &ItemType) -> bool {
match *self {
ItemType::Regular => true,
ItemType::Equipment => {
*other == ItemType::Weapon || *other == ItemType::Armor
}
}
}
}
#[test]
fn inventory_insert_empty_fixed() {
let ii = ItemInstance::<u32, ()>::new(1u32, 1);
let mut inv = Inventory::<u32, (), (), ()>::new_fixed(8);
inv.insert(ii).expect("");
}
#[test]
fn complex_items() {
// Weight, enchants and Effects
// TODO: Effectors
let item_def = ItemDefinitionBuilder::default()
.key(1u32)
.item_type(ItemType::Consumable)
.name("Apple".to_string())
.friendly_name("apple".to_string())
.description("Food is nice".to_string())
.maximum_stack(16)
.maximum_durability(None)
.user_data(CustomItemDefinitionData::new(1.0))
.build()
.unwrap();
let ii = ItemInstanceBuilder::default()
.key(1u32)
.quantity(4)
.durability(Some(64))
.user_data(CustomItemInstanceData::new(0.0))
.build()
.unwrap();
let mut inv =
Inventory::<u32, ItemType, CustomItemInstanceData>::new_fixed(8);
let mut inv2 =
Inventory::<u32, ItemType, CustomItemInstanceData>::new_fixed(8);
inv.insert(ii.clone()).expect("");
inv2.insert(ii).expect("");
inv.transfer(0, &mut inv2, 1, 2, false).expect("");
}
}*/