API tweaks

master
Mark 2024-02-16 18:26:37 -08:00
parent 9f19becf0c
commit 82f2f5fc85
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
2 changed files with 72 additions and 13 deletions

View File

@ -1,6 +1,8 @@
use galactica_content::{Content, Outfit}; use galactica_content::{Content, Outfit, OutfitSpace};
use log::error; use log::error;
use rhai::{CustomType, FnNamespace, FuncRegistration, ImmutableString, Module, TypeBuilder}; use rhai::{
CustomType, Dynamic, FnNamespace, FuncRegistration, ImmutableString, Map, Module, TypeBuilder,
};
use std::sync::Arc; use std::sync::Arc;
pub fn build_ct_module(ct_src: Arc<Content>) -> Module { pub fn build_ct_module(ct_src: Arc<Content>) -> Module {
@ -40,6 +42,10 @@ impl OutfitState {
pub fn new_none() -> Self { pub fn new_none() -> Self {
Self { outfit: None } Self { outfit: None }
} }
pub fn get_outfit(&self) -> Option<Arc<Outfit>> {
self.outfit.as_ref().cloned()
}
} }
impl CustomType for OutfitState { impl CustomType for OutfitState {
@ -71,6 +77,20 @@ impl CustomType for OutfitState {
.map(|x| x.thumbnail.index.to_string()) .map(|x| x.thumbnail.index.to_string())
.unwrap_or("".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 // Stat getters
// //
@ -104,6 +124,9 @@ impl CustomType for OutfitState {
.map(|x| x.stats.shield_delay) .map(|x| x.stats.shield_delay)
.unwrap_or(0.0) .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| { .with_fn("stat_shield_dps", |s: &mut Self| {
s.outfit s.outfit
.as_ref() .as_ref()

View File

@ -7,7 +7,7 @@ use galactica_util::to_degrees;
use log::error; use log::error;
use nalgebra::Vector2; use nalgebra::Vector2;
use rapier2d::dynamics::RigidBody; use rapier2d::dynamics::RigidBody;
use rhai::{Array, CustomType, Dynamic, ImmutableString, TypeBuilder}; use rhai::{Array, CustomType, Dynamic, ImmutableString, Map, TypeBuilder};
use std::sync::Arc; use std::sync::Arc;
use super::{functions::OutfitState, Color, UiVector}; use super::{functions::OutfitState, Color, UiVector};
@ -94,14 +94,6 @@ impl CustomType for ShipState {
s.get_content().thumbnail.index.clone().into() s.get_content().thumbnail.index.clone().into()
}) })
.with_fn("landed_on", |s: &mut Self| s.landed_on()) .with_fn("landed_on", |s: &mut Self| s.landed_on())
.with_fn("current_shields", |s: &mut Self| {
s.get_ship().get_data().get_shields()
})
.with_fn("total_hull", |s: &mut Self| s.get_content().hull)
.with_fn("empty_mass", |s: &mut Self| s.get_content().mass)
.with_fn("current_hull", |s: &mut Self| {
s.get_ship().get_data().get_hull()
})
.with_fn("get_size", |s: &mut Self| s.get_content().size) .with_fn("get_size", |s: &mut Self| s.get_content().size)
.with_fn("phys_uid", |s: &mut Self| format!("{}", s.get_ship().uid)) .with_fn("phys_uid", |s: &mut Self| format!("{}", s.get_ship().uid))
.with_fn("get_pos", |s: &mut Self| { .with_fn("get_pos", |s: &mut Self| {
@ -114,7 +106,46 @@ impl CustomType for ShipState {
Color::new(c[0], c[1], c[2], 1.0) Color::new(c[0], c[1], c[2], 1.0)
}) })
// //
// Stat getters // Ship stat getters
//
.with_fn("used_space", |s: &mut Self| {
Map::from_iter(
s.get_ship()
.get_data()
.get_outfits()
.get_used_space()
.to_hashmap()
.iter()
.map(|(k, v)| (ImmutableString::from(k).into(), Dynamic::from(*v as i64))),
)
})
.with_fn("total_space", |s: &mut Self| {
Map::from_iter(
s.get_ship()
.get_data()
.get_outfits()
.get_total_space()
.to_hashmap()
.iter()
.map(|(k, v)| (ImmutableString::from(k).into(), Dynamic::from(*v as i64))),
)
})
.with_fn("total_gun_points", |s: &mut Self| {
s.get_ship().get_data().get_outfits().total_gun_points() as i64
})
.with_fn("free_gun_points", |s: &mut Self| {
s.get_ship().get_data().get_outfits().free_gun_points() as i64
})
.with_fn("current_shields", |s: &mut Self| {
s.get_ship().get_data().get_shields()
})
.with_fn("total_hull", |s: &mut Self| s.get_content().hull)
.with_fn("empty_mass", |s: &mut Self| s.get_content().mass)
.with_fn("current_hull", |s: &mut Self| {
s.get_ship().get_data().get_hull()
})
//
// Outfit stat getters
// //
.with_fn("stat_thrust", |s: &mut Self| { .with_fn("stat_thrust", |s: &mut Self| {
s.get_ship() s.get_ship()
@ -264,7 +295,12 @@ impl State {
pub fn player_ship(&mut self) -> ShipState { pub fn player_ship(&mut self) -> ShipState {
ShipState { ShipState {
input: self.input.clone(), input: self.input.clone(),
ship: self.input.player.ship.map(|x| PhysSimShipHandle(x)), ship: self
.input
.player
.borrow()
.ship
.map(|x| PhysSimShipHandle(x)),
} }
} }