Exemplo n.º 1
0
    private void Update()
    {
        GetComponent <DAnimator>().attacking = false;

        RandomMinMaxDistance();
        if (owner == null)
        {
            owner = DGameSystem.player;
        }
        if (owner == null)
        {
            return;
        }

        float distanceToOwner = Vector3.Distance(transform.position, owner.transform.position);

        enemy = enemyFinder.FindEnemy(owner, MAX_DISTANCE);

        if (distanceToOwner < MIN_DISTANCE)
        {
            GetComponent <DMovement>().enabled         = true;
            GetComponent <DMovementExecutor>().enabled = true;
            state = "stand";
        }
        else if (distanceToOwner > MAX_DISTANCE)
        {
            GetComponent <DMovement>().enabled         = false;
            GetComponent <DMovementExecutor>().enabled = false;
            vectorToTarget = owner.transform.position - transform.position;
            vectorToTarget = vectorToTarget * data.speed * 1.5f / distanceToOwner;
            GetComponent <Rigidbody2D>().velocity = vectorToTarget;
            state     = "follow";
            direction = DCommonUtils.GetLeftRightFacingToTarget(transform, owner.transform);
            if (direction != "vertical")
            {
                GetComponent <DAnimator>().spritesheet = DCommonUtils.GetSpriteSheet(data, "run_" + direction);
            }
        }

        if (state != "follow")
        {
            enemy = enemyFinder.FindEnemy(owner, MAX_DISTANCE);
            if (enemy != null)
            {
                if (isAttacking)
                {
                    Attack();
                    attackCount -= Time.deltaTime;
                    if (attackCount < 0)
                    {
                        isAttacking = false;
                        attackCount = ATTACK_STOP_DURATION;
                    }
                }
                else
                {
                    GetComponent <DMovement>().enabled         = true;
                    GetComponent <DMovementExecutor>().enabled = true;
                    attackCount -= Time.deltaTime;
                    if (attackCount < 0)
                    {
                        isAttacking = true;
                        attackCount = ATTACK_DURATION;
                    }
                }
            }
            else
            {
                Debug.Log("not found enemy!");
                GetComponent <DMovement>().enabled         = true;
                GetComponent <DMovementExecutor>().enabled = true;
            }
        }

        void Attack()
        {
            GetComponent <DMovement>().enabled         = false;
            GetComponent <DMovementExecutor>().enabled = false;

            float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position);

            if (distanceToEnemy > data.attackRange)
            {
                vectorToTarget = enemy.transform.position - transform.position;
                vectorToTarget = vectorToTarget * data.speed * 1.5f / distanceToEnemy;
                GetComponent <Rigidbody2D>().velocity = vectorToTarget;
                direction = DCommonUtils.GetLeftRightFacingToTarget(transform, enemy.transform);
                GetComponent <DAnimator>().attacking   = true;
                GetComponent <DAnimator>().spritesheet = DCommonUtils.GetSpriteSheet(data, "run_" + direction);
            }
            else
            {
                GetComponent <Rigidbody2D>().velocity = new Vector3(0, 0);
                direction = DCommonUtils.GetLeftRightFacingToTarget(transform, enemy.transform);
                GetComponent <DAnimator>().attacking   = true;
                GetComponent <DAnimator>().spritesheet = DCommonUtils.GetSpriteSheet(data, "attack_" + direction);
            }
        }
    }