2020-06-29 16:22:19 +00:00
|
|
|
use lazy_static::lazy_static;
|
2020-07-03 19:40:37 +00:00
|
|
|
use std::{
|
|
|
|
collections::{HashMap, HashSet},
|
|
|
|
hash::Hash,
|
|
|
|
};
|
2020-06-29 16:22:19 +00:00
|
|
|
use tracing::warn;
|
|
|
|
|
2020-07-03 19:40:37 +00:00
|
|
|
lazy_static! {
|
|
|
|
// Determines the skills that comprise each skill group - this data is used to determine
|
|
|
|
// which of a player's skill groups a particular skill should be added to when a skill unlock
|
|
|
|
// is requested. TODO: Externalise this data in a RON file for ease of modification
|
|
|
|
pub static ref SKILL_GROUP_DEFS: HashMap<SkillGroupType, HashSet<Skill>> = {
|
|
|
|
let mut defs = HashMap::new();
|
|
|
|
defs.insert(SkillGroupType::T1, [ Skill::TestT1Skill1,
|
|
|
|
Skill::TestT1Skill2,
|
|
|
|
Skill::TestT1Skill3,
|
|
|
|
Skill::TestT1Skill4,
|
|
|
|
Skill::TestT1Skill5]
|
|
|
|
.iter().cloned().collect::<HashSet<Skill>>());
|
|
|
|
|
|
|
|
defs.insert(SkillGroupType::Swords, [ Skill::TestSwordSkill1,
|
|
|
|
Skill::TestSwordSkill2,
|
|
|
|
Skill::TestSwordSkill3]
|
|
|
|
.iter().cloned().collect::<HashSet<Skill>>());
|
|
|
|
|
|
|
|
defs.insert(SkillGroupType::Axes, [ Skill::TestAxeSkill1,
|
|
|
|
Skill::TestAxeSkill2,
|
|
|
|
Skill::TestAxeSkill3]
|
|
|
|
.iter().cloned().collect::<HashSet<Skill>>());
|
|
|
|
|
|
|
|
defs
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-29 16:22:19 +00:00
|
|
|
/// Represents a skill that a player can unlock, that either grants them some
|
|
|
|
/// kind of active ability, or a passive effect etc. Obviously because this is
|
|
|
|
/// an enum it doesn't describe what the skill actually -does-, this will be
|
|
|
|
/// handled by dedicated ECS systems.
|
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
pub enum Skill {
|
|
|
|
TestT1Skill1,
|
|
|
|
TestT1Skill2,
|
|
|
|
TestT1Skill3,
|
|
|
|
TestT1Skill4,
|
|
|
|
TestT1Skill5,
|
|
|
|
TestSwordSkill1,
|
|
|
|
TestSwordSkill2,
|
|
|
|
TestSwordSkill3,
|
|
|
|
TestAxeSkill1,
|
|
|
|
TestAxeSkill2,
|
|
|
|
TestAxeSkill3,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
pub enum SkillGroupType {
|
|
|
|
T1,
|
|
|
|
Swords,
|
|
|
|
Axes,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A group of skills that have been unlocked by a player. Each skill group has
|
|
|
|
/// independent exp and skill points which are used to unlock skills in that
|
|
|
|
/// skill group.
|
2020-07-03 19:40:37 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
2020-06-29 16:22:19 +00:00
|
|
|
pub struct SkillGroup {
|
2020-07-03 19:40:37 +00:00
|
|
|
pub skill_group_type: SkillGroupType,
|
2020-06-29 16:22:19 +00:00
|
|
|
pub exp: u32,
|
|
|
|
pub available_sp: u8,
|
|
|
|
}
|
|
|
|
|
2020-07-03 19:40:37 +00:00
|
|
|
impl SkillGroup {
|
|
|
|
fn new(skill_group_type: SkillGroupType) -> SkillGroup {
|
|
|
|
SkillGroup {
|
|
|
|
skill_group_type,
|
2020-06-29 16:22:19 +00:00
|
|
|
exp: 0,
|
|
|
|
available_sp: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 19:40:37 +00:00
|
|
|
/// Contains all of a player's skill groups and skills. Provides methods for
|
|
|
|
/// manipulating assigned skills and skill groups including unlocking skills,
|
|
|
|
/// refunding skills etc.
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
|
2020-06-29 16:22:19 +00:00
|
|
|
pub struct SkillSet {
|
2020-07-03 19:40:37 +00:00
|
|
|
pub skill_groups: Vec<SkillGroup>,
|
|
|
|
pub skills: HashSet<Skill>,
|
2020-06-29 16:22:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for SkillSet {
|
|
|
|
/// Instantiate a new skill set with the default skill groups with no
|
|
|
|
/// unlocked skills in them - used when adding a skill set to a new
|
|
|
|
/// player
|
|
|
|
fn default() -> Self {
|
2020-07-03 19:40:37 +00:00
|
|
|
// TODO: Default skill groups for new players?
|
|
|
|
Self {
|
|
|
|
skill_groups: Vec::new(),
|
|
|
|
skills: HashSet::new(),
|
|
|
|
}
|
2020-06-29 16:22:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SkillSet {
|
2020-07-03 19:40:37 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
skill_groups: Vec::new(),
|
|
|
|
skills: HashSet::new(),
|
|
|
|
}
|
2020-06-29 16:22:19 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 19:40:37 +00:00
|
|
|
// TODO: Game design to determine how skill groups are unlocked
|
|
|
|
/// Unlocks a skill group for a player, starting with 0 exp and 0 sp
|
|
|
|
pub fn unlock_skill_group(&mut self, skill_group_type: SkillGroupType) {
|
|
|
|
if !self
|
|
|
|
.skill_groups
|
|
|
|
.iter()
|
|
|
|
.any(|x| x.skill_group_type == skill_group_type)
|
|
|
|
{
|
|
|
|
self.skill_groups.push(SkillGroup::new(skill_group_type));
|
|
|
|
} else {
|
|
|
|
warn!("Tried to unlock already known skill group");
|
|
|
|
}
|
|
|
|
}
|
2020-06-29 16:22:19 +00:00
|
|
|
|
2020-07-03 19:40:37 +00:00
|
|
|
/// Unlocks a skill for a player, assuming they have the relevant Skill
|
|
|
|
/// Group unlocked and available SP in that skill group
|
|
|
|
pub fn unlock_skill(&mut self, skill: Skill) {
|
|
|
|
if self.skills.contains(&skill) {
|
|
|
|
if let Some(skill_group_type) = SkillSet::get_skill_group_type_for_skill(&skill) {
|
|
|
|
if let Some(mut skill_group) = self
|
|
|
|
.skill_groups
|
|
|
|
.iter_mut()
|
|
|
|
.find(|x| x.skill_group_type == skill_group_type)
|
|
|
|
{
|
2020-06-29 16:22:19 +00:00
|
|
|
if skill_group.available_sp > 0 {
|
|
|
|
skill_group.available_sp -= 1;
|
|
|
|
} else {
|
|
|
|
warn!("Tried to unlock skill for skill group with no available SP");
|
|
|
|
}
|
|
|
|
} else {
|
2020-07-03 19:40:37 +00:00
|
|
|
warn!("Tried to unlock skill for a skill group that player does not have");
|
2020-06-29 16:22:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-07-03 19:40:37 +00:00
|
|
|
warn!(
|
|
|
|
?skill,
|
|
|
|
"Tried to unlock skill that does not exist in any skill group!"
|
|
|
|
);
|
2020-06-29 16:22:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-07-03 19:40:37 +00:00
|
|
|
warn!("Tried to unlock already unlocked skill");
|
2020-06-29 16:22:19 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-03 19:40:37 +00:00
|
|
|
|
|
|
|
/// Removes a skill for a player and refunds 1 SP in the relevant Skill
|
|
|
|
/// Group
|
|
|
|
pub fn refund_skill(&mut self, skill: Skill) {
|
|
|
|
if !self.skills.contains(&skill) {
|
|
|
|
if let Some(skill_group_type) = SkillSet::get_skill_group_type_for_skill(&skill) {
|
|
|
|
if let Some(mut skill_group) = self
|
|
|
|
.skill_groups
|
|
|
|
.iter_mut()
|
|
|
|
.find(|x| x.skill_group_type == skill_group_type)
|
|
|
|
{
|
|
|
|
skill_group.available_sp += 1;
|
|
|
|
} else {
|
|
|
|
warn!("Tried to refund skill for a skill group that player does not have");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
warn!(
|
|
|
|
?skill,
|
|
|
|
"Tried to refund skill that does not exist in any skill group"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
warn!("Tried to refund skill that has not been unlocked");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the skill group type for a skill from the static skill group
|
|
|
|
/// definitions
|
|
|
|
fn get_skill_group_type_for_skill(skill: &Skill) -> Option<SkillGroupType> {
|
|
|
|
SKILL_GROUP_DEFS.iter().find_map(|(key, val)| {
|
|
|
|
if val.contains(&skill) {
|
|
|
|
Some(*key)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-06-29 16:22:19 +00:00
|
|
|
}
|