Change to RwLock over Mutex, so we can read without locking, changes to BattleRandom to allow for cloning, and prevent race conditions.
This commit is contained in:
@@ -1,8 +1,22 @@
|
||||
use crate::utils::random::Random;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
use std::sync::Mutex;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct BattleRandom {
|
||||
random: Random,
|
||||
random: Mutex<Random>,
|
||||
}
|
||||
|
||||
impl BattleRandom {
|
||||
pub fn new_with_seed(seed: u128) -> Self {
|
||||
BattleRandom {
|
||||
random: Mutex::new(Random::new(seed)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_rng(&self) -> &Mutex<Random> {
|
||||
&self.random
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for BattleRandom {
|
||||
@@ -11,20 +25,10 @@ impl Debug for BattleRandom {
|
||||
}
|
||||
}
|
||||
|
||||
impl BattleRandom {
|
||||
pub fn new() -> Self {
|
||||
return BattleRandom {
|
||||
random: Default::default(),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn new_with_seed(seed: u128) -> Self {
|
||||
return BattleRandom {
|
||||
random: Random::new(seed),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn get_rng(&mut self) -> &mut Random {
|
||||
&mut self.random
|
||||
impl Clone for BattleRandom {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
random: Mutex::new(self.random.lock().unwrap().clone()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user