120 lines
3.6 KiB
Rust
120 lines
3.6 KiB
Rust
use bevy_ecs::prelude::*;
|
|
|
|
use crate::{
|
|
bundle::{
|
|
armour::PlayerArmour,
|
|
passive::{DrugCooldown, Education, FactionUpgrades, Merits, PassiveBundle},
|
|
player::{PlayerBundle, PlayerStrategy, Weapons},
|
|
stat::{Defence, Dexterity, Speed, StatBundle, Strength},
|
|
},
|
|
dto::{armour::ArmourDto, draw_id, weapon::WeaponDto},
|
|
};
|
|
|
|
#[cfg_attr(feature = "json", derive(serde::Deserialize))]
|
|
pub struct EquippedWeapons {
|
|
pub primary: Option<WeaponDto>,
|
|
pub secondary: Option<WeaponDto>,
|
|
pub melee: Option<WeaponDto>,
|
|
pub temporary: Option<WeaponDto>,
|
|
}
|
|
|
|
#[cfg_attr(feature = "json", derive(serde::Deserialize))]
|
|
#[derive(Default)]
|
|
pub struct EquippedArmour {
|
|
pub helmet: Option<ArmourDto>,
|
|
pub body: Option<ArmourDto>,
|
|
pub pants: Option<ArmourDto>,
|
|
pub gloves: Option<ArmourDto>,
|
|
pub boots: Option<ArmourDto>,
|
|
}
|
|
|
|
#[cfg_attr(feature = "json", derive(serde::Deserialize))]
|
|
pub struct Stats {
|
|
pub str: f32,
|
|
pub def: f32,
|
|
pub spd: f32,
|
|
pub dex: f32,
|
|
}
|
|
|
|
#[cfg_attr(feature = "json", derive(serde::Deserialize))]
|
|
pub struct PlayerDto {
|
|
pub name: String,
|
|
pub level: u16,
|
|
pub stats: Stats,
|
|
pub merits: Option<Merits>,
|
|
pub education: Option<Education>,
|
|
pub weapons: EquippedWeapons,
|
|
pub armour: EquippedArmour,
|
|
pub faction: Option<FactionUpgrades>,
|
|
pub drug: Option<DrugCooldown>,
|
|
pub strategy: PlayerStrategy,
|
|
}
|
|
|
|
impl PlayerDto {
|
|
pub fn spawn(self, world: &mut World) -> EntityWorldMut<'_> {
|
|
let mut commands = world.spawn(draw_id());
|
|
|
|
commands.insert((
|
|
PlayerBundle::new(self.name, self.level, self.strategy),
|
|
StatBundle::<Strength>::new(self.stats.str),
|
|
StatBundle::<Defence>::new(self.stats.def),
|
|
StatBundle::<Speed>::new(self.stats.spd),
|
|
StatBundle::<Dexterity>::new(self.stats.dex),
|
|
));
|
|
|
|
let education = self.education.unwrap_or_default();
|
|
let merits = self.merits.unwrap_or_default();
|
|
let faction = self.faction.unwrap_or_default();
|
|
|
|
commands.insert(PassiveBundle {
|
|
education,
|
|
merits,
|
|
faction,
|
|
});
|
|
|
|
let mut weapons = None;
|
|
commands.with_children(|spawner| {
|
|
let primary = self.weapons.primary.map(|p| p.spawn(spawner));
|
|
let secondary = self.weapons.secondary.map(|s| s.spawn(spawner));
|
|
let melee = self.weapons.melee.map(|m| m.spawn(spawner));
|
|
let temporary = self.weapons.temporary.map(|m| m.spawn(spawner));
|
|
let fists = WeaponDto::FISTS.spawn(spawner);
|
|
let kick = WeaponDto::KICK.spawn(spawner);
|
|
|
|
weapons = Some(Weapons {
|
|
primary,
|
|
secondary,
|
|
melee,
|
|
temporary,
|
|
kick,
|
|
fists,
|
|
});
|
|
});
|
|
if let Some(weapons) = weapons {
|
|
commands.insert(weapons);
|
|
}
|
|
|
|
let mut armour = None;
|
|
commands.with_children(|spawner| {
|
|
let head = self.armour.helmet.map(|a| a.spawn(spawner));
|
|
let torso = self.armour.body.map(|a| a.spawn(spawner));
|
|
let legs = self.armour.pants.map(|a| a.spawn(spawner));
|
|
let hands = self.armour.gloves.map(|a| a.spawn(spawner));
|
|
let feet = self.armour.boots.map(|a| a.spawn(spawner));
|
|
|
|
armour = Some(PlayerArmour {
|
|
torso,
|
|
head,
|
|
legs,
|
|
feet,
|
|
hands,
|
|
})
|
|
});
|
|
|
|
if let Some(armour) = armour {
|
|
commands.insert(armour);
|
|
}
|
|
|
|
commands
|
|
}
|
|
}
|