dotzuki_engine/party/
party.rs1use super::{MonsterInstance, MonsterProvider};
7
8#[derive(Debug, PartialEq, Eq)]
12pub struct PartyFull<P: MonsterProvider>(pub MonsterInstance<P>);
13
14#[derive(Clone, Debug, PartialEq, Eq)]
16pub struct Party<P: MonsterProvider> {
17 members: Vec<MonsterInstance<P>>,
18 capacity: usize,
19}
20
21impl<P: MonsterProvider> Party<P> {
22 pub fn new(capacity: usize) -> Self {
25 Self {
26 members: Vec::new(),
27 capacity,
28 }
29 }
30
31 pub fn capacity(&self) -> usize {
33 self.capacity
34 }
35
36 pub fn len(&self) -> usize {
38 self.members.len()
39 }
40
41 pub fn is_empty(&self) -> bool {
43 self.members.is_empty()
44 }
45
46 pub fn is_full(&self) -> bool {
48 self.members.len() >= self.capacity
49 }
50
51 pub fn add(&mut self, m: MonsterInstance<P>) -> Result<(), PartyFull<P>> {
54 if self.is_full() {
55 return Err(PartyFull(m));
56 }
57 self.members.push(m);
58 Ok(())
59 }
60
61 pub fn remove(&mut self, index: usize) -> Option<MonsterInstance<P>> {
63 if index < self.members.len() {
64 Some(self.members.remove(index))
65 } else {
66 None
67 }
68 }
69
70 pub fn swap(&mut self, a: usize, b: usize) {
73 if a < self.members.len() && b < self.members.len() {
74 self.members.swap(a, b);
75 }
76 }
77
78 pub fn get(&self, index: usize) -> Option<&MonsterInstance<P>> {
80 self.members.get(index)
81 }
82
83 pub fn get_mut(&mut self, index: usize) -> Option<&mut MonsterInstance<P>> {
85 self.members.get_mut(index)
86 }
87
88 pub fn first_able(&self) -> Option<usize> {
90 self.members.iter().position(|m| !m.is_fainted())
91 }
92
93 pub fn all_fainted(&self) -> bool {
96 !self.members.is_empty() && self.members.iter().all(|m| m.is_fainted())
97 }
98
99 pub fn iter(&self) -> impl Iterator<Item = &MonsterInstance<P>> {
101 self.members.iter()
102 }
103
104 pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut MonsterInstance<P>> {
106 self.members.iter_mut()
107 }
108}
109
110#[derive(Clone, Debug, PartialEq, Eq)]
113pub struct StorageBox<P: MonsterProvider> {
114 members: Vec<MonsterInstance<P>>,
115 capacity: usize,
116}
117
118impl<P: MonsterProvider> StorageBox<P> {
119 pub fn new(capacity: usize) -> Self {
121 Self {
122 members: Vec::new(),
123 capacity,
124 }
125 }
126
127 pub fn capacity(&self) -> usize {
129 self.capacity
130 }
131
132 pub fn len(&self) -> usize {
134 self.members.len()
135 }
136
137 pub fn is_empty(&self) -> bool {
139 self.members.is_empty()
140 }
141
142 pub fn is_full(&self) -> bool {
144 self.members.len() >= self.capacity
145 }
146
147 pub fn add(&mut self, m: MonsterInstance<P>) -> Result<(), PartyFull<P>> {
149 if self.is_full() {
150 return Err(PartyFull(m));
151 }
152 self.members.push(m);
153 Ok(())
154 }
155
156 pub fn remove(&mut self, index: usize) -> Option<MonsterInstance<P>> {
158 if index < self.members.len() {
159 Some(self.members.remove(index))
160 } else {
161 None
162 }
163 }
164
165 pub fn get(&self, index: usize) -> Option<&MonsterInstance<P>> {
167 self.members.get(index)
168 }
169
170 pub fn get_mut(&mut self, index: usize) -> Option<&mut MonsterInstance<P>> {
172 self.members.get_mut(index)
173 }
174
175 pub fn iter(&self) -> impl Iterator<Item = &MonsterInstance<P>> {
177 self.members.iter()
178 }
179}
180
181#[derive(Clone, Debug, PartialEq, Eq)]
184pub struct BoxStore<P: MonsterProvider> {
185 boxes: Vec<StorageBox<P>>,
186 current: usize,
187}
188
189impl<P: MonsterProvider> BoxStore<P> {
190 pub fn new(box_count: usize, box_capacity: usize) -> Self {
192 Self {
193 boxes: (0..box_count)
194 .map(|_| StorageBox::new(box_capacity))
195 .collect(),
196 current: 0,
197 }
198 }
199
200 pub fn box_count(&self) -> usize {
202 self.boxes.len()
203 }
204
205 pub fn current_index(&self) -> usize {
207 self.current
208 }
209
210 pub fn current(&self) -> &StorageBox<P> {
212 &self.boxes[self.current]
213 }
214
215 pub fn current_mut(&mut self) -> &mut StorageBox<P> {
217 &mut self.boxes[self.current]
218 }
219
220 pub fn get(&self, index: usize) -> Option<&StorageBox<P>> {
222 self.boxes.get(index)
223 }
224
225 pub fn get_mut(&mut self, index: usize) -> Option<&mut StorageBox<P>> {
227 self.boxes.get_mut(index)
228 }
229
230 pub fn switch(&mut self, index: usize) {
232 if index < self.boxes.len() {
233 self.current = index;
234 }
235 }
236}