如何使我的攻击组合系统更具模块化?

时间:2018-07-25 22:28:45

标签: unity3d

加强节奏。我充实了处理攻击连击的基本方法。我正在寻找一种更好的方法来处理模块性,以增加新的动作并使逻辑更加简洁。

public class Attack : AbstractBehavior
{

public bool attack1;
public bool attack2;
public bool attack3;

public int currentAttack = 0;
public float lastAttackTime;
public float attackDelay = 0.2f;

private void FixedUpdate()
{
    var canAttack = inputState.GetButtonValue(InputButtons[0]);
    var holdTime = inputState.GetButtonHoldtime(InputButtons[0]);

    if (canAttack && currentAttack == 0 && holdTime < 0.1f)
    {
        attack1 = true;
        currentAttack++;
        lastAttackTime = Time.time;
    }

    if (canAttack && currentAttack == 1 && holdTime < 0.1f && Time.time - lastAttackTime > attackDelay)
    {
        attack1 = false;
        attack2 = true;
        currentAttack++;
        lastAttackTime = Time.time;
    }

    if (canAttack && currentAttack == 2 && holdTime < 0.1f && Time.time - lastAttackTime > attackDelay)
    {
        attack2 = false;
        attack3 = true;
    }
}

1 个答案:

答案 0 :(得分:1)

我只使用AttackComboUnits的“树”,在其中嵌套之后的内容。然后,在主Attack脚本中,您拥有初始攻击组合单位和当前活动的组合单位。

注意:这只是一个概念,脚本可能无法正常工作,因为它们是在Notepad ++中编写的,而我尚未对其进行测试。

public class AttackComboUnit : MonoBehaviour
{
    [SerializeField]
    privaet bool baseComboUnit;
    [SerializeField]
    private AttackComboUnit nextUnit;
    [SerializeField]
    private float maxDelay;

    private Animator animator;
    private float enableTime;





    private void OnEnable()
    {
        enableTime = Time.time;
    }




    public AttackComboUnit Attack()
    {
        // Player didn't use attack for too long and this is not base attack unit, so delay must be applied
        if(!baseComboUnit)
        {
            if(Time.time - enableTime > maxDelay)
                return null;
        }


        animator.Play("myAwesomeComboSkill");

        // // // // // // // // // // // // //
        // Do some damage or something else //
        // // // // // // // // // // // // //

        return nextUnit;
    }
}





public class Attack : AbstractBehaviour
{
    [SerializeField]
    private AttackComboUnit initialAttackUnit;

    private AttackComboUnit currentAttackUnit;





    private void FixedUpdate()
    {
        if(canAttack && holdTime < 0.1f)
        {
            AttackComboUnit comboUnit = currentAttackUnit.Attack();
            SetAttackUnit(comboUnit);
        }
    }

    private void Start()
    {
        currentAttackUnit = initialAttackUnit;
    }





    private void SetAttackUnit(AttackComboUnit attackComboUnit)
    {
        currentAttackUnit.enabled = false;
        currentAttackUnit = attackComboUnit;
        attackComboUnit.enabled = true;
    }
}

现在,只需将所有AttackComboUnit放在GameObject上,蚂蚁就可以分配之后的内容。调整延迟。从启用脚本开始算起的延迟。禁用除初始攻击以外的所有攻击。

您还可以为应该播放的动画名称添加字段,以使您的AttackComboUnit脚本更加通用。