首页 > 代码库 > 小妖精的完美游戏教室——技能系统
小妖精的完美游戏教室——技能系统
//================================================================
//
// Copyright (C) 2017 Team Saluka
// All Rights Reserved
//
// Author:小妖精Balous
//
//================================================================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Saruka
{
/// <summary>
/// 技能系统
/// </summary>
public class SkillSystem
{
/// <summary>
/// 拥有本技能系统的生物
/// </summary>
Creature owner;
/// <summary>
/// 是否进入后摇
/// </summary>
bool isAfterComplete;
/// <summary>
/// 技能集合
/// </summary>
public Skill[] skillList;
/// <summary>
/// 构造器
/// </summary>
/// <param name="owner">拥有本技能系统的生物</param>
public SkillSystem(Creature _owner)
{
owner = _owner;
}
/// <summary>
/// 正在施放的技能
/// </summary>
public Skill skillOnCasting
{
private set;
get;
}
/// <summary>
/// 取消正在施放的技能
/// </summary>
public void CancelSkill()
{
if (skillOnCasting != null)
{
skillOnCasting.Exit(owner);
//技能开始冷却
skillOnCasting.coldDownTime = skillOnCasting.totalColdDownTime;
skillOnCasting = null;
}
}
/// <summary>
/// 施放技能
/// </summary>
/// <param name="index">要施放的技能在技能集合中的索引</param>
/// <param name="force">是否强制施放</param>
public void CastSkill(int index, bool isForce = false)
{
//判断生物是否处于打断状态
if (owner.interrupt > 0) return;
//判断冷却时间
if (skillList[index].coldDownTime > 0) return;
//判断魔法值是否足够
if (owner.mana < skillList[index].manaCost) return;
//判断是否有技能在施放中
if (skillOnCasting != null)
{
if (isForce)
{
skillOnCasting.Exit(owner);
//技能开始冷却
skillOnCasting.coldDownTime = skillOnCasting.totalColdDownTime;
}
else return;
}
skillOnCasting = skillList[index];
//消耗魔法值
owner.mana -= skillOnCasting.manaCost;
//记录技能施放时间
skillOnCasting.castingTime = skillOnCasting.totalCastingTime;
//初始化后摇
isAfterComplete = false;
skillOnCasting.afterCompleteTime = skillOnCasting.totalAfterCompleteTime;
//调用Enter
skillOnCasting.Enter(owner);
}
/// <summary>
/// 刷新技能状态,在Update中调用
/// </summary>
public void Refresh()
{
//刷新技能冷却时间
if (skillList != null) foreach (Skill skill in skillList) skill.coldDownTime -= Time.deltaTime;
//如果有技能正在施放
if (skillOnCasting != null)
{
//如果技能被打断
if (owner.interrupt > 0)
{
skillOnCasting.Exit(owner);
//技能开始冷却
skillOnCasting.coldDownTime = skillOnCasting.totalColdDownTime;
skillOnCasting = null;
return;
}
//如果没有进入后摇
if (!isAfterComplete)
{
//刷新技能施放时间
skillOnCasting.castingTime -= Time.deltaTime;
//如果技能施放完毕
if (skillOnCasting.castingTime <= 0.0f)
{
skillOnCasting.Complete(owner);
isAfterComplete = true;
}
//如果技能没有施放完毕
else skillOnCasting.Execute(owner);
}
//如果进入后摇
if (isAfterComplete)
{
//刷新后摇时间
skillOnCasting.afterCompleteTime -= Time.deltaTime;
//如果后摇结束
if (skillOnCasting.afterCompleteTime <= 0.0f)
{
skillOnCasting.Exit(owner);
//技能开始冷却
skillOnCasting.coldDownTime = skillOnCasting.totalColdDownTime;
skillOnCasting = null;
}
//如果后摇没有结束
else skillOnCasting.AfterComplete(owner);
}
}
}
}
/// <summary>
/// 技能
/// </summary>
public abstract class Skill
{
public Skill() { }
public Skill(int _level, float _totalColdDownTime, float _totalCastingTime, float _totalAfterCompleteTime, int _manaCost)
{
level = _level;
totalColdDownTime = _totalColdDownTime;
totalCastingTime = _totalCastingTime;
totalAfterCompleteTime = _totalAfterCompleteTime;
manaCost = _manaCost;
}
/// <summary>
/// 技能等级
/// </summary>
public int level;
/// <summary>
/// 全部冷却时间
/// </summary>
public float totalColdDownTime;
/// <summary>
/// 当前冷却时间
/// </summary>
public float coldDownTime
{
set
{
if (value < 0.0f) _coldDownTime = 0.0f;
else if (value > totalColdDownTime) _coldDownTime = totalColdDownTime;
else _coldDownTime = value;
}
get{ return _coldDownTime; }
}
private float _coldDownTime;
/// <summary>
/// 当前后摇时间
/// </summary>
public float afterCompleteTime
{
set;
get;
}
/// <summary>
/// 全部后摇时间
/// </summary>
public float totalAfterCompleteTime = 0;
/// <summary>
/// 魔法值消耗
/// </summary>
public int manaCost;
/// <summary>
/// 全部施放时间
/// </summary>
public float totalCastingTime;
/// <summary>
/// 当前施放时间
/// </summary>
public float castingTime
{
set;
get;
}
public virtual void Enter(Creature owner) { }
public virtual void Execute(Creature owner) { }
public abstract void Complete(Creature owner);
public virtual void AfterComplete(Creature owner) { }
public virtual void Exit(Creature owner) { }
}
}
小妖精的完美游戏教室——技能系统