You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

92 lines
2.8 KiB

using System;
using UnityEngine;
namespace Skill_V2
{
public class Skill : Entity
{
[SerializeField] uint id;
[SerializeField] StateHandler stateHandler;
[SerializeField] SkillState[] sequences;
Entity caster;
int currentSequenceIndex = -1;
float elapsedCoolTime;
public SkillData Data { get; private set; }
public SkillState CurrentSequence => currentSequenceIndex >= 0 ? sequences[currentSequenceIndex] : null;
public bool IsInCoolTime => elapsedCoolTime < Data.DefaultCoolTime;
public float CoolTimeRemainTime => Mathf.Clamp(Data.DefaultCoolTime - elapsedCoolTime, 0, Data.DefaultCoolTime);
public float ElapsedCoolTime => Mathf.Clamp(elapsedCoolTime, 0, Data.DefaultCoolTime);
public float ElapsedCoolTimeRate { get; private set; }
private void Awake()
{
if (SkillDataGroup.Instance.TryGetSkillData(id, out SkillData skillData))
{
Data = skillData;
for (int i = 0; i < sequences.Length; ++i)
{
sequences[i].SetOwner(this);
}
}
else
{
Logger.LogError("Skill id " + id + " can't find skilldata");
}
}
private void Update()
{
if (CurrentSequence != null && CurrentSequence.IsDone)
{
currentSequenceIndex++;
if (currentSequenceIndex < sequences.Length)
{
stateHandler.ChangeState(sequences[currentSequenceIndex]);
}
else
{
currentSequenceIndex = -1;
stateHandler.ChangeState(null);
caster = null;
gameObject.SetActive(false);
}
}
}
public void UpdateSkillCoolTime()
{
if (!IsInCoolTime) return;
elapsedCoolTime += Time.fixedUnscaledDeltaTime;
ElapsedCoolTimeRate = Mathf.Clamp01(elapsedCoolTime / Data.DefaultCoolTime);
}
public void Cast(Entity caster)
{
this.caster = caster;
currentSequenceIndex = 0;
transform.position = caster.CenterPivot;
ChangeLookDirection(caster.LookDirection);
gameObject.SetActive(true);
stateHandler.ChangeState(CurrentSequence);
}
public void StartCoolTime()
{
elapsedCoolTime = 0f;
}
public void Stop()
{
CurrentSequence?.Stop();
currentSequenceIndex = -1;
stateHandler?.ChangeState(null);
caster = null;
gameObject.SetActive(false);
}
}
}