145 lines
3.3 KiB
Rust
145 lines
3.3 KiB
Rust
use galactica_content::{Content, Outfit, OutfitSpace};
|
|
use log::error;
|
|
use rhai::{
|
|
CustomType, Dynamic, FnNamespace, FuncRegistration, ImmutableString, Map, Module, TypeBuilder,
|
|
};
|
|
use std::sync::Arc;
|
|
|
|
pub fn build_ct_module(ct_src: Arc<Content>) -> Module {
|
|
let mut module = Module::new();
|
|
module.set_id("GalacticaContentModule");
|
|
|
|
let ct = ct_src.clone();
|
|
let _ = FuncRegistration::new("get_outfit")
|
|
.with_namespace(FnNamespace::Internal)
|
|
.set_into_module(&mut module, move |outfit_name: ImmutableString| {
|
|
let outfit = ct.outfits.get(&outfit_name.clone().into());
|
|
|
|
match outfit {
|
|
Some(o) => OutfitState::new(o.clone()),
|
|
None => {
|
|
error!("called `ct::get_outfit` with an invalid outfit `{outfit_name:?}`");
|
|
OutfitState::new_none()
|
|
}
|
|
}
|
|
});
|
|
|
|
return module;
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct OutfitState {
|
|
outfit: Option<Arc<Outfit>>,
|
|
}
|
|
|
|
impl OutfitState {
|
|
pub fn new(outfit: Arc<Outfit>) -> Self {
|
|
Self {
|
|
outfit: Some(outfit),
|
|
}
|
|
}
|
|
|
|
pub fn new_none() -> Self {
|
|
Self { outfit: None }
|
|
}
|
|
|
|
pub fn get_outfit(&self) -> Option<Arc<Outfit>> {
|
|
self.outfit.as_ref().cloned()
|
|
}
|
|
}
|
|
|
|
impl CustomType for OutfitState {
|
|
fn build(mut builder: TypeBuilder<Self>) {
|
|
builder
|
|
.with_name("OutfitState")
|
|
.with_fn("is_some", |s: &mut Self| s.outfit.is_some())
|
|
.with_fn("display_name", |s: &mut Self| {
|
|
s.outfit
|
|
.as_ref()
|
|
.map(|x| x.display_name.clone())
|
|
.unwrap_or("".to_string())
|
|
})
|
|
.with_fn("desc", |s: &mut Self| {
|
|
s.outfit
|
|
.as_ref()
|
|
.map(|x| x.desc.clone())
|
|
.unwrap_or("".to_string())
|
|
})
|
|
.with_fn("index", |s: &mut Self| {
|
|
s.outfit
|
|
.as_ref()
|
|
.map(|x| x.index.to_string())
|
|
.unwrap_or("".to_string())
|
|
})
|
|
.with_fn("thumbnail", |s: &mut Self| {
|
|
s.outfit
|
|
.as_ref()
|
|
.map(|x| x.thumbnail.index.to_string())
|
|
.unwrap_or("".to_string())
|
|
})
|
|
.with_fn("cost", |s: &mut Self| {
|
|
s.outfit.as_ref().map(|x| x.cost).unwrap_or(0)
|
|
})
|
|
.with_fn("required_space", |s: &mut Self| {
|
|
Map::from_iter(
|
|
s.outfit
|
|
.as_ref()
|
|
.map(|x| x.space)
|
|
.unwrap_or(OutfitSpace::new())
|
|
.to_hashmap()
|
|
.iter()
|
|
.map(|(k, v)| (ImmutableString::from(k).into(), Dynamic::from(*v as i64))),
|
|
)
|
|
})
|
|
//
|
|
// Stat getters
|
|
//
|
|
.with_fn("stat_thrust", |s: &mut Self| {
|
|
s.outfit
|
|
.as_ref()
|
|
.map(|x| x.stats.steer_power)
|
|
.unwrap_or(0.0)
|
|
})
|
|
.with_fn("stat_steer_power", |s: &mut Self| {
|
|
s.outfit
|
|
.as_ref()
|
|
.map(|x| x.stats.steer_power)
|
|
.unwrap_or(0.0)
|
|
})
|
|
.with_fn("stat_shield_strength", |s: &mut Self| {
|
|
s.outfit
|
|
.as_ref()
|
|
.map(|x| x.stats.shield_strength)
|
|
.unwrap_or(0.0)
|
|
})
|
|
.with_fn("stat_shield_generation", |s: &mut Self| {
|
|
s.outfit
|
|
.as_ref()
|
|
.map(|x| x.stats.shield_generation)
|
|
.unwrap_or(0.0)
|
|
})
|
|
.with_fn("stat_shield_delay", |s: &mut Self| {
|
|
s.outfit
|
|
.as_ref()
|
|
.map(|x| x.stats.shield_delay)
|
|
.unwrap_or(0.0)
|
|
})
|
|
.with_fn("stat_is_gun", |s: &mut Self| {
|
|
s.outfit.as_ref().map(|x| x.gun.is_some()).unwrap_or(false)
|
|
})
|
|
.with_fn("stat_shield_dps", |s: &mut Self| {
|
|
s.outfit
|
|
.as_ref()
|
|
.map(|x| {
|
|
if x.gun.is_some() {
|
|
(1.0 / x.gun.as_ref().unwrap().rate)
|
|
* x.gun.as_ref().unwrap().projectile.damage
|
|
} else {
|
|
0.0
|
|
}
|
|
})
|
|
.unwrap_or(0.0)
|
|
});
|
|
}
|
|
}
|