Adds ability to get the current time of day
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-07-22 13:22:47 +02:00
parent c75541720b
commit 0c6a0cadfe
10 changed files with 132 additions and 25 deletions

View File

@@ -156,7 +156,7 @@ pub mod test {
static_data: Arc::new(crate::static_data::libraries::static_data::test::build()),
stat_calculator: Arc::new(Gen7BattleStatCalculator::new()),
damage_calculator: Arc::new(Gen7DamageLibrary::new(false)),
misc_library: Arc::new(Gen7MiscLibrary::new()),
misc_library: Arc::new(Gen7MiscLibrary::default()),
script_resolver: Arc::new(EmptyScriptResolver {}),
}
}

View File

@@ -1,4 +1,5 @@
use std::fmt::Debug;
use chrono::Timelike;
use std::fmt::{Debug, Formatter};
use std::sync::Arc;
use hashbrown::HashSet;
@@ -6,7 +7,7 @@ use hashbrown::HashSet;
use crate::dynamic_data::choices::{MoveChoice, TurnChoice};
use crate::dynamic_data::Pokemon;
use crate::dynamic_data::{LearnedMove, MoveLearnMethod};
use crate::static_data::{MoveCategory, MoveData, MoveDataImpl, MoveTarget, SecondaryEffectImpl};
use crate::static_data::{MoveCategory, MoveData, MoveDataImpl, MoveTarget, SecondaryEffectImpl, TimeOfDay};
use crate::StringKey;
/// The misc library holds several misc functions required for the battle to run.
@@ -16,19 +17,24 @@ pub trait MiscLibrary: Debug {
/// Returns the move we need to use if we can't use another move. Typically Struggle.
fn replacement_move(&self, user: &Pokemon, target_side: u8, target_index: u8) -> TurnChoice;
// TODO: can evolve from level up?
// TODO: get time
/// Gets the current time of day for the battle.
fn time_of_day(&self) -> TimeOfDay;
}
/// A function pointer to get the time of day.
type GetTimeOfDayFn = Box<dyn Fn() -> TimeOfDay>;
/// A gen 7 implementation for the MiscLibrary.
#[derive(Debug)]
pub struct Gen7MiscLibrary {
/// The learned move data for struggle.
struggle_learned_move: Arc<LearnedMove>,
/// The function to get the time of day.
get_time_fn: GetTimeOfDayFn,
}
impl Gen7MiscLibrary {
/// Instantiates a new MiscLibrary.
pub fn new() -> Self {
pub fn new(get_time_fn: GetTimeOfDayFn) -> Self {
let struggle_data: Arc<dyn MoveData> = Arc::new(MoveDataImpl::new(
&StringKey::new("struggle"),
0.into(),
@@ -46,13 +52,28 @@ impl Gen7MiscLibrary {
HashSet::new(),
));
let struggle_learned_move = Arc::new(LearnedMove::new(struggle_data, MoveLearnMethod::Unknown));
Self { struggle_learned_move }
Self {
struggle_learned_move,
get_time_fn,
}
}
}
impl Default for Gen7MiscLibrary {
fn default() -> Self {
Self::new()
Self::new(Box::new(|| {
let time = chrono::Local::now().time();
let hour = time.hour();
// Following the values for Pokemon Sun.
match hour {
0..=5 => TimeOfDay::Night,
6..=9 => TimeOfDay::Morning,
10..=16 => TimeOfDay::Day,
17 => TimeOfDay::Evening,
18..=23 => TimeOfDay::Night,
_ => unreachable!(),
}
}))
}
}
@@ -70,4 +91,14 @@ impl MiscLibrary for Gen7MiscLibrary {
target_index,
))
}
fn time_of_day(&self) -> TimeOfDay {
(self.get_time_fn)()
}
}
impl Debug for Gen7MiscLibrary {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str("Gen7MiscLibrary")
}
}