Esempio n. 1
0
    public bool TakeAction()
    {
        if (currentlyMovement == false)
        {
            bool bMovementFound = false;
            bool bPlayerFound   = false;

            Vector2 nextPos = Vector2.zero;
            int     xPos    = 0;
            int     yPos    = 0;
            while (bMovementFound == false)
            {
                nextPos = CurrentEnemy.RetreivePosition() + currentDir;
                xPos    = (int)nextPos.x;
                yPos    = (int)nextPos.y;


                if (((currentDir.y == -1 && yPos >= 0) || (currentDir.y == 1 && yPos < gridYSize) || currentDir.y == 0) && ((currentDir.x == -1 && xPos >= 0) || (currentDir.x == 1 && xPos < gridXSize) || currentDir.x == 0))
                {
                    if (CurrentEnemy.Grid[xPos, yPos] == null || CurrentEnemy.Grid[xPos, yPos].GetComponent <GridSquare>().ObjectAbove())
                    {
                        currentDir *= -1;
                    }
                    else if (CurrentEnemy.Grid[xPos, yPos].GetComponent <GridSquare>().ObjectAbove() == false)
                    {
                        bMovementFound = true;
                    }
                }
                else
                {
                    currentDir *= -1;
                }

                if (player.CurrentSquare == new Vector2(xPos, yPos))
                {
                    bPlayerFound = true;
                }
            }

            if (bPlayerFound)
            {
                Vector3 targetDir = player.transform.position - transform.position;
                rb.velocity    = targetDir.normalized * 35;
                actionFinished = true;
                CurrentEnemy.RetreiveLevelManager().PlayerDeath();
            }
            else
            {
                Vector2 GridDifference = CurrentEnemy.RetreivePosition() - nextPos;
                if (GridDifference.x == -1)
                {
                    transform.rotation = Quaternion.Euler(new Vector3(0, 270, 0));
                }
                else if (GridDifference.x == 1)
                {
                    transform.rotation = Quaternion.Euler(new Vector3(0, 90, 0));
                }
                else if (GridDifference.y == -1)
                {
                    transform.rotation = Quaternion.Euler(new Vector3(0, 180, 0));
                }
                else if (GridDifference.y == 1)
                {
                    transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
                }


                CurrentEnemy.Grid[(int)CurrentEnemy.RetreivePosition().x, (int)CurrentEnemy.RetreivePosition().y].GetComponent <GridSquare>().SetAbove(false);
                CurrentEnemy.Grid[xPos, yPos].GetComponent <GridSquare>().SetAbove(true);
                CurrentEnemy.UpdatePosition(new Vector2(xPos, yPos));

                Vector3 targetPosition = CurrentEnemy.Grid[xPos, yPos].transform.position;
                targetPosition = new Vector3(targetPosition.x, transform.position.y, targetPosition.z);



                StartCoroutine(MovePos(transform, targetPosition, 0.2f));
            }
        }

        return(actionFinished);
    }