game_features/item_transition.rs
1use crate::*;
2
3use std::collections::HashMap;
4use std::hash::Hash;
5
6// crafting
7/// A transition from one or more items into one or more different items.
8/// Can be used for all sorts of crafting.
9#[derive(new, Clone, Serialize, Deserialize, Debug, Builder)]
10pub struct ItemTransitionDefinition<K, I, E, S> {
11 /// The id of this item transition.
12 pub key: K,
13 /// The name of the transition.
14 pub name: String,
15 /// The friendly name of the transition.
16 pub friendly_name: String,
17 /// The icon path.
18 pub icon_path: Option<String>,
19 /// The different input items, quantities and `UseMode` for each one.
20 pub input_items: Vec<(I, usize, UseMode)>,
21 /// The required stats conditions required to process the transition.
22 pub stat_conditions: Vec<StatCondition<S>>,
23 /// The effectors applied during crafting.
24 pub stat_effectors: Vec<E>,
25 /// The different output items.
26 pub output_items: Vec<(I, usize)>,
27 /// What happens when you lose the condition required to continue the transition.
28 pub on_condition_lost: ConditionLostReaction,
29 /// The time to complete the transition.
30 pub time_to_complete: f64,
31 /// Consume the input items at the start of the transition, regardless of the result.
32 pub consume_input_immediate: bool,
33 /// Automatically transition when all the required conditions are met.
34 pub auto_trigger: bool,
35}
36
37/// The way items are used in a transition.
38#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
39pub enum UseMode {
40 /// Consumes the item. The item will be lost.
41 Consume,
42 /// Uses a set amount of durability from the item.
43 UseOnce {
44 /// The amount of durability used.
45 durability: f64,
46 },
47 /// Uses a set amount of durability from the item each second.
48 UsePerSecond {
49 /// The durability usage per second.
50 rate: f64,
51 },
52}
53
54/// What happens when the transition is stopped or conditions aren't met anymore for it to
55/// continue.
56#[derive(Clone, Serialize, Deserialize, Debug)]
57pub enum ConditionLostReaction {
58 /// Nothing happens, the transition continues.
59 None,
60 /// The transition pauses and keeps its progress.
61 Pause,
62 /// The transition is cancelled and all progress is lost.
63 /// If consume_input_immediate was true, the input items are not returned.
64 Cancel,
65}
66
67/// A transition in progress.
68#[derive(new, Clone, Serialize, Deserialize, Debug)]
69pub struct ItemTransitionBatch<K> {
70 /// The transition id.
71 transition: K,
72 /// The number of transitions that are queued.
73 remaining: u32,
74 /// The time until the current transition is completed.
75 next_completion_remaining: f64,
76}
77
78/// The definitions of all known stats.
79#[derive(Debug, Clone, Serialize, Deserialize, new)]
80pub struct ItemTransitionDefinitions<K: Hash + Eq, I, E, S> {
81 /// The definitions.
82 pub defs: HashMap<K, ItemTransitionDefinition<K, I, E, S>>,
83}
84
85impl<K: Hash + Eq, I, E, S> Default for ItemTransitionDefinitions<K, I, E, S> {
86 fn default() -> Self {
87 Self {
88 defs: HashMap::default(),
89 }
90 }
91}
92
93impl<K: Hash + Eq + Clone, I, E, S> From<Vec<ItemTransitionDefinition<K, I, E, S>>>
94 for ItemTransitionDefinitions<K, I, E, S>
95{
96 fn from(t: Vec<ItemTransitionDefinition<K, I, E, S>>) -> Self {
97 let defs = t
98 .into_iter()
99 .map(|s| (s.key.clone(), s))
100 .collect::<HashMap<_, _>>();
101 Self::new(defs)
102 }
103}