Exemplo n.º 1
0
    void OnTriggerEnter2D(Collider2D player)
    {
        List <string> allParticipants = new List <string>();

        switch (name)
        {
        case "tutorialFight":
            allParticipants.Add("Bewitched Mole");
            break;

        case "firstFight":
            allParticipants.Add("Bewitched Fox");
            allParticipants.Add("Bewitched Mole");
            StoryStaticStorage.storySwitch("felixAttack");
            break;

        case "secondFight":
            allParticipants.Add("Bewitched Grasshopper");
            allParticipants.Add("Bewitched Mole");
            StoryStaticStorage.storySwitch("grasshopperAttack");
            break;

        case "thirdFight":
            allParticipants.Add("Bewitched Squirrel");
            allParticipants.Add("Bewitched Treant");
            break;

        case "bossFight":
            allParticipants.Add("King Jebediah");
            allParticipants.Add("Bewitched Mole");
            allParticipants.Add("Bewitched Treant");
            isLastFight = true;
            break;
        }

        foreach (Character playerCharacter in playerParty)
        {
            allParticipants.Add(playerCharacter.name);
        }
        foreach (string c in allParticipants.ToArray())
        {
            Debug.Log(c);
        }

        newEncounter(allParticipants.ToArray());
    }
    // Update is called once per frame
    void Update()
    {
        switch (state)
        {
        case "end":     //end of battle
            //SceneManager.LoadScene("Overworld");
            foreach (GameObject clone in cloneList)
            {
                Destroy(clone);
            }
            mainCameraCombat.SetActive(false);
            SceneManager.UnloadSceneAsync("CombatScene");
            player.mainCamera.SetActive(true);
            player.GetComponent <Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeRotation;
            inCombat = false;
            if (isLastFight)
            {
                StoryStaticStorage.storySwitch("wonLastFight");
            }
            break;

        case "start":     // Prepare the player's selection, or prepare and complete the turn for ai
        {
            contextMenu.SetActive(false);

            //check to see if all of one team are dead, if all players team dies at the same time as the enemy, they lose
            bool friendlyAlive = false;
            foreach (Character character in friendlyParty)
            {
                if (!character.isDead)
                {
                    friendlyAlive = true;
                    break;
                }
            }
            bool enemyAlive = false;
            foreach (Character character in enemyParty)
            {
                if (!character.isDead)
                {
                    enemyAlive = true;
                    break;
                }
            }

            if (!enemyAlive && friendlyAlive)
            {
                if (allCharacters["Toa"].isDead == true)
                {
                    if (GetItemCount(allItems["Depetrification Crystal"]) > 0)
                    {
                        StaticStorage.UsePlayerItem("Depetrification Crystal");
                        StaticStorage.allCharacters["Toa"].isDead        = false;
                        StaticStorage.allCharacters["Toa"].currentHealth = StaticStorage.allCharacters["Toa"].maxHealth;
                        state = "end";
                        Debug.Log("You won");
                    }
                    else
                    {
                        //restorePlayerToSave();
                        Debug.Log("You lost");
                        SceneManager.LoadScene("LoseScene");
                    }
                }
                else
                {
                    state = "end";
                    Debug.Log("You won");
                }
            }
            else if (!friendlyAlive)
            {
                //restorePlayerToSave();
                state = "end";
                Debug.Log("You lost");
                SceneManager.LoadScene("LoseScene");
            }
            else
            {
                Character playerTakingTurn = turnOrder.Peek();         //which character is taking the turn
                playerTakingTurnName = playerTakingTurn.name;

                string tempTurnOrderText = "Turn Order: ";
                int    turnOrderI        = 0;
                foreach (Character c in turnOrder)
                {
                    if (turnOrderI == turnOrder.Count - 1)
                    {
                        tempTurnOrderText += c.name;
                    }
                    else
                    {
                        tempTurnOrderText += c.name + ", ";
                    }
                    turnOrderI++;
                }
                turnOrderText.text = tempTurnOrderText;

                if (playerTakingTurn.isInterrupt)
                {
                    turnOrder.Dequeue();         //dont put them at the back of the queue
                }
                else
                {
                    turnOrder.Enqueue(turnOrder.Dequeue());         //put them at the back of the queue
                }

                if (!playerTakingTurn.isDead)
                {
                    //check for status effects
                    bool stunned = false;         //stun wears off each turn
                    currentlyStunned = false;
                    List <StatusEffect> statusEffects = playerTakingTurn.statusEffects;
                    currentStrength = 0;
                    foreach (StatusEffect statusEffect in statusEffects)
                    {
                        if (!(statusEffect.currentTurnsRemaining <= 0))
                        {
                            playerTakingTurn.currentHealth -= statusEffect.dot;

                            currentStrength = statusEffect.strength;

                            //for damageIndicator
                            if (!(statusEffect.strength != 0 && statusEffect.dot == 0 && statusEffect.stun == false))
                            {
                                GameObject target;
                                if (playerTakingTurn.team == 0)         //friendly
                                {
                                    int targetIndex = friendlyParty.IndexOf(playerTakingTurn);
                                    target = friendlyTargets["FriendlyTarget" + (targetIndex + 1)];
                                }
                                else         //enemy
                                {
                                    int targetIndex = enemyParty.IndexOf(playerTakingTurn);
                                    target = enemyTargets["EnemyTarget" + (targetIndex + 1)];
                                }
                                GameObject damageIndicator = Instantiate(damageIndicatorPrefab, target.transform.position, new Quaternion(0, 0, 0, 0));
                                cloneList.Add(damageIndicator);
                                damageIndicator.transform.SetParent(canvas.transform);
                                TextMeshProUGUI textMeshProUGUI = damageIndicator.GetComponent <TextMeshProUGUI>();
                                textMeshProUGUI.text = System.Math.Abs(statusEffect.dot).ToString();
                                if (statusEffect.dot > 0)
                                {
                                    textMeshProUGUI.color = new Color(1, 0, 0, textMeshProUGUI.color.a);
                                }
                                else if (statusEffect.dot < 0)
                                {
                                    textMeshProUGUI.color = new Color(0, 1, 0, textMeshProUGUI.color.a);
                                }
                                else
                                {
                                    textMeshProUGUI.color = new Color(1, 1, 0, textMeshProUGUI.color.a);
                                }
                            }
                            //end damageIndicator

                            statusEffect.currentTurnsRemaining -= 1;

                            RefreshHealth(playerTakingTurn);
                            if (playerTakingTurn.isDead)
                            {
                                break;
                            }

                            if (statusEffect.stun)
                            {
                                stunned          = true;
                                currentlyStunned = true;
                            }
                        }
                    }
                    if (!playerTakingTurn.isDead)
                    {
                        if (!stunned)
                        {
                            if (enemyParty.Contains(playerTakingTurn))         //enemy automatically takes action
                            {
                                int randomMoveNum = Random.Range(0, playerTakingTurn.moves.Count);
                                int randomTargetNum;
                                if (playerTakingTurn.moves[randomMoveNum].appliedToTeam)
                                {
                                    currentlySelectedMoveOrItem     = "move";
                                    currentlySelectedMoveOrItemName = playerTakingTurn.moves[randomMoveNum].name;
                                    while (true)
                                    {
                                        List <string> targetableCharacters = new List <string>();
                                        if (playerTakingTurn.moves[randomMoveNum].statusEffects.Length == 0)         // if the applied to team move has no lasting effects eg: HoT or a buff, the ai will never heal a full health friend
                                        {
                                            int i = 1;
                                            foreach (Character character in enemyParty)         //finding characters in need of healing
                                            {
                                                string targetName = "EnemyTarget" + i;
                                                if (character.currentHealth < character.maxHealth && !character.isDead)
                                                {
                                                    targetableCharacters.Add(targetName);
                                                }
                                                i++;
                                            }
                                            if (targetableCharacters.Count == 0)         //if no one needs healing, heal a random alive character
                                            {
                                                int j = 1;
                                                foreach (Character character in enemyParty)
                                                {
                                                    if (!character.isDead)
                                                    {
                                                        string targetName = "EnemyTarget" + j;
                                                        targetableCharacters.Add(targetName);
                                                    }
                                                    j++;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            int i = 1;
                                            foreach (Character character in enemyParty)         //cast on a random allied character instead of specifically people who need healing
                                            {
                                                if (!character.isDead)
                                                {
                                                    string targetName = "EnemyTarget" + i;
                                                    targetableCharacters.Add(targetName);
                                                }
                                                i++;
                                            }
                                        }
                                        randomTargetNum = Random.Range(0, targetableCharacters.Count);
                                        string tempChosenTargetName = targetableCharacters[randomTargetNum];
                                        SetChosenTargetName(tempChosenTargetName);
                                        if (!StaticStorage.allCharacters[chosenTargetName].isDead)
                                        {
                                            break;
                                        }
                                    }
                                    StartCoroutine("WaitEndTurn");
                                    StartCoroutine("WaitUseMoveOrItem");
                                }
                                else
                                {
                                    currentlySelectedMoveOrItem     = "move";
                                    currentlySelectedMoveOrItemName = playerTakingTurn.moves[randomMoveNum].name;
                                    while (true)
                                    {
                                        randomTargetNum = Random.Range(0, friendlyParty.Count);
                                        string tempChosenTargetName = "FriendlyTarget" + (randomTargetNum + 1);
                                        SetChosenTargetName(tempChosenTargetName);
                                        if (!StaticStorage.allCharacters[chosenTargetName].isDead)
                                        {
                                            break;
                                        }
                                    }
                                    StartCoroutine("WaitEndTurn");
                                    StartCoroutine("WaitUseMoveOrItem");
                                }
                            }
                            else if (friendlyParty.Contains(playerTakingTurn))         //player is given a selection of their possible actions
                            {
                                turnMenuText.text = playerTakingTurn.name;
                                //Adding the the player's items to their selection
                                int         i         = 1;
                                List <Item> seenItems = new List <Item>();
                                foreach (Item item in playerItems)
                                {
                                    if (!seenItems.Contains(item))
                                    {
                                        TextMeshProUGUI textMeshProUGUI = items["Item" + i].GetComponentInChildren <TextMeshProUGUI>();
                                        textMeshProUGUI.SetText(item.name + " (" + StaticStorage.GetItemCount(item) + ")");
                                        seenItems.Add(item);
                                        i++;
                                    }
                                }
                                for (int j = i; j <= 4; j++)         //getting rid of buttons for items we dont have
                                {
                                    items["Item" + j].SetActive(false);
                                }

                                //Adding the the player's moves to their selection
                                int m = 1;
                                foreach (Move move in playerTakingTurn.moves)
                                {
                                    TextMeshProUGUI textMeshProUGUI = moves["Move" + m].GetComponentInChildren <TextMeshProUGUI>();
                                    textMeshProUGUI.SetText(move.name);
                                    moves["Move" + m].SetActive(true);
                                    m++;
                                }
                                for (int j = m; j <= 4; j++)         //getting rid of buttons for moves we dont have
                                {
                                    moves["Move" + j].SetActive(false);
                                }

                                turnMenu.SetActive(true);

                                //all actions are taken by the listeners
                            }
                            else if (playerTakingTurn.isInterrupt)
                            {
                                currentlySelectedMoveOrItem     = "move";
                                currentlySelectedMoveOrItemName = playerTakingTurn.moves[0].name;
                                SetChosenTargetName(currentInterruptTargetName);
                                StartCoroutine("WaitEndTurn");
                                if (!allCharacters[chosenTargetName].isDead)
                                {
                                    StartCoroutine("WaitUseMoveOrItem");
                                }
                            }

                            state = "waiting"; //wait for user or ai to activate button listeners to progress + time for turn to animate etc
                        }
                        else                   //the ai or player is stunned
                        {
                            state = "waiting";
                            StartCoroutine("WaitEndTurn");
                        }
                    }
                }
            }
            break;
        }

        default:
            break;
        }
    }