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

View file

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

View file

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

View file

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