Beispiel #1
0
    private void WalkingTowardsTarget(int x, int y, bool facingUp, bool facingDown, bool facingLeft, bool facingRight)
    {
        bool inRange = false;

        bool moved = false;

        float distanceTop       = 100;
        float distanceBottom    = 99;
        float distanceLeft      = 98;
        float distanceRight     = 97;
        float distanceTopAbs    = 100;
        float distanceBottomAbs = 99;
        float distanceLeftAbs   = 98;
        float distanceRightAbs  = 97;

        bool dontGoUp    = false;
        bool dontGoDown  = false;
        bool dontGoLeft  = false;
        bool dontGoRight = false;

        if (-y - 1 > -1)
        {
            distanceTop    = Mathf.Abs(transform.position.x - target.position.x) + Mathf.Abs((transform.position.y + 1) - target.position.y);
            distanceTopAbs = distanceTop;
            Debug.Log("top " + distanceTop + -y);
        }
        if (-y - 1 < 0 || !tileData.rowsMovement[-y - 1].transform.GetChild(x).CompareTag("MoveTile") || facingDown)
        {
            dontGoUp    = true;
            distanceTop = 100;
            Debug.Log("U");
        }
        if (tileData.currMap[-y - 1, x] > wall && tileData.currMapCharPos[-y - 1, x] == 0 && tileData.rowsMovement[-y - 1].transform.GetChild(x).CompareTag("MoveTileRed"))
        {
            Debug.Log("BlockU");
            dontGoUp    = true;
            dontGoDown  = true;
            dontGoLeft  = true;
            dontGoRight = true;
        }
        if (-y + 1 < tileData.rowsMovement.Count)
        {
            distanceBottom    = Mathf.Abs(transform.position.x - target.position.x) + Mathf.Abs((transform.position.y - 1) - target.position.y);
            distanceBottomAbs = distanceBottom;
            Debug.Log("bottom " + distanceBottom);
        }
        if (-y + 1 >= tileData.rowsMovement.Count || !tileData.rowsMovement[-y + 1].transform.GetChild(x).CompareTag("MoveTile") || facingUp)
        {
            dontGoDown     = true;
            distanceBottom = 99;
            Debug.Log("D");
        }
        if (tileData.currMap[-y + 1, x] > wall && tileData.currMapCharPos[-y + 1, x] == 0 && tileData.rowsMovement[-y + 1].transform.GetChild(x).CompareTag("MoveTileRed"))
        {
            Debug.Log("BlockD");
            dontGoUp    = true;
            dontGoDown  = true;
            dontGoLeft  = true;
            dontGoRight = true;
        }
        if (x - 1 > -1)
        {
            distanceLeft    = Mathf.Abs((transform.position.x - 1) - target.position.x) + Mathf.Abs(transform.position.y - target.position.y);
            distanceLeftAbs = distanceLeft;
            Debug.Log("left " + distanceLeft);
        }
        if (x - 1 < 0 || !tileData.rowsMovement[-y].transform.GetChild(x - 1).CompareTag("MoveTile") || facingRight)
        {
            dontGoLeft   = true;
            distanceLeft = 98;
            Debug.Log("L");
        }
        if (x + 1 < tileData.rowsMovement[-y].transform.childCount)
        {
            distanceRight    = Mathf.Abs((transform.position.x + 1) - target.position.x) + Mathf.Abs(transform.position.y - target.position.y);
            distanceRightAbs = distanceRight;
            Debug.Log("right " + distanceRight);
        }
        if (x + 1 >= tileData.rowsMovement[-y].transform.childCount || !tileData.rowsMovement[-y].transform.GetChild(x + 1).CompareTag("MoveTile") || facingLeft)
        {
            dontGoRight   = true;
            distanceRight = 97;
            Debug.Log("R");
        }

        if (distanceTopAbs == 0 || distanceBottomAbs == 0 || distanceLeftAbs == 0 || distanceRightAbs == 0)
        {
            if (!stats.equippedWeapon.rangeOneAndTwo)
            {
                if (!dontGoUp)
                {
                    tileData.currMapCharPos[-y, x]     = 0;
                    tileData.currMapCharPos[-y - 1, x] = charId;
                    transform.position = new Vector2(transform.position.x, transform.position.y + 1);
                    y += 1;
                }
                else if (!dontGoDown)
                {
                    Debug.Log("Do this!!!");
                    tileData.currMapCharPos[-y, x]     = 0;
                    tileData.currMapCharPos[-y + 1, x] = charId;
                    transform.position = new Vector2(transform.position.x, transform.position.y - 1);
                    y -= 1;
                }
                else if (!dontGoLeft)
                {
                    tileData.currMapCharPos[-y, x]     = 0;
                    tileData.currMapCharPos[-y, x - 1] = charId;
                    transform.position = new Vector2(transform.position.x - 1, transform.position.y);
                    x -= 1;
                }
                else if (!dontGoRight)
                {
                    tileData.currMapCharPos[-y, x]     = 0;
                    tileData.currMapCharPos[-y, x + 1] = charId;
                    transform.position = new Vector2(transform.position.x + 1, transform.position.y);
                    x += 1;
                }
            }
        }

        if (distanceTopAbs < stats.equippedWeapon.range || distanceBottomAbs < stats.equippedWeapon.range || distanceLeftAbs < stats.equippedWeapon.range || distanceRightAbs < stats.equippedWeapon.range)
        {
            if (tileData.currMapCharPos[-y, x] == charId)
            {
                inRange = true;
                moved   = true;
                tileData.DeselectMovement();
                GetComponent <Attack>().CheckAttackRange(false);
            }
            else
            {
                if (!dontGoUp)
                {
                    tileData.currMapCharPos[-y, x] = 0;
                    transform.position             = new Vector2(transform.position.x, transform.position.y + 1);
                    StartCoroutine(DelayMovement(x, y + 1, true, false, false, false));
                }
                else if (!dontGoDown)
                {
                    tileData.currMapCharPos[-y, x] = 0;
                    transform.position             = new Vector2(transform.position.x, transform.position.y - 1);
                    StartCoroutine(DelayMovement(x, y - 1, false, true, false, false));
                }
                else if (!dontGoLeft)
                {
                    tileData.currMapCharPos[-y, x] = 0;
                    transform.position             = new Vector2(transform.position.x - 1, transform.position.y);
                    StartCoroutine(DelayMovement(x - 1, y, false, false, true, false));
                }
                else if (!dontGoRight)
                {
                    tileData.currMapCharPos[-y, x] = 0;
                    transform.position             = new Vector2(transform.position.x + 1, transform.position.y);
                    StartCoroutine(DelayMovement(x + 1, y, false, false, false, true));
                }
            }
        }

        if (distanceTop >= stats.equippedWeapon.range && distanceBottom >= stats.equippedWeapon.range && distanceLeft >= stats.equippedWeapon.range && distanceRight >= stats.equippedWeapon.range && !inRange)
        {
            if (distanceTop <= distanceBottom && distanceTop <= distanceLeft && distanceTop <= distanceRight && !facingDown && !dontGoUp)
            {
                dontGoUp    = true;
                dontGoDown  = true;
                dontGoLeft  = true;
                dontGoRight = true;

                if (tileData.currMapCharPos[-y - 1, x] == 0 || tileData.rowsMovement[-y - 2].transform.GetChild(x).CompareTag("MoveTile") && tileData.currMapCharPos[-y - 2, x] == 0 && tileData.currMapCharPos[-y - 1, x] != 0)
                {
                    dontGoUp    = false;
                    dontGoDown  = false;
                    dontGoLeft  = false;
                    dontGoRight = false;
                    moved       = true;

                    tileData.currMapCharPos[-y, x] = 0;
                    if (tileData.currMapCharPos[-y - 1, x] == 0)
                    {
                        tileData.currMapCharPos[-y - 1, x] = charId;
                    }
                    transform.position = new Vector2(transform.position.x, transform.position.y + 1);
                    StartCoroutine(DelayMovement(x, y + 1, true, false, false, false));
                }
            }
            else if (distanceBottom < distanceTop && distanceBottom <= distanceLeft && distanceBottom <= distanceRight && !facingUp && !dontGoDown)
            {
                dontGoUp    = true;
                dontGoDown  = true;
                dontGoLeft  = true;
                dontGoRight = true;

                if (tileData.currMapCharPos[-y + 1, x] == 0 || tileData.rowsMovement[-y + 2].transform.GetChild(x).CompareTag("MoveTile") && -y + 2 < tileData.rowsMovement.Count && tileData.currMapCharPos[-y + 2, x] == 0 && tileData.currMapCharPos[-y + 1, x] != 0)
                {
                    dontGoUp    = false;
                    dontGoDown  = false;
                    dontGoLeft  = false;
                    dontGoRight = false;
                    moved       = true;

                    tileData.currMapCharPos[-y, x] = 0;
                    transform.position             = new Vector2(transform.position.x, transform.position.y - 1);
                    if (tileData.currMapCharPos[-y + 1, x] == 0)
                    {
                        tileData.currMapCharPos[-y + 1, x] = charId;
                    }
                    StartCoroutine(DelayMovement(x, y - 1, false, true, false, false));
                }
            }
            else if (distanceLeft < distanceBottom && distanceLeft < distanceTop && distanceLeft <= distanceRight && !facingRight && !dontGoLeft)
            {
                dontGoUp    = true;
                dontGoDown  = true;
                dontGoLeft  = true;
                dontGoRight = true;

                if (tileData.currMapCharPos[-y, x - 1] == 0 || tileData.rowsMovement[-y].transform.GetChild(x - 2).CompareTag("MoveTile") && tileData.currMapCharPos[-y, x - 2] == 0 && tileData.currMapCharPos[-y, x - 1] != 0)
                {
                    dontGoUp    = false;
                    dontGoDown  = false;
                    dontGoLeft  = false;
                    dontGoRight = false;
                    moved       = true;

                    tileData.currMapCharPos[-y, x] = 0;
                    transform.position             = new Vector2(transform.position.x - 1, transform.position.y);
                    if (tileData.currMapCharPos[-y, x - 1] == 0)
                    {
                        tileData.currMapCharPos[-y, x - 1] = charId;
                    }
                    StartCoroutine(DelayMovement(x - 1, y, false, false, true, false));
                }
            }
            else if (distanceRight < distanceBottom && distanceRight < distanceLeft && distanceRight < distanceTop && !facingLeft && !dontGoRight)
            {
                dontGoUp    = true;
                dontGoDown  = true;
                dontGoLeft  = true;
                dontGoRight = true;

                if (tileData.currMapCharPos[-y, x + 1] == 0 || tileData.rowsMovement[-y].transform.GetChild(x + 2).CompareTag("MoveTile") && tileData.currMapCharPos[-y, x + 2] == 0 && tileData.currMapCharPos[-y, x + 1] != 0)
                {
                    dontGoUp    = false;
                    dontGoDown  = false;
                    dontGoLeft  = false;
                    dontGoRight = false;
                    moved       = true;

                    tileData.currMapCharPos[-y, x] = 0;
                    transform.position             = new Vector2(transform.position.x + 1, transform.position.y);
                    if (tileData.currMapCharPos[-y, x + 1] == 0)
                    {
                        tileData.currMapCharPos[-y, x + 1] = charId;
                    }
                    StartCoroutine(DelayMovement(x + 1, y, false, false, false, true));
                }
            }
        }

        if (!moved || dontGoDown && dontGoLeft && dontGoRight && dontGoUp && !tileData.rowsMovement[-Mathf.RoundToInt(target.position.y + 0.5f)].transform.GetChild(Mathf.RoundToInt(target.position.x - 0.5f)).CompareTag("MoveTileRed") && !inRange)
        {
            tileData.DeselectMovement();
            BSM.enemyIndex++;
            BSM.GiveTurnToAI();
        }
    }
Beispiel #2
0
    private IEnumerator AttackOrder(Stats player, Stats enemy, Stats damageHolder, GameObject playerSprite, GameObject enemySprite, float distance, int i, bool twiceFourDoubling)
    {
        Debug.Log(playerSprite);
        Debug.Log(enemySprite);
        playerSprite.GetComponent <AttackingInBattle>().done = false;
        enemySprite.GetComponent <AttackingInBattle>().done  = false;
        Attack(player, damageHolder.GetComponent <Attack>().damage, damageHolder.GetComponent <Attack>().acc, damageHolder.GetComponent <Attack>().crit, playerSprite, enemySprite);//1st player
        while (!playerSprite.GetComponent <AttackingInBattle>().done)
        {
            yield return(new WaitForSeconds(0.2f));
        }
        playerSprite.GetComponent <Animator>().Play("Idle");
        if (damageHolder.GetComponent <Attack>().enemyDoubling == i && enemy.hp > 0 && distance <= enemy.equippedWeapon.range && enemy.equippedWeapon.rangeOneAndTwo || distance != 1 && distance <= enemy.equippedWeapon.range && !enemy.equippedWeapon.rangeOneAndTwo || enemy.equippedWeapon.counterAll)
        {
            enemySprite.GetComponent <AttackingInBattle>().done  = false;
            playerSprite.GetComponent <AttackingInBattle>().done = false;
            Attack(enemy, damageHolder.GetComponent <Attack>().enemyDamage, damageHolder.GetComponent <Attack>().enemyAcc, damageHolder.GetComponent <Attack>().enemyCrit, enemySprite, playerSprite);//1st enemy
            while (!enemySprite.GetComponent <AttackingInBattle>().done)
            {
                yield return(new WaitForSeconds(0.2f));
            }
            enemySprite.GetComponent <Animator>().Play("Idle");
            if (player.hp <= 0)
            {
                if (damageHolder == playerBattle)
                {
                    tileData.players.Remove(player.gameObject);
                    tileData.currMapCharPos[Mathf.RoundToInt(-player.GetComponent <PlayerMovement>().oldPos.y + 0.5f), Mathf.RoundToInt(player.GetComponent <PlayerMovement>().oldPos.x - 0.5f)] = 0;
                    tileData.currMapCharPos[Mathf.RoundToInt(-player.transform.position.y + 0.5f), Mathf.RoundToInt(player.transform.position.x - 0.5f)] = 0;
                }
                else
                {
                    tileData.enemiesInGame.Remove(player.gameObject);
                    tileData.currMapCharPos[Mathf.RoundToInt(-player.transform.position.y + 0.5f), Mathf.RoundToInt(player.transform.position.x - 0.5f)] = 0;

                    if (xp > 0 && playerBattle != null)
                    {
                        leveling = true;
                        StartCoroutine(Leveling());
                        while (leveling)
                        {
                            yield return(new WaitForSeconds(0.5f));
                        }
                    }
                }

                Destroy(player.gameObject);

                yield return(new WaitForSeconds(1));

                Destroy(playerSprite);
                Destroy(enemySprite);
                cursor.battlePanel.SetActive(false);
                tileData.DeselectMovement();

                if (damageHolder == enemyBattle)
                {
                    tileData.DeselectMovement();
                    BSM.GiveTurnToAI();
                }
            }
        }
        else if (enemy.hp <= 0)
        {
            tileData.currMapCharPos[Mathf.RoundToInt(-enemy.transform.position.y + 0.5f), Mathf.RoundToInt(enemy.transform.position.x - 0.5f)] = 0;
            if (damageHolder == playerBattle)
            {
                xp *= 10;
                tileData.enemiesInGame.Remove(enemy.gameObject);
            }
            else
            {
                tileData.players.Remove(enemy.gameObject);
            }
            Destroy(enemy.gameObject);
        }

        yield return(new WaitForSeconds(0.2f));

        if (damageHolder != null)
        {
            if (player.hp > 0 && enemy.hp > 0 && damageHolder.GetComponent <Attack>().doubling == 2 && i == 1 || enemy.hp > 0 && damageHolder.GetComponent <Attack>().doubling == 4 && i == 1 || enemy.hp > 0 && damageHolder.GetComponent <Attack>().doubling == 4 && i == 2 || enemy.hp > 0 && damageHolder.GetComponent <Attack>().doubling == 4 && i == 4 && twiceFourDoubling)
            {
                int j = 0;
                switch (i)
                {
                case 1:
                    j = 2;
                    break;

                case 2:
                    j = 4;
                    twiceFourDoubling = true;
                    break;

                case 4:
                    twiceFourDoubling = false;
                    break;
                }
                StartCoroutine(AttackOrder(player, enemy, damageHolder, playerSprite, enemySprite, distance, j, twiceFourDoubling));
            }
            else
            {
                if (xp > 0 && playerBattle != null)
                {
                    leveling = true;
                    StartCoroutine(Leveling());
                    while (leveling)
                    {
                        yield return(new WaitForSeconds(0.5f));
                    }
                }

                yield return(new WaitForSeconds(1));

                Destroy(playerSprite);
                Destroy(enemySprite);
                cursor.battlePanel.SetActive(false);

                if (damageHolder == playerBattle)
                {
                    GameObject.FindGameObjectWithTag("Canvas").GetComponent <SelectChoices>().Wait();
                }
                else
                {
                    enemyBattle.GetComponent <EnemyAI>().wait = true;
                    tileData.DeselectMovement();
                    BSM.enemyIndex++;
                    BSM.GiveTurnToAI();
                }
            }
        }
    }