This commit is contained in:
@@ -1,12 +1,16 @@
|
||||
use crate::dynamic_data::models::pokemon::Pokemon;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::dynamic_data::models::pokemon::Pokemon;
|
||||
|
||||
/// A list of Pokemon belonging to a trainer.
|
||||
#[derive(Debug)]
|
||||
pub struct PokemonParty<'pokemon, 'library> {
|
||||
/// The underlying list of Pokemon.
|
||||
pokemon: Vec<Option<Arc<Pokemon<'pokemon, 'library>>>>,
|
||||
}
|
||||
|
||||
impl<'own, 'library> PokemonParty<'own, 'library> {
|
||||
/// Instantiates a party with a set size.
|
||||
pub fn new(size: usize) -> Self {
|
||||
let mut pokemon = Vec::with_capacity(size);
|
||||
for _i in 0..size {
|
||||
@@ -15,10 +19,12 @@ impl<'own, 'library> PokemonParty<'own, 'library> {
|
||||
Self { pokemon }
|
||||
}
|
||||
|
||||
/// Instantiates a party with a list.
|
||||
pub fn new_from_vec(pokemon: Vec<Option<Arc<Pokemon<'own, 'library>>>>) -> Self {
|
||||
Self { pokemon }
|
||||
}
|
||||
|
||||
/// Gets a Pokemon at an index in the party.
|
||||
pub fn at(&self, index: usize) -> &Option<Arc<Pokemon<'own, 'library>>> {
|
||||
let opt = self.pokemon.get(index);
|
||||
if let Some(v) = opt {
|
||||
@@ -28,10 +34,12 @@ impl<'own, 'library> PokemonParty<'own, 'library> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Swaps two Pokemon in the party around.
|
||||
pub fn switch(&mut self, a: usize, b: usize) {
|
||||
self.pokemon.swap(a, b);
|
||||
}
|
||||
|
||||
/// Sets the Pokemon at an index to a Pokemon, returning the old Pokemon.
|
||||
pub fn swap_into(
|
||||
&mut self,
|
||||
index: usize,
|
||||
@@ -45,6 +53,7 @@ impl<'own, 'library> PokemonParty<'own, 'library> {
|
||||
old
|
||||
}
|
||||
|
||||
/// Whether or not the party still has Pokemon that can be used in battle.
|
||||
pub fn has_usable_pokemon(&self) -> bool {
|
||||
for pokemon in self.pokemon.iter().flatten() {
|
||||
if pokemon.is_usable() {
|
||||
@@ -54,14 +63,17 @@ impl<'own, 'library> PokemonParty<'own, 'library> {
|
||||
false
|
||||
}
|
||||
|
||||
/// Get the length of the underlying list of Pokemon.
|
||||
pub fn length(&self) -> usize {
|
||||
self.pokemon.len()
|
||||
}
|
||||
|
||||
/// Gets the underlying list of Pokemon.
|
||||
pub fn pokemon(&self) -> &Vec<Option<Arc<Pokemon<'own, 'library>>>> {
|
||||
&self.pokemon
|
||||
}
|
||||
|
||||
/// Makes sure there are no empty spots in the party anymore, leaving the length the same.
|
||||
pub fn pack_party(&mut self) {
|
||||
let mut first_empty = None;
|
||||
let mut i = 0;
|
||||
|
||||
Reference in New Issue
Block a user