Encounter / Assets / Scripts / EntityStateManagement / EntitySkills.cs
EntitySkills.cs
Raw
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices.WindowsRuntime;
using UnityEngine;

/// <summary>
/// Handles logical functionality of unlocked skills, available skill, and shown skills
/// </summary>
public class EntitySkills
{
    private Entity entityStored;

    private List<SkillSO> nonIntegratedSkills;              // Skills that the player has not used a skill point on
    private List<Skill> integratedSkills;                   // Skills that the player has paid skill points for
    private List<Skill> enabledSkills;                      // Skills that are capable of being enabled/disabled, enabled
    private List<Skill> disabledSkills;                     // Skills that are capable of being enabled/disabled, disabled

    public Guid UniqueId { get; private set; }

    public EntitySkills(Entity entityStored, List<SkillSO> skillsInTree)
    {
        this.entityStored           = entityStored;
        this.nonIntegratedSkills    = new List<SkillSO>(skillsInTree);
        this.integratedSkills       = new List<Skill>();
        this.enabledSkills          = new List<Skill>();
        this.disabledSkills         = new List<Skill>();

        this.UniqueId = Guid.NewGuid();
    }

    public bool TryAddSkill(SkillSO skillData)
    {
        if (skillData.RequiredSkills.Count > 0)
        {
            foreach (SkillSO skill in skillData.RequiredSkills)
            {
                // For dynamic skills in other trees, if the requirement isn't in the given list, simply continue and check other required skills
                if (!nonIntegratedSkills.Contains(skill))
                    continue;
                else if (nonIntegratedSkills.Contains(skill))
                    return false;
            }
        }

        // Removes from the non integrated group and adds to the integrated group
        Skill skillObject = new Skill(skillData, entityStored);
        integratedSkills.Add(skillObject);

        if (skillData.IsEnablingSkill)
            enabledSkills.Add(skillObject);

        nonIntegratedSkills.Remove(skillData);

        return true;
    }
    /// <summary>
    /// Retrieval for the skills that have not yet been activated
    /// </summary>
    /// <returns></returns>
    public List<SkillSO> GetNonActivatedSkills()
        => nonIntegratedSkills;
    /// <summary>
    /// Retrieval for the list of skills that have been activated
    /// </summary>
    /// <returns></returns>
    public List<Skill> GetActivatedSkills()
        => integratedSkills;
    /// <summary>
    /// Retrieval for the skills that are capable of being enabled/disabled and this list represents the enabled
    /// </summary>
    /// <returns></returns>
    public List<Skill> GetEnabledSkills()
        => enabledSkills;
    /// <summary>
    /// Retrieval for the skills that are capable of being enabled/disabled and this list represents the disabled
    /// </summary>
    /// <returns></returns>
    public List<Skill> GetDisabledSkills()
        => disabledSkills;

    public bool ContainsActivated(SkillSO skill)
    {
        foreach (Skill activeSkill in integratedSkills)
        {
            if (activeSkill.GetName().Equals(skill.SkillName))
                return true;
        }

        return false;
    }

    public void DeactivateSkill(SkillSO skill)
    {
        if (!skill.IsEnablingSkill)
            throw new InvalidOperationException();

        Skill skillToRemove = null;
        foreach (Skill enabledSkill in enabledSkills)
        {
            if (enabledSkill.GetName().Equals(skill.SkillName))
            {
                skillToRemove = enabledSkill;
                break;
            }
        }

        if (skillToRemove != null)
        {
            skillToRemove.DeactivateSkill();
            disabledSkills.Add(skillToRemove);
            enabledSkills.Remove(skillToRemove);
        }
        else
            throw new DataMisalignedException();
    }

    public void ReactivateSkill(SkillSO skill)
    {
        if (!skill.IsEnablingSkill)
            throw new InvalidOperationException();

        Skill skillToRemove = null;
        foreach (Skill disabledSkill in disabledSkills)
        {
            if (disabledSkill.GetName().Equals(skill.SkillName))
            {
                skillToRemove = disabledSkill;
                break;
            }
        }

        if (skillToRemove != null)
        {
            skillToRemove.ActivateSkill();
            enabledSkills.Add(skillToRemove);
            disabledSkills.Remove(skillToRemove);
        }
        else
            throw new DataMisalignedException();
    }

    public bool IsSkillEnabled(SkillSO skill)
    {
        // If the skill can't be enabled, something went very wrong in cycling through the data
        if (!skill.IsEnablingSkill)
            throw new InvalidOperationException();

        foreach (Skill enabledSkill in enabledSkills)
        {
            if (enabledSkill.GetName().Equals(skill.SkillName))
                return true;
        }

        return false;
    }
}