125 lines
3.4 KiB
Rust
125 lines
3.4 KiB
Rust
|
use std::{
|
||
|
str::{from_utf8, FromStr},
|
||
|
sync::Arc,
|
||
|
};
|
||
|
|
||
|
use anyhow::Result;
|
||
|
use engine::prelude::*;
|
||
|
|
||
|
use super::{Rarities, Tooltip};
|
||
|
use crate::{
|
||
|
components::{attributes::Attribute, inventory::Storable, statistic_types::StatisticType},
|
||
|
config::items::ItemSettings,
|
||
|
};
|
||
|
|
||
|
#[derive(Clone, Debug)]
|
||
|
pub struct Jewel {
|
||
|
pub rarity: Rarities,
|
||
|
pub level: u32,
|
||
|
pub attribute: Attribute,
|
||
|
|
||
|
pub stat: StatisticType,
|
||
|
|
||
|
pub icon: Option<Arc<Image>>,
|
||
|
}
|
||
|
|
||
|
impl PartialEq for Jewel {
|
||
|
fn eq(&self, other: &Self) -> bool {
|
||
|
self.rarity == other.rarity && self.level == other.level
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Jewel {
|
||
|
pub fn into_persistent(&self) -> String {
|
||
|
format!(
|
||
|
"{}|{}|{}|{}",
|
||
|
self.rarity, self.level, self.attribute, self.stat
|
||
|
)
|
||
|
}
|
||
|
|
||
|
pub fn from_persistent<'a>(split: &mut impl Iterator<Item = &'a str>) -> Result<Self> {
|
||
|
let rarity = Rarities::from_str(split.next().unwrap())?;
|
||
|
let level = split.next().unwrap().parse::<u32>()?;
|
||
|
let attribute = Attribute::from_str(split.next().unwrap())?;
|
||
|
let stat = StatisticType::from_str(split.next().unwrap())?;
|
||
|
|
||
|
Ok(Self {
|
||
|
rarity,
|
||
|
level,
|
||
|
attribute,
|
||
|
stat,
|
||
|
|
||
|
icon: None,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
pub fn update_stat(&mut self, item_settings: &ItemSettings) {
|
||
|
self.stat
|
||
|
.apply_for_jewel(self.rarity, self.level, item_settings);
|
||
|
}
|
||
|
|
||
|
pub fn create_tooltip(
|
||
|
&self,
|
||
|
gui_handler: &Arc<GuiHandler>,
|
||
|
item_settings: &ItemSettings,
|
||
|
position: (i32, i32),
|
||
|
) -> Result<Tooltip> {
|
||
|
let inspector_snippet: Arc<GuiBuilder> = GuiBuilder::from_str(
|
||
|
gui_handler,
|
||
|
include_str!("../../resources/jewel_tooltip.xml"),
|
||
|
)?;
|
||
|
|
||
|
let main_grid: Arc<Grid> = inspector_snippet.element("main_grid")?;
|
||
|
main_grid.change_position_unscaled(position.0, position.1)?;
|
||
|
|
||
|
let jewel_icon: Arc<Icon> = inspector_snippet.element("jewel_icon")?;
|
||
|
let rarity_label: Arc<Label> = inspector_snippet.element("rarity_label")?;
|
||
|
|
||
|
let attribute_type: Arc<Label> = inspector_snippet.element("attribute_type")?;
|
||
|
let attribute_value: Arc<Label> = inspector_snippet.element("attribute_value")?;
|
||
|
|
||
|
let stat_type: Arc<Label> = inspector_snippet.element("stat_type")?;
|
||
|
let stat_value: Arc<Label> = inspector_snippet.element("stat_value")?;
|
||
|
|
||
|
jewel_icon.set_icon(&self.icon())?;
|
||
|
rarity_label.set_text(format!("{} ({})", self.rarity, self.level))?;
|
||
|
|
||
|
attribute_type.set_text({
|
||
|
let mut s = self.attribute.to_string();
|
||
|
s.replace_range(
|
||
|
0..1,
|
||
|
from_utf8(&[s.as_bytes()[0].to_ascii_uppercase()]).unwrap(),
|
||
|
);
|
||
|
|
||
|
s
|
||
|
})?;
|
||
|
|
||
|
attribute_value.set_text(
|
||
|
item_settings
|
||
|
.jewel_rarity_multiplier
|
||
|
.from_rarity(self.rarity)
|
||
|
* self.level
|
||
|
* item_settings.general.jewel_level_multiplier,
|
||
|
)?;
|
||
|
|
||
|
stat_type.set_text(&self.stat)?;
|
||
|
stat_value.set_text(self.stat.display_value())?;
|
||
|
|
||
|
Ok(Tooltip::new(
|
||
|
main_grid,
|
||
|
inspector_snippet,
|
||
|
gui_handler.clone(),
|
||
|
))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Storable for Jewel {
|
||
|
fn rarity(&self) -> Rarities {
|
||
|
self.rarity
|
||
|
}
|
||
|
|
||
|
fn icon(&self) -> Arc<Image> {
|
||
|
self.icon.clone().unwrap()
|
||
|
}
|
||
|
}
|