1#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
5pub enum InvItem {
6 DoctorBag = 0,
7 DivingSuit = 1,
8 CrystalBall = 2,
9 BeastVision = 3,
10 JumpBoots = 4,
11}
12
13pub const INV_SLOTS: usize = 5;
14pub const INV_NAMES: [&str; INV_SLOTS] = [
15 "Doctor's Bag",
16 "Diving Suit",
17 "Crystal Ball",
18 "Beast Vision",
19 "Jumping Boots",
20];
21pub const INV_ITEM_TYPES: [i16; INV_SLOTS] = [107, 118, 121, 125, 115];
23pub const INV_MAX: [f64; INV_SLOTS] = [100.0, 60.0, 1.0, 60.0, 60.0];
25
26#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Default)]
27pub struct Inventory {
28 pub charge: [f64; INV_SLOTS],
29 pub active: [bool; INV_SLOTS],
30 pub selected: usize,
31}
32
33impl Inventory {
34 pub fn has(&self, i: InvItem) -> bool {
35 self.charge[i as usize] > 0.0
36 }
37
38 pub fn is_active(&self, i: InvItem) -> bool {
39 self.active[i as usize] && self.has(i)
40 }
41
42 pub fn give(&mut self, i: InvItem) -> bool {
44 let k = i as usize;
45 if self.charge[k] >= INV_MAX[k] {
46 return false;
47 }
48 self.charge[k] = INV_MAX[k];
49 if !self.has(self.slot(self.selected)) {
50 self.selected = k;
51 }
52 true
53 }
54
55 fn slot(&self, k: usize) -> InvItem {
56 [
57 InvItem::DoctorBag,
58 InvItem::DivingSuit,
59 InvItem::CrystalBall,
60 InvItem::BeastVision,
61 InvItem::JumpBoots,
62 ][k % INV_SLOTS]
63 }
64
65 pub fn cycle(&mut self, dir: i32) {
67 for step in 1..=INV_SLOTS as i32 {
68 let k = (self.selected as i32 + dir * step).rem_euclid(INV_SLOTS as i32) as usize;
69 if self.charge[k] > 0.0 {
70 self.selected = k;
71 return;
72 }
73 }
74 }
75
76 pub fn use_selected(&mut self, health: &mut i32) -> Option<InvItem> {
79 let k = self.selected;
80 if self.charge[k] <= 0.0 {
81 return None;
82 }
83 let item = self.slot(k);
84 match item {
85 InvItem::DoctorBag => {
86 if *health >= 100 {
87 return None;
88 }
89 let need = (100 - *health) as f64;
90 let heal = need.min(self.charge[k]);
91 *health += heal as i32;
92 self.charge[k] -= heal;
93 }
94 InvItem::CrystalBall => {}
95 _ => self.active[k] = !self.active[k],
96 }
97 Some(item)
98 }
99
100 pub fn tick(&mut self, dt: f64) {
102 for k in 1..INV_SLOTS {
103 if self.active[k] && k != InvItem::CrystalBall as usize {
104 self.charge[k] = (self.charge[k] - dt).max(0.0);
105 if self.charge[k] <= 0.0 {
106 self.active[k] = false;
107 }
108 }
109 }
110 }
111}
112
113#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Default)]
115pub struct Powerups {
116 pub invisible: f64,
117 pub invulnerable: f64,
118 pub akimbo: f64,
119 pub reflect: f64,
120 pub feather_fall: f64,
121 pub rage: f64,
122}
123
124impl Powerups {
125 pub fn tick(&mut self, dt: f64) {
126 for t in [
127 &mut self.invisible,
128 &mut self.invulnerable,
129 &mut self.akimbo,
130 &mut self.reflect,
131 &mut self.feather_fall,
132 &mut self.rage,
133 ] {
134 *t = (*t - dt).max(0.0);
135 }
136 }
137
138 pub fn damage_multiplier(&self) -> i32 {
139 if self.rage > 0.0 {
140 2
141 } else {
142 1
143 }
144 }
145
146 pub fn list(&self) -> Vec<(&'static str, f64)> {
148 [
149 ("Invisible", self.invisible),
150 ("Invulnerable", self.invulnerable),
151 ("Guns Akimbo", self.akimbo),
152 ("Reflective Shots", self.reflect),
153 ("Feather Fall", self.feather_fall),
154 ("Rage", self.rage),
155 ]
156 .into_iter()
157 .filter(|(_, t)| *t > 0.0)
158 .collect()
159 }
160}
161
162#[cfg(test)]
163mod tests {
164 use super::*;
165
166 #[test]
167 fn doctor_bag_heals_partially() {
168 let mut inv = Inventory::default();
169 assert!(inv.give(InvItem::DoctorBag));
170 let mut hp = 30;
171 assert_eq!(inv.use_selected(&mut hp), Some(InvItem::DoctorBag));
172 assert_eq!(hp, 100);
173 assert!((inv.charge[0] - 30.0).abs() < 1e-9);
174 let mut hp = 100;
175 assert_eq!(inv.use_selected(&mut hp), None, "no use at full health");
176 }
177
178 #[test]
179 fn timed_items_drain_and_cycle() {
180 let mut inv = Inventory::default();
181 inv.give(InvItem::JumpBoots);
182 inv.give(InvItem::DivingSuit);
183 inv.selected = 4;
184 inv.cycle(1);
185 assert_eq!(inv.selected, 1);
186 let mut hp = 100;
187 inv.use_selected(&mut hp);
188 assert!(inv.is_active(InvItem::DivingSuit));
189 inv.tick(61.0);
190 assert!(!inv.is_active(InvItem::DivingSuit) && !inv.has(InvItem::DivingSuit));
191 }
192
193 #[test]
194 fn powerups_expire() {
195 let mut p = Powerups {
196 rage: 1.0,
197 ..Default::default()
198 };
199 assert_eq!(p.damage_multiplier(), 2);
200 p.tick(2.0);
201 assert_eq!(p.damage_multiplier(), 1);
202 assert!(p.list().is_empty());
203 }
204}