Fix base types crate

This commit is contained in:
hodasemi 2024-08-24 12:15:40 +02:00
parent 53fdd899bd
commit 89023a8e6f
4 changed files with 48 additions and 23 deletions

View file

@ -6,18 +6,19 @@ use paste;
use std::{ use std::{
slice::{Iter, IterMut}, slice::{Iter, IterMut},
str::FromStr, str::FromStr,
sync::Arc,
}; };
use crate::components::inventory::Storable;
use crate::config::save_game::SaveGame; use crate::config::save_game::SaveGame;
use crate::items::{ability_addon::AbilityAddonTypes, ability_book::AbilityBook, Rarities}; use crate::items::{ability_addon::AbilityAddonTypes, ability_book::AbilityBook, Rarities};
use crate::{components::inventory::Storable, items::ItemSystem};
macro_rules! load { macro_rules! load {
($me: ident, $save_game:ident, $($index:literal,)+) => { ($me: ident, $save_game:ident, $($index:literal,)+) => {
paste::expr! { paste::expr! {
$( $(
if $save_game.[<ability_ $index>].used { if $save_game.[<ability_ $index>].used {
let item_system = $me.game_handle.upgrade().item_system(); let item_system = &$me.item_system;
let ability = &$save_game.[<ability_ $index>]; let ability = &$save_game.[<ability_ $index>];
@ -106,6 +107,8 @@ macro_rules! store {
} }
pub struct AbilitySlots { pub struct AbilitySlots {
item_system: Arc<ItemSystem>,
pub direction: Vector2<f32>, pub direction: Vector2<f32>,
abilities: [Option<AbilityBook>; AbilitySlots::MAX_ABILITIES], abilities: [Option<AbilityBook>; AbilitySlots::MAX_ABILITIES],
@ -115,16 +118,18 @@ impl AbilitySlots {
// stupid workaround for serde Deserialize // stupid workaround for serde Deserialize
pub const MAX_ABILITIES: usize = 4; pub const MAX_ABILITIES: usize = 4;
pub fn empty() -> Self { pub fn empty(item_system: Arc<ItemSystem>) -> Self {
Self { Self {
item_system,
direction: Vector2::zero(), direction: Vector2::zero(),
abilities: Default::default(), abilities: Default::default(),
} }
} }
pub fn load(save_game: &SaveGame) -> Result<Self> { pub fn load(item_system: Arc<ItemSystem>, save_game: &SaveGame) -> Result<Self> {
let mut me = Self::empty(); let mut me = Self::empty(item_system);
load!(me, save_game, 0, 1, 2, 3,); load!(me, save_game, 0, 1, 2, 3,);

View file

@ -3,7 +3,7 @@ use assetpath::AssetPath;
use engine::prelude::*; use engine::prelude::*;
use cgmath::Vector3; use cgmath::Vector3;
use std::{path::Path, sync::Arc}; use std::path::Path;
use crate::{ use crate::{
ability_type::AbilityType, ability_type::AbilityType,
@ -116,6 +116,10 @@ impl AbilityLoader {
)?) )?)
} }
pub fn create_ability(&self) -> Ability {
Ability { data: self.clone() }
}
pub fn name(&self) -> &str { pub fn name(&self) -> &str {
&self.name &self.name
} }
@ -183,48 +187,54 @@ impl AbilityLoader {
pub fn create_on_hit_particles2( pub fn create_on_hit_particles2(
&self, &self,
engine: &Engine,
position: Vector3<f32>, position: Vector3<f32>,
offset: Vector3<f32>, offset: Vector3<f32>,
particle_system_vulkan_objects: &ParticleSystemVulkanObjects, particle_system_vulkan_objects: &ParticleSystemVulkanObjects,
build_path: impl Fn(&str) -> AssetPath,
) -> Result<Option<EntityObject>> { ) -> Result<Option<EntityObject>> {
Ok(if self.settings.parameter.on_hit_particles.is_empty() { Ok(if self.settings.parameter.on_hit_particles.is_empty() {
None None
} else { } else {
let file_name = game.build_data_path(&self.settings.parameter.on_hit_particles); let file_name = build_path(&self.settings.parameter.on_hit_particles);
let info = ParticleSystemInfo::load(file_name)?; let info = ParticleSystemInfo::load(file_name)?;
Some(Self::create_on_hit_particles( Some(Self::create_on_hit_particles(
engine,
info, info,
&self.settings.parameter.on_collision_sound, &self.settings.parameter.on_collision_sound,
position, position,
offset, offset,
particle_system_vulkan_objects, particle_system_vulkan_objects,
build_path,
)?) )?)
}) })
} }
pub fn create_on_hit_particles( pub fn create_on_hit_particles(
engine: &Engine,
info: ParticleSystemInfo, info: ParticleSystemInfo,
on_collision_sound: &str, on_collision_sound: &str,
position: Vector3<f32>, position: Vector3<f32>,
offset: Vector3<f32>, offset: Vector3<f32>,
particle_system_vulkan_objects: &ParticleSystemVulkanObjects, particle_system_vulkan_objects: &ParticleSystemVulkanObjects,
build_path: impl Fn(&str) -> AssetPath,
) -> Result<EntityObject> { ) -> Result<EntityObject> {
let mut particle_entity = game.engine().assets().empty_entity(); let mut particle_entity = engine.assets().empty_entity();
{ {
let mut draw = Draw::new(Vec::new()); let mut draw = Draw::new(Vec::new());
let particles = ParticleSystem::new_with_vk_objects( let particles = ParticleSystem::new_with_vk_objects(
info, info,
&game.engine(), engine,
particle_system_vulkan_objects, particle_system_vulkan_objects,
&mut draw, &mut draw,
)?; )?;
if !on_collision_sound.is_empty() { if !on_collision_sound.is_empty() {
let mut audio = Audio::new(game.engine().context(), None)?; let mut audio = Audio::new(engine.context(), None)?;
audio.set_sound(game.build_data_path(on_collision_sound), SOUND_CREATE_KEY)?; audio.set_sound(build_path(on_collision_sound), ON_ENABLE_SOUND)?;
particle_entity.insert_component(audio); particle_entity.insert_component(audio);
} }

View file

@ -17,9 +17,19 @@ use super::{
ItemSystem, Rarities, ToolTipBuilder, Tooltip, ItemSystem, Rarities, ToolTipBuilder, Tooltip,
}; };
pub trait Ability: Send + Sync { #[derive(Debug, Clone)]
fn data(&self) -> &AbilityLoader; pub struct Ability {
fn name(&self) -> &str; pub(crate) data: AbilityLoader,
}
impl Ability {
pub fn data(&self) -> &AbilityLoader {
&self.data
}
pub fn name(&self) -> &str {
self.data.name()
}
} }
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
@ -31,7 +41,7 @@ pub struct CastInformation {
#[derive(Clone)] #[derive(Clone)]
pub struct AbilityBook { pub struct AbilityBook {
ability: Arc<dyn Ability>, ability: Ability,
// meta // meta
icon: Arc<Image>, icon: Arc<Image>,
@ -49,7 +59,7 @@ pub struct AbilityBook {
impl AbilityBook { impl AbilityBook {
pub fn new( pub fn new(
ability: Arc<dyn Ability>, ability: Ability,
icon: Arc<Image>, icon: Arc<Image>,
rarity: Rarities, rarity: Rarities,
ability_settings: &AbilitySettings, ability_settings: &AbilitySettings,
@ -73,7 +83,7 @@ impl AbilityBook {
} }
pub fn load( pub fn load(
ability: Arc<dyn Ability>, ability: Ability,
icon: Arc<Image>, icon: Arc<Image>,
rarity: Rarities, rarity: Rarities,
addons: Vec<Option<AbilityAddon>>, addons: Vec<Option<AbilityAddon>>,
@ -191,8 +201,8 @@ impl AbilityBook {
} }
} }
pub fn ability(&self) -> &dyn Ability { pub fn ability(&self) -> &Ability {
&*self.ability &self.ability
} }
pub fn addons(&self) -> &AbilityAddonCollection { pub fn addons(&self) -> &AbilityAddonCollection {

View file

@ -49,7 +49,7 @@ pub struct ItemSystem {
addon_icon_combinations: HashMap<(Rarities, AbilityAddonTypes), Arc<Image>>, addon_icon_combinations: HashMap<(Rarities, AbilityAddonTypes), Arc<Image>>,
jewel_icon_combinations: HashMap<(Rarities, u32, Attribute), Arc<Image>>, jewel_icon_combinations: HashMap<(Rarities, u32, Attribute), Arc<Image>>,
abilities: Vec<Arc<dyn Ability>>, abilities: Vec<Ability>,
} }
impl ItemSystem { impl ItemSystem {
@ -263,7 +263,7 @@ impl ItemSystem {
let abilities = ability_loader let abilities = ability_loader
.into_iter() .into_iter()
.map(|loader| loader.create_ability()) .map(|loader| loader.create_ability())
.collect::<Result<Vec<Arc<dyn Ability>>>>()?; .collect::<Vec<Ability>>();
Ok(ItemSystem { Ok(ItemSystem {
item_settings: item_settings.clone(), item_settings: item_settings.clone(),
@ -545,7 +545,7 @@ impl ItemSystem {
} }
#[inline] #[inline]
pub fn find_ability(&self, name: &str) -> Arc<dyn Ability> { pub fn find_ability(&self, name: &str) -> Ability {
self.abilities self.abilities
.iter() .iter()
.find(|a| a.name() == name) .find(|a| a.name() == name)
@ -554,7 +554,7 @@ impl ItemSystem {
} }
#[inline] #[inline]
pub fn random_ability(&self) -> Arc<dyn Ability> { pub fn random_ability(&self) -> Ability {
let n = Random::range(0, self.abilities.len() as u32); let n = Random::range(0, self.abilities.len() as u32);
self.abilities[n as usize].clone() self.abilities[n as usize].clone()
} }