A lot more work on handling scripts properly.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
164
src/dynamic_data/models/executing_move.rs
Normal file
164
src/dynamic_data/models/executing_move.rs
Normal file
@@ -0,0 +1,164 @@
|
||||
use crate::dynamic_data::models::learned_move::LearnedMove;
|
||||
use crate::dynamic_data::models::pokemon::Pokemon;
|
||||
use crate::dynamic_data::script_handling::script::ScriptContainer;
|
||||
use crate::dynamic_data::script_handling::{ScriptSource, ScriptSourceData, ScriptWrapper};
|
||||
use crate::static_data::MoveData;
|
||||
use crate::{PkmnResult, PokemonError};
|
||||
use parking_lot::RwLock;
|
||||
use std::ops::Deref;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct HitData {
|
||||
critical: bool,
|
||||
base_power: u8,
|
||||
effectiveness: f32,
|
||||
damage: u32,
|
||||
move_type: u8,
|
||||
has_failed: bool,
|
||||
}
|
||||
|
||||
impl HitData {
|
||||
pub fn is_critical(&self) -> bool {
|
||||
self.critical
|
||||
}
|
||||
pub fn base_power(&self) -> u8 {
|
||||
self.base_power
|
||||
}
|
||||
pub fn effectiveness(&self) -> f32 {
|
||||
self.effectiveness
|
||||
}
|
||||
pub fn damage(&self) -> u32 {
|
||||
self.damage
|
||||
}
|
||||
pub fn move_type(&self) -> u8 {
|
||||
self.move_type
|
||||
}
|
||||
pub fn has_failed(&self) -> bool {
|
||||
self.has_failed
|
||||
}
|
||||
|
||||
pub fn set_critical(&mut self, value: bool) {
|
||||
self.critical = value;
|
||||
}
|
||||
pub fn set_base_power(&mut self, value: u8) {
|
||||
self.base_power = value;
|
||||
}
|
||||
pub fn set_effectiveness(&mut self, value: f32) {
|
||||
self.effectiveness = value;
|
||||
}
|
||||
pub fn set_damage(&mut self, value: u32) {
|
||||
self.damage = value;
|
||||
}
|
||||
pub fn set_move_type(&mut self, value: u8) {
|
||||
self.move_type = value;
|
||||
}
|
||||
pub fn fail(&mut self) {
|
||||
self.has_failed = true;
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ExecutingMove<'a, 'b> {
|
||||
number_of_hits: u8,
|
||||
hits: Vec<HitData>,
|
||||
user: &'a Pokemon<'b>,
|
||||
chosen_move: &'a LearnedMove,
|
||||
use_move: &'a MoveData,
|
||||
script: ScriptContainer,
|
||||
targets: Vec<Option<&'a Pokemon<'b>>>,
|
||||
}
|
||||
|
||||
impl<'a, 'b> ExecutingMove<'a, 'b> {
|
||||
pub fn new(
|
||||
targets: Vec<Option<&'a Pokemon<'b>>>,
|
||||
number_of_hits: u8,
|
||||
user: &'a Pokemon<'b>,
|
||||
chosen_move: &'a LearnedMove,
|
||||
use_move: &'a MoveData,
|
||||
script: ScriptContainer,
|
||||
) -> Self {
|
||||
let total_hits = number_of_hits as usize * targets.len();
|
||||
let mut hits = Vec::with_capacity(total_hits);
|
||||
for _i in 0..total_hits {
|
||||
hits.push(HitData::default())
|
||||
}
|
||||
Self {
|
||||
number_of_hits,
|
||||
hits,
|
||||
user,
|
||||
chosen_move,
|
||||
use_move,
|
||||
script,
|
||||
targets,
|
||||
}
|
||||
}
|
||||
pub fn target_count(&self) -> usize {
|
||||
self.targets.len()
|
||||
}
|
||||
pub fn number_of_hits(&self) -> u8 {
|
||||
self.number_of_hits
|
||||
}
|
||||
pub fn user(&self) -> &'a Pokemon<'b> {
|
||||
self.user
|
||||
}
|
||||
pub fn chosen_move(&self) -> &'a LearnedMove {
|
||||
self.chosen_move
|
||||
}
|
||||
pub fn use_move(&self) -> &'a MoveData {
|
||||
self.use_move
|
||||
}
|
||||
pub fn script(&self) -> &ScriptContainer {
|
||||
&self.script
|
||||
}
|
||||
|
||||
pub fn get_hit_data(&self, for_target: &Pokemon<'b>, hit: u8) -> PkmnResult<&HitData> {
|
||||
for (index, target) in self.targets.iter().enumerate() {
|
||||
if let Some(target) = target {
|
||||
if std::ptr::eq(target.deref(), for_target) {
|
||||
let i = index * self.number_of_hits as usize + hit as usize;
|
||||
return Ok(&self.hits[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(PokemonError::InvalidTargetRequested)
|
||||
}
|
||||
|
||||
pub fn get_target_slice(&self, for_target: &Pokemon<'b>) -> PkmnResult<&[HitData]> {
|
||||
for (index, target) in self.targets.iter().enumerate() {
|
||||
if let Some(target) = target {
|
||||
if std::ptr::eq(target.deref(), for_target) {
|
||||
let i = index * self.number_of_hits as usize;
|
||||
return Ok(&self.hits[i..i + self.number_of_hits as usize]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(PokemonError::InvalidTargetRequested)
|
||||
}
|
||||
|
||||
pub fn is_pokemon_target(&self, pokemon: &Pokemon<'b>) -> bool {
|
||||
for target in self.targets.iter().flatten() {
|
||||
if std::ptr::eq(target.deref(), pokemon) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b> ScriptSource<'a> for ExecutingMove<'a, 'b> {
|
||||
fn get_script_count(&self) -> usize {
|
||||
1
|
||||
}
|
||||
|
||||
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn get_own_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
|
||||
scripts.push((&self.script).into());
|
||||
}
|
||||
|
||||
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
|
||||
self.get_own_scripts(scripts);
|
||||
self.user.get_own_scripts(scripts);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user