예제 #1
0
    public void FixedUpdate()
    {
        // target이 있을때
        if (target != null)
        {
            tileMapController.FindPath(target, this.gameObject);

            //nodes = tileMapController.GetNodes(this.gameObject);

            //for(int i = 0; i < nodes.Count; ++i)
            //{
            //    lNodes.Add(nodes.Dequeue());
            //}

            // 목적지에 타겟 포지션 넣어줌
            destinationPosition = target.position;
            //destinationPosition = Vector2.zero;

            // player와의 거리
            attackDistance = Vector2.Distance(transform.position, target.position);
            if (attackDistance > 1f)
            {
                //Node node = nodes.Dequeue();
                //Debug.Log(lNodes.Count);

                if (lNodes.Count > 0)
                {
                    Node node = lNodes[nIndex];

                    Vector2 vec = new Vector2(node.Position.x, node.Position.y);

                    Debug.Log(vec);

                    destinationPosition = vec;
                }

                // 기본 이동식
                transform.position = Vector2.MoveTowards(transform.position, destinationPosition, Time.fixedDeltaTime * speed);
                animator.SetFloat("Speed", 1f);

                float distance = Vector2.Distance(transform.position, destinationPosition);

                if (distance < .5f)
                {
                    ++nIndex;
                }
            }
            else if (attackDistance <= 1f)
            {
                //++nIndex;
                Debug.Log(nIndex);
                if (attackTime > 2f)
                {
                    animator.SetFloat("Speed", 0f);
                    animator.SetTrigger("Attack");
                    player.Hp--;
                    player.PlayerStateSet();
                    attackTime = 0f;
                }
            }
            attackTime += Time.deltaTime;
            FlipFacing();
        }

        //target이 없을때
        else
        {
            // enemy의 위치와 목적지의 거리가 0.1보다 작으면
            if (Vector2.Distance(transform.position, destinationPosition) < 0.1f)
            {
                stayTime += Time.deltaTime;
                animator.SetFloat("Speed", 0f);

                if (stayTime > 2f)
                {
                    //랜덤으로 목적지 위치 생성
                    SetRedirection();
                    stayTime = 0;
                }
            }
            else
            {
                animator.SetFloat("Speed", 1f);
            }
            //enemy의 위치 = 직선으로 현재위치에서 목적지까지 거리이동
            transform.position = Vector2.MoveTowards(transform.position, destinationPosition, Time.fixedDeltaTime * speed);
        }
        if (underAttack)
        {
            invincibleTime += Time.deltaTime;
        }
        if (invincibleTime > 1f)
        {
            underAttack    = false;
            invincibleTime = 0;
        }
    }