166 lines
4.9 KiB
C#
166 lines
4.9 KiB
C#
using System.Collections.Generic;
|
|
using BlueWater.Interfaces;
|
|
using BlueWater.Uis;
|
|
using UnityEngine;
|
|
|
|
namespace BlueWater
|
|
{
|
|
public class CombatSkillController : MonoBehaviour, ISkillHandler
|
|
{
|
|
// Variables
|
|
#region Variables
|
|
|
|
// Components
|
|
private IDashable _dashable;
|
|
private IComboAttackable _comboAttackable;
|
|
private IStunnable _stunnable;
|
|
|
|
// Variables
|
|
[field: SerializeField]
|
|
public List<BaseSkill> Skills { get; private set; }
|
|
public Dictionary<string, BaseSkill> SkillInstances { get; private set; }
|
|
public BaseSkill CurrentActivatingSkill { get; private set; }
|
|
|
|
public bool IsSkillEnabled { get; private set; } = true;
|
|
public bool IsActivatingSkill { get; private set; }
|
|
|
|
#endregion
|
|
|
|
// Unity events
|
|
#region Unity events
|
|
|
|
private void Awake()
|
|
{
|
|
InitializeComponents();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
Initialize();
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (SkillInstances == null) return;
|
|
|
|
foreach (var element in SkillInstances.Values)
|
|
{
|
|
if (!element) continue;
|
|
|
|
Destroy(element.gameObject);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
// Initialize methods
|
|
#region Initialize methods
|
|
|
|
private void InitializeComponents()
|
|
{
|
|
_dashable = GetComponent<IDashable>();
|
|
_comboAttackable = GetComponent<IComboAttackable>();
|
|
_stunnable = GetComponent<IStunnable>();
|
|
}
|
|
|
|
private void Initialize()
|
|
{
|
|
SkillInstances = new Dictionary<string, BaseSkill>(Skills.Count);
|
|
|
|
foreach (var element in Skills)
|
|
{
|
|
var newSkill = Instantiate(element);
|
|
newSkill.Initialize(gameObject);
|
|
SkillInstances.Add(newSkill.SkillName, newSkill);
|
|
}
|
|
|
|
CurrentActivatingSkill = SkillInstances[Skills[0].SkillName];
|
|
CombatUiManager.Instance.CombatSkillUi.ResetSkillUi();
|
|
}
|
|
|
|
#endregion
|
|
|
|
// Methods
|
|
#region Methods
|
|
|
|
public void EnableSkill() => IsSkillEnabled = true;
|
|
public void DisableSkill() => IsSkillEnabled = false;
|
|
|
|
public bool HasSkill(string skillName = null)
|
|
{
|
|
if (string.IsNullOrEmpty(skillName)) skillName = CurrentActivatingSkill.SkillName;
|
|
|
|
if (SkillInstances.ContainsKey(skillName)) return true;
|
|
|
|
Debug.Log($"{skillName}(이)라는 스킬을 찾을 수 없습니다.");
|
|
return false;
|
|
}
|
|
|
|
public bool CanSkill(string skillName = null)
|
|
{
|
|
if (string.IsNullOrEmpty(skillName)) skillName = CurrentActivatingSkill.SkillName;
|
|
|
|
if (!IsSkillEnabled || IsActivatingSkill) return false;
|
|
|
|
var isDashing = _dashable?.IsDashing ?? false;
|
|
var isAttacking = _comboAttackable?.CurrentComboAttackCount > 0;
|
|
var isStunned = _stunnable?.IsStunned ?? false;
|
|
|
|
if (isDashing || isAttacking || isStunned) return false;
|
|
|
|
var canSkill = SkillInstances[skillName].CanSkill();
|
|
if (!canSkill)
|
|
{
|
|
CombatUiManager.Instance.CombatSkillUi.PunchAnimation();
|
|
}
|
|
|
|
return canSkill;
|
|
}
|
|
|
|
/// <summary>
|
|
/// HasSkill()만 확인하고 스킬을 강제로 사용
|
|
/// </summary>
|
|
public void ActivateSkill(string skillName = null)
|
|
{
|
|
if (string.IsNullOrEmpty(skillName)) skillName = CurrentActivatingSkill.SkillName;
|
|
|
|
if (!HasSkill(skillName)) return;
|
|
|
|
IsSkillEnabled = false;
|
|
IsActivatingSkill = true;
|
|
CurrentActivatingSkill = SkillInstances[skillName];
|
|
|
|
CurrentActivatingSkill.ActivateSkill(EndSkill);
|
|
}
|
|
|
|
/// <summary>
|
|
/// HasSkill(), CanSkill()를 모두 확인하고 스킬을 사용하기까지 모든 과정
|
|
/// </summary>
|
|
public void TryActivateSkill(string skillName = null)
|
|
{
|
|
if (string.IsNullOrEmpty(skillName)) skillName = CurrentActivatingSkill.SkillName;
|
|
|
|
if (!CanSkill(skillName)) return;
|
|
|
|
ActivateSkill(skillName);
|
|
}
|
|
|
|
private void EndSkill()
|
|
{
|
|
IsSkillEnabled = true;
|
|
IsActivatingSkill = false;
|
|
|
|
CombatUiManager.Instance.CombatSkillUi.CoolDown(CurrentActivatingSkill.Cooldown);
|
|
}
|
|
|
|
public void StopAllCoroutine()
|
|
{
|
|
foreach (var element in SkillInstances.Values)
|
|
{
|
|
element.StopAllCoroutines();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |