pub struct EnumMap<K, V> { /* private fields */ }Expand description
A map from enum-like keys to values, backed by a Vec of (K, V) pairs.
Designed for use with small, game-specific enums (e.g. stat IDs, type IDs). Lookups are O(n) linear scan — acceptable for N ≤ 15.
§Type Parameters
K— Key type, typically a copyable game-enum variant (Copy + PartialEq).V— Value type.
Implementations§
Source§impl<K: PartialEq, V> EnumMap<K, V>
impl<K: PartialEq, V> EnumMap<K, V>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create an empty map.
Examples found in repository?
examples/hello_dotzuki.rs (line 251)
246 fn create_monster(&self, species: Species, level: u8) -> BattlerState<Self> {
247 let (hp, atk, def, spd, moves) = match species {
248 Species::Warrior => (100u16, 15u16, 10u16, 8u16, vec![MoveKind::Slash]),
249 Species::Mage => (80u16, 20u16, 5u16, 12u16, vec![MoveKind::Fireball]),
250 };
251 let mut stats = EnumMap::new();
252 stats.set(StatId::HP, hp);
253 stats.set(StatId::ATK, atk);
254 stats.set(StatId::DEF, def);
255 stats.set(StatId::SPD, spd + level as u16);
256 BattlerState::new(species, hp, hp, stats, moves)
257 }Sourcepub fn set(&mut self, key: K, value: V)
pub fn set(&mut self, key: K, value: V)
Insert or update a key-value pair.
Examples found in repository?
examples/hello_dotzuki.rs (line 252)
246 fn create_monster(&self, species: Species, level: u8) -> BattlerState<Self> {
247 let (hp, atk, def, spd, moves) = match species {
248 Species::Warrior => (100u16, 15u16, 10u16, 8u16, vec![MoveKind::Slash]),
249 Species::Mage => (80u16, 20u16, 5u16, 12u16, vec![MoveKind::Fireball]),
250 };
251 let mut stats = EnumMap::new();
252 stats.set(StatId::HP, hp);
253 stats.set(StatId::ATK, atk);
254 stats.set(StatId::DEF, def);
255 stats.set(StatId::SPD, spd + level as u16);
256 BattlerState::new(species, hp, hp, stats, moves)
257 }Sourcepub fn get(&self, key: K) -> Option<&V>
pub fn get(&self, key: K) -> Option<&V>
Look up a value by key. Returns None if the key is not present.
Examples found in repository?
examples/hello_dotzuki.rs (line 205)
197 fn calculate_damage(
198 &self,
199 move_: &MoveKind,
200 attacker: &BattlerState<Self>,
201 defender: &BattlerState<Self>,
202 _random: u8,
203 _is_critical: bool,
204 ) -> DamageResult {
205 let atk = attacker.stats.get(StatId::ATK).copied().unwrap_or(10);
206 let def = defender.stats.get(StatId::DEF).copied().unwrap_or(5).max(1);
207 let eff = HelloConfig::effectiveness(&move_.element(), &[defender.species.element()]);
208 let raw = (move_.power() as f32 * eff * atk as f32 / def as f32) as u16;
209 DamageResult {
210 damage: raw.max(1),
211 effectiveness: eff,
212 is_miss: false,
213 }
214 }Trait Implementations§
Auto Trait Implementations§
impl<K, V> Freeze for EnumMap<K, V>
impl<K, V> RefUnwindSafe for EnumMap<K, V>
impl<K, V> Send for EnumMap<K, V>
impl<K, V> Sync for EnumMap<K, V>
impl<K, V> Unpin for EnumMap<K, V>
impl<K, V> UnsafeUnpin for EnumMap<K, V>
impl<K, V> UnwindSafe for EnumMap<K, V>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more