Ejemplo n.º 1
0
    void Update()
    {
        snap.snapX = true;
        snap.snapZ = true;
        isWalking  = false;
        if (!isDead)
        {
            if (gameObject.GetComponent <Health>().Currenthealth <= 0)
            {
                Die();
            }
            Vector3 move = new Vector3(0, 0, 0);
            if (IsSelected.IsSelected)
            {
                Vector3 vSnap = snap.VectorToClosestSnapPoint();

                // Node nodeToLeft = null;
                // Node nodeToRight = null;
                // Node nodeAbove = null;
                // Node nodeBelow = null;
                bool leftIsFree  = false;
                bool rightIsFree = false;
                bool aboveIsFree = false;
                bool belowIsFree = false;
                try
                {
                    Node n = customGrid.nodes[customGrid.gridIndexFromPos(transform.position.x, transform.position.z)].GetComponent <Node>().adjacents[1].GetComponent <Node>();
                    if (!n.isObstacle && !n.isOccupied)
                    {
                        leftIsFree = true;
                    }
                }
                catch (System.NullReferenceException) { }
                try
                {
                    Node n = customGrid.nodes[customGrid.gridIndexFromPos(transform.position.x, transform.position.z)].GetComponent <Node>().adjacents[3].GetComponent <Node>();
                    if (!n.isObstacle && !n.isOccupied)
                    {
                        rightIsFree = true;
                    }
                }
                catch (System.NullReferenceException) { }
                try
                {
                    Node n = customGrid.nodes[customGrid.gridIndexFromPos(transform.position.x, transform.position.z)].GetComponent <Node>().adjacents[0].GetComponent <Node>();
                    if (!n.isObstacle && !n.isOccupied)
                    {
                        aboveIsFree = true;
                    }
                }
                catch (System.NullReferenceException) { }
                try
                {
                    Node n = customGrid.nodes[customGrid.gridIndexFromPos(transform.position.x, transform.position.z)].GetComponent <Node>().adjacents[2].GetComponent <Node>();
                    if (!n.isObstacle && !n.isOccupied)
                    {
                        belowIsFree = true;
                    }
                }
                catch (System.NullReferenceException) { }

                if ((leftIsFree && ((Input.GetKey("a") && (lastMove.z == 0 || vSnap == Vector3.zero))) ||
                     (lastMove.x < 0 && vSnap != Vector3.zero)))
                {
                    move.x -= GetComponent <UnitAttributes>().walkspeed *Time.deltaTime;
                    GetComponent <UnitAnimator>().Run();
                    snap.snapX = false;
                    isWalking  = true;
                }
                else if ((rightIsFree && Input.GetKey("d") && (lastMove.z == 0 || vSnap == Vector3.zero)) ||
                         (lastMove.x > 0 && vSnap != Vector3.zero))
                {
                    move.x = GetComponent <UnitAttributes>().walkspeed *Time.deltaTime;
                    GetComponent <UnitAnimator>().Run();
                    snap.snapX = false;
                    isWalking  = true;
                }
                else if ((aboveIsFree && Input.GetKey("w") && (lastMove.x == 0 || vSnap == Vector3.zero)) ||
                         (lastMove.z > 0 && vSnap != Vector3.zero))
                {
                    move.z = GetComponent <UnitAttributes>().walkspeed *Time.deltaTime;
                    GetComponent <UnitAnimator>().Run();
                    snap.snapZ = false;
                    isWalking  = true;
                }
                else if ((belowIsFree && Input.GetKey("s") && (lastMove.x == 0 || vSnap == Vector3.zero)) ||
                         (lastMove.z < 0 && vSnap != Vector3.zero))
                {
                    move.z = -GetComponent <UnitAttributes>().walkspeed *Time.deltaTime;
                    GetComponent <UnitAnimator>().Run();
                    snap.snapZ = false;
                    isWalking  = true;
                }
                if ((!Input.GetKey("d") || (vSnap.x <= move.x && !rightIsFree)) && lastMove.x > 0 && vSnap.x >= 0 && vSnap.x <= move.x)
                {
                    move.x = vSnap.x;
                }
                else if ((!Input.GetKey("a") || (vSnap.x >= move.x && !leftIsFree)) && lastMove.x < 0 && vSnap.x <= 0 && vSnap.x >= move.x)
                {
                    move.x = vSnap.x;
                }
                else if ((!Input.GetKey("w") || (vSnap.z <= move.z && !aboveIsFree)) && lastMove.z > 0 && vSnap.z >= 0 && vSnap.z <= move.z)
                {
                    move.z = vSnap.z;
                }
                else if ((!Input.GetKey("s") || (vSnap.z >= move.z && !belowIsFree)) && lastMove.z < 0 && vSnap.z <= 0 && vSnap.z >= move.z)
                {
                    move.z = vSnap.z;
                }
            }



            Vector3 nextPos  = transform.position;
            int     posIndex = gameController.GridIndexFromPos(nextPos.x + move.x, nextPos.z);
            if (posIndex != -1 && posIndex < gameController.gameObject.GetComponent <CustomGrid>().nodes.Length)
            {
                GameObject currentNode = gameController.gameObject.GetComponent <CustomGrid>().nodes[posIndex];
                if (!currentNode.GetComponent <Node>().isObstacle)
                {
                    nextPos.x += move.x;
                }
            }
            posIndex = gameController.GridIndexFromPos(nextPos.x, nextPos.z + move.z);
            if (posIndex != -1 && posIndex < gameController.gameObject.GetComponent <CustomGrid>().nodes.Length)
            {
                GameObject currentNode = gameController.gameObject.GetComponent <CustomGrid>().nodes[posIndex];
                if (!currentNode.GetComponent <Node>().isObstacle)
                {
                    nextPos.z += move.z;
                }
            }

            lastMove           = nextPos - transform.position;
            transform.position = nextPos;

            Node _lastNode = currentNode;
            currentNode = customGrid.nodes[customGrid.gridIndexFromPos(transform.position.x, transform.position.z)].GetComponent <Node>();
            if (lastNode == null)
            {
                lastNode = currentNode;
            }
            if (currentNode != lastNode)
            {
                lastNode.isOccupied    = false;
                currentNode.isOccupied = true;
            }
            lastNode = _lastNode;

            Vector3 face = new Vector3(lastMove.x, lastMove.y, lastMove.z);
            if (face.sqrMagnitude != 0)
            {
                float damping = 10f;
                face.y = 0;
                var targetRotation = Quaternion.LookRotation(face);
                transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * damping);
            }
            else
            {
            }
            if (target == null && lastMove == Vector3.zero)
            {
                GetComponent <UnitAnimator>().Idle();
            }
            if (target == null)
            {
                return;
            }
            if (lastMove == Vector3.zero)
            {
                GetComponent <UnitAnimator>().Attack();

                Vector3    dir          = target.transform.position - transform.position;
                Quaternion lookRotation = Quaternion.LookRotation(dir);
                float      damping      = 10f;
                transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * damping);
                // Vector3 rotation = lookRotation.eulerAngles;
                // transform.rotation = Quaternion.Euler(0f, rotation.y, 0f);
                //Shoot();
                //print("shoot");
            }
            GetComponent <UnitAttributes>().firecountdwon -= Time.deltaTime;
            if (GetComponent <UnitAttributes>().firecountdwon <= 1)
            {
                GetComponent <UnitAttributes>().firecountdwon = 1 / GetComponent <UnitAttributes>().firerate;
                //print("Shoot");
                Shoot();
            }
        }
    }