Пример #1
0
    void Update()
    {
        //倒计时
        if (motionTimer > -rollCoolingTime)
        {
            motionTimer -= Time.deltaTime;
        }

        //死亡判定
        if (HP <= 0)
        {
            Death();
        }
        else
        {
            //玩家
            if (CompareTag("Player"))
            {
                //特殊动作
                if (motionTimer <= 0)
                {
                    if (Input.GetKeyDown(KeyCode.Space) && motionTimer <= -rollCoolingTime)
                    {
                        status      = 2;
                        motionTimer = rollTime;
                        speed       = run;
                        direction   = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
                        //GetComponent<CapsuleCollider2D>().enabled = false;
                    }
                    if (motionTimer <= 0)
                    {
                        status = 0;
                    }
                }
                else if (status == 2)
                {
                    transform.Translate(direction * speed * Time.deltaTime);
                    //if (motionTimer - 2 * Time.deltaTime <= 0)
                    //{
                    //    //GetComponent<CapsuleCollider2D>().enabled = true;
                    //}
                }

                //正常动作
                if (status == 0)
                {
                    //移动控制
                    speed     = walk;
                    direction = GetDirection();
                    if (direction.x < 0)
                    {
                        sr.flipX = true;
                    }
                    else if (direction.x > 0)
                    {
                        sr.flipX = false;
                    }
                    transform.Translate(direction * speed * Time.deltaTime);

                    //激发武器
                    if (arsenal.CurrentWeapon() != null && (arsenal.CurrentWeapon().GetInfo("剩余冷却时间") <= 0 || !arsenal.CurrentWeapon().IsMelee()))
                    {
                        if (lastFire > 0)
                        {
                            arsenal.SwitchWeapon(lastFire);
                            lastFire = -1;
                        }

                        if (Input.GetButton("Fire1") && gameObject.layer == LayerMask.NameToLayer("Player"))
                        {
                            float _cost = arsenal.Attack();
                            if (_cost > 0)
                            {
                                MP -= _cost;
                            }
                            lastFire = -1;
                        }
                        else if (Input.GetButton("Fire2") && gameObject.layer == LayerMask.NameToLayer("Player"))
                        {
                            float _cost = arsenal.MeleeAttack();
                            if (_cost > 0)
                            {
                                MP -= _cost;
                            }
                            lastFire = arsenal.tempIndex;
                        }
                    }

                    //丢弃物品
                    if (Input.GetKeyDown(KeyCode.Q))
                    {
                        arsenal.ThrowAway();
                    }
                    //捡起物品
                    if (Input.GetKeyDown(KeyCode.E))
                    {
                        arsenal.PickUp(GetClosestWeapon());
                    }
                    //切换武器
                    if (Input.GetAxis("Mouse ScrollWheel") != 0)
                    {
                        arsenal.SwitchWeapon();
                    }
                }
            }
            //随从
            else if (CompareTag("Retinue"))
            {
                if (status != 0)
                {
                    if (direction.x < 0)
                    {
                        sr.flipX = true;
                    }
                    else if (direction.x > 0)
                    {
                        sr.flipX = false;
                    }
                    transform.Translate(direction * speed * Time.deltaTime);
                }
                if (Input.GetKey(KeyCode.I))
                {
                    print("转移物品到当前角色");
                    TransforAllItemsToPlayer();
                }
                bool       hasEnemy;
                GameObject closestEnemy = WeaponBase.FindClosest(transform, WeaponBase.ENEMY);
                if (closestEnemy != null)
                {
                    hasEnemy = true;
                    target   = closestEnemy;
                }
                else
                {
                    hasEnemy = false;
                    target   = GameObject.FindGameObjectWithTag("Player");
                    if (target == null)
                    {
                        GameObject.Find("GameManager").GetComponent <GameManager>().ChangeCharacter();
                        target = GameObject.FindGameObjectWithTag("Player");
                    }
                }

                if (target == null)
                {
                    target = gameObject;
                }
                //动作准备
                if ((target.transform.position - transform.position).sqrMagnitude > 25)
                {
                    status = -1;
                    if (target.CompareTag("Enemy") && arsenal.CurrentWeapon() != null && arsenal.CurrentWeapon().IsMelee())
                    {
                        speed = walk;
                    }
                    else
                    {
                        speed = target.CompareTag("Player") ? walk : sneak;
                    }
                    //靠近目标
                    direction = (target.transform.position - transform.position).normalized;
                }
                else if (motionTimer <= 0 && (target.transform.position - transform.position).sqrMagnitude < 3)
                {
                    status = -1;
                    speed  = target.CompareTag("Player") ? walk : sneak;
                    //远离目标
                    direction = -(target.transform.position - transform.position).normalized;
                    if (direction == Vector3.zero)
                    {
                        direction = Random.insideUnitCircle.normalized;
                    }
                }
                else if (motionTimer <= 0)
                {
                    bool willMove = Random.Range(0f, 1f) < moveAgility;
                    if (willMove)
                    {
                        motionTimer = Random.Range(0.2f, 0.8f);
                        status      = -1;
                        direction   = Random.insideUnitCircle;
                    }
                    else
                    {
                        motionTimer = Random.Range(0.7f, 1f);
                        status      = 0;
                    }
                }

                //开火
                if (hasEnemy == true)
                {
                    if (arsenal.CurrentWeapon() != null && arsenal.CurrentWeapon().IsMelee())
                    {
                        if ((target.transform.position - transform.position).sqrMagnitude < Mathf.Sqrt(meleeAttackDistance))
                        {
                            if (Random.Range(0f, 1f) < fireAgility)
                            {
                                Attack();
                            }
                        }
                    }
                    else
                    {
                        if (Random.Range(0f, 1f) < fireAgility)
                        {
                            Attack();
                        }
                    }
                }
            }

            //恢复颜色
            if (injureTimer > 0)
            {
                injureTimer -= Time.deltaTime;
            }
            else if (GetComponent <SpriteRenderer>().color != Color.white)
            {
                GetComponent <SpriteRenderer>().color = Color.white;
            }
        }
    }