Пример #1
0
 private void TakeDamage()
 {
     if (health > 0f)
     {
         health -= .05f;
         healthBar.SetSize(health);
     }
 }
Пример #2
0
 // Do damage on enemy
 public void enemyDamage(string type)
 {
     // If enemy health is greater than zero, add damage
     if (enemyHealth > 0)
     {
         // Do more damage if cast spell of enemy weakness, otherwise do regular damage
         float damage = 0.0f;
         bool  crit   = false;
         if (checkDamageWeakness(type) && !checkCritical()) // If weakness spell cast and critical not met, do weakness damage
         {
             damage       = regularPlayerDamage + 0.05f;
             enemyHealth -= damage;
         }
         else if (!checkDamageWeakness(type) && !checkCritical()) // If weakness spell not cast and critical not met, do regular damage
         {
             damage       = regularPlayerDamage;
             enemyHealth -= damage;
         }
         else // Otherwise do critical damage
         {
             crit = true;
             reduceInventory();    // Reduce inventory on screen and in code
             reduceSpellbar();     //reduce the crtical bar on screen and in code
             damage       = 0.40f; // Do critical damage
             enemyHealth -= damage;
             // (Zach)Do health bar size change
         }
         damage = damage * 100;
         int d = (int)damage;
         displayAttack(d, type, crit); // Display the attack on screen
         if (enemyHealth < 0.0f)
         {
             enemyHealth = 0.0f;
         }
         healthBar.SetSize(enemyHealth);
     }
 }
Пример #3
0
    // Start is called before the first frame update
    void Start()
    {
        // Instantiate levels
        levels = new List <string> {
            "Level1", "Level2", "Level3"
        };
        Scene  currentScene = SceneManager.GetActiveScene(); //reset playerPrefs if at starting level
        string sceneName    = currentScene.name;

        if (sceneName == "Level1")
        {
            PlayerPrefs.DeleteAll();
        }

        // Set enemy data
        string level = sceneName.Replace("Level", "");
        int    lev   = 0;

        Int32.TryParse(level, out lev);
        if (lev != 0)
        {
            setEnemy(lev);
        }

        if (PlayerPrefs.HasKey("currentLevel"))
        {
            currentLevel = PlayerPrefs.GetInt("currentLevel"); //setup current level in playerprefs
        }
        else
        {
            PlayerPrefs.SetInt("currentLevel", 0);
        }
        // Instantiate jewel list
        jewels = new List <GameObject>()
        {
            blueJewel, orangeJewel, yellowJewel, redJewel, purpleJewel, greenJewel
        };
        // Instantiate matches
        blueMatches   = 0;
        orangeMatches = 0;
        yellowMatches = 0;
        redMatches    = 0;
        purpleMatches = 0;
        greenMatches  = 0;
        // Instantiate selected
        firstSelected  = null;
        secondSelected = null;
        // Instantiate component grid
        componentGrid    = new GameObject[6][];
        componentGrid[0] = new GameObject[6];
        componentGrid[1] = new GameObject[6];
        componentGrid[2] = new GameObject[6];
        componentGrid[3] = new GameObject[6];
        componentGrid[4] = new GameObject[6];
        componentGrid[5] = new GameObject[6];
        heroHealth       = 1.0f;
        enemyHealth      = 1.0f;
        enemyAttack      = 3;
        spellComponent1.SetSize(0f);
        spellComponent2.SetSize(0f);
        spellComponent3.SetSize(0f);
        swapping = false;
        matching = false;
        paused   = false;
        // Create the starting grid for the level
        createGrid();
    }
Пример #4
0
    // Update is called once per frame
    void Update()
    {
        // Implement animation based swap
        if (swapping)
        {
            // Move Components slowly
            firstSelected.transform.position  = Vector3.MoveTowards(firstSelected.transform.position, initialSecondPos, 4 * Time.deltaTime);
            secondSelected.transform.position = Vector3.MoveTowards(secondSelected.transform.position, initialFirstPos, 4 * Time.deltaTime);

            // If swapped, finish swap
            if (firstSelected.transform.position == initialSecondPos && secondSelected.transform.position == initialFirstPos)
            {
                // Delete selects after swap
                GameObject[] selects = GameObject.FindGameObjectsWithTag("select");
                Destroy(selects[0]);
                Destroy(selects[1]);

                enemyAttack--;
                GameObject.Find("EnemyAttackCountText").GetComponent <Text>().text = "Enemy will attack in " + enemyAttack + " moves";

                if (enemyAttack == 0)
                {
                    enemyAttack = 3;
                    int damage = UnityEngine.Random.Range(enemyMinDamage, enemyMaxDamage); // does damage based on enemy script
                    heroHealth -= (float)damage / 100;

                    source.PlayOneShot(s_EnemyAttack);

                    // Call gameover is health drops below zero
                    if (heroHealth <= 0.01f)
                    {
                        SceneManager.LoadScene("GameOver");
                    }
                    healthBarHero.SetSize(heroHealth);
                    GameObject.Find("EnemyAttackCountText").GetComponent <Text>().text = "Enemy attacked for " + damage + " damage";
                }

                // Check for matches and match
                match(checkMatch(), firstSelected.GetComponent <GridPosition>().getRow(), firstSelected.GetComponent <GridPosition>().getColumn());
                match(checkMatch(), secondSelected.GetComponent <GridPosition>().getRow(), secondSelected.GetComponent <GridPosition>().getColumn());

                // Set selected as null
                setFirstSelected(null);
                setSecondSelected(null);
                swapping = false;
            }
        }

        // Implement animation based matching
        if (matching)
        {
            // If in same column, match vertically
            if (getStringCol(currMatch[0]) == getStringCol(currMatch[1]))
            {
                int topRow = getStringRow(currMatch[0]);
                int topCol = getStringCol(currMatch[0]);


                // Only change grid positions once, iterate the rest until all objects moved on screen
                if (firstIteration)
                {
                    type           = componentGrid[topRow][topCol].tag;
                    verticalMatch1 = componentGrid[topRow][topCol].transform.position;
                    verticalMatch2 = componentGrid[topRow + 1][topCol].transform.position;
                    verticalMatch3 = componentGrid[topRow + 2][topCol].transform.position;
                    // Remove match from grid
                    GameObject match1 = componentGrid[topRow][topCol];
                    GameObject match2 = componentGrid[topRow + 1][topCol];
                    GameObject match3 = componentGrid[topRow + 2][topCol];
                    componentGrid[topRow][topCol]     = null;
                    componentGrid[topRow + 1][topCol] = null;
                    componentGrid[topRow + 2][topCol] = null;
                    // Destroy matched on screen
                    Destroy(match1);
                    Destroy(match2);
                    Destroy(match3);
                    // Move other objects down
                    int bottomRow = topRow - 1;
                    int p         = 2;

                    for (int i = bottomRow; i > -1; i--)
                    {
                        componentGrid[i + 3][topCol] = componentGrid[i][topCol];
                        componentGrid[i][topCol]     = null;
                        componentGrid[i + 3][topCol].GetComponent <GridPosition>().setRowColumn(i + 3, topCol);
                        p--;
                    }
                    // Replace top three with randoms
                    n1 = Instantiate(getRandomJewel(), new Vector2(topCol - 7, 1), Quaternion.identity);
                    n2 = Instantiate(getRandomJewel(), new Vector2(topCol - 7, 1), Quaternion.identity);
                    n3 = Instantiate(getRandomJewel(), new Vector2(topCol - 7, 1), Quaternion.identity);
                    // Keep components random
                    while (n1.tag == n2.tag && n1.tag == n3.tag)
                    {
                        Destroy(n1);
                        Destroy(n2);
                        Destroy(n3);
                        n1 = Instantiate(getRandomJewel(), new Vector2(topCol - 7, 1), Quaternion.identity);
                        n2 = Instantiate(getRandomJewel(), new Vector2(topCol - 7, 1), Quaternion.identity);
                        n3 = Instantiate(getRandomJewel(), new Vector2(topCol - 7, 1), Quaternion.identity);
                    }
                    // Set grid position of new components
                    n3.GetComponent <GridPosition>().setRowColumn(2, topCol);
                    componentGrid[2][topCol] = n3;
                    n2.GetComponent <GridPosition>().setRowColumn(1, topCol);
                    componentGrid[1][topCol] = n2;
                    n1.GetComponent <GridPosition>().setRowColumn(0, topCol);
                    componentGrid[0][topCol] = n1;
                    firstIteration           = false;
                }

                // Move matches on screen
                int bottomR = topRow - 1;
                int pos     = 2;
                int rowP3   = bottomR + 3;
                int rowP2   = bottomR + 2;
                int rowP1   = bottomR + 1;


                // Move non-matched components down
                for (int i = bottomR; i > -1; i--)
                {
                    if (pos == 0)
                    {
                        componentGrid[i + 3][topCol].transform.position = Vector3.MoveTowards(componentGrid[i + 3][topCol].transform.position, verticalMatch1, 4 * Time.deltaTime);
                    }
                    if (pos == 1)
                    {
                        componentGrid[i + 3][topCol].transform.position = Vector3.MoveTowards(componentGrid[i + 3][topCol].transform.position, verticalMatch2, 4 * Time.deltaTime);
                    }
                    if (pos == 2)
                    {
                        componentGrid[i + 3][topCol].transform.position = Vector3.MoveTowards(componentGrid[i + 3][topCol].transform.position, verticalMatch3, 4 * Time.deltaTime);
                    }
                    pos--;
                }

                // Move new components down on screen
                Vector3 position3   = new Vector2(topCol - 7, -1);
                Vector3 position2   = new Vector2(topCol - 7, 0);
                Vector3 firstMoved1 = componentGrid[rowP3][topCol].transform.position;
                Vector3 firstMoved2 = componentGrid[rowP2][topCol].transform.position;
                Vector3 firstMoved3 = componentGrid[rowP1][topCol].transform.position;

                n3.transform.position = Vector3.MoveTowards(n3.transform.position, position3, 4 * Time.deltaTime);
                n2.transform.position = Vector3.MoveTowards(n2.transform.position, position2, 4 * Time.deltaTime);

                // If non matched components have been moved down as well as the new components, done matching
                if (firstMoved3 == verticalMatch1 && firstMoved2 == verticalMatch2 && firstMoved1 == verticalMatch3)
                {
                    if (n3.transform.position == position3 && n2.transform.position == position2)
                    {
                        matching = false;
                        enemyDamage(type);
                        firstIteration = true;
                    }
                }
            }
            // Otherwise match horizontally
            else
            {
                int frontCol = getStringCol(currMatch[0]);
                int topRow   = getStringRow(currMatch[0]);

                // Change grid positions once, iterate to move the components on the screen
                if (firstIteration)
                {
                    type = componentGrid[topRow][frontCol].tag;
                    // Remove match from grid
                    GameObject match1 = componentGrid[topRow][frontCol];
                    GameObject match2 = componentGrid[topRow][frontCol + 1];
                    GameObject match3 = componentGrid[topRow][frontCol + 2];
                    componentGrid[topRow][frontCol]     = null;
                    componentGrid[topRow][frontCol + 1] = null;
                    componentGrid[topRow][frontCol + 2] = null;
                    // Destroy matched on screen
                    Destroy(match1);
                    Destroy(match2);
                    Destroy(match3);
                    // Move other objects down
                    int bottomRow = topRow - 1;

                    for (int i = bottomRow; i > -1; i--)
                    {
                        componentGrid[i + 1][frontCol]     = componentGrid[i][frontCol];
                        componentGrid[i + 1][frontCol + 1] = componentGrid[i][frontCol + 1];
                        componentGrid[i + 1][frontCol + 2] = componentGrid[i][frontCol + 2];
                        componentGrid[i][frontCol]         = null;
                        componentGrid[i][frontCol + 1]     = null;
                        componentGrid[i][frontCol + 2]     = null;

                        componentGrid[i + 1][frontCol].GetComponent <GridPosition>().setRowColumn(i + 1, frontCol);
                        componentGrid[i + 1][frontCol + 1].GetComponent <GridPosition>().setRowColumn(i + 1, frontCol + 1);
                        componentGrid[i + 1][frontCol + 2].GetComponent <GridPosition>().setRowColumn(i + 1, frontCol + 2);
                    }
                    // Replace top three with randoms
                    n1 = Instantiate(getRandomJewel(), new Vector2(0, 0), Quaternion.identity);
                    n2 = Instantiate(getRandomJewel(), new Vector2(0, 0), Quaternion.identity);
                    n3 = Instantiate(getRandomJewel(), new Vector2(0, 0), Quaternion.identity);
                    // Keep components random
                    while (n1.tag == n2.tag && n1.tag == n3.tag)
                    {
                        Destroy(n1);
                        Destroy(n2);
                        Destroy(n3);
                        n1 = Instantiate(getRandomJewel(), new Vector2(0, 0), Quaternion.identity);
                        n2 = Instantiate(getRandomJewel(), new Vector2(0, 0), Quaternion.identity);
                        n3 = Instantiate(getRandomJewel(), new Vector2(0, 0), Quaternion.identity);
                    }
                    // Position new components in grid and on screen
                    n3.GetComponent <GridPosition>().setRowColumn(0, frontCol + 2);
                    n3.transform.SetPositionAndRotation(new Vector2(frontCol - 5, 1), Quaternion.identity);
                    componentGrid[0][frontCol + 2] = n3;
                    n2.GetComponent <GridPosition>().setRowColumn(0, frontCol + 1);
                    n2.transform.SetPositionAndRotation(new Vector2(frontCol - 6, 1), Quaternion.identity);
                    componentGrid[0][frontCol + 1] = n2;
                    n1.GetComponent <GridPosition>().setRowColumn(0, frontCol);
                    n1.transform.SetPositionAndRotation(new Vector2(frontCol - 7, 1), Quaternion.identity);
                    componentGrid[0][frontCol] = n1;
                    firstIteration             = false;
                }

                // Move old components on screen
                int  bottomR = topRow - 1;
                bool done    = true;

                // Move non-matched components on screen
                for (int i = bottomR; i > -1; i--)
                {
                    componentGrid[i + 1][frontCol].transform.position = Vector3.MoveTowards(componentGrid[i + 1][frontCol].transform.position, new Vector2(frontCol - 7, -i), 4 * Time.deltaTime);
                    if (componentGrid[i + 1][frontCol].transform.position != new Vector3(frontCol - 7, -i))
                    {
                        done = false;
                    }
                    componentGrid[i + 1][frontCol + 1].transform.position = Vector3.MoveTowards(componentGrid[i + 1][frontCol + 1].transform.position, new Vector2(frontCol - 6, -i), 4 * Time.deltaTime);
                    if (componentGrid[i + 1][frontCol + 1].transform.position != new Vector3(frontCol - 6, -i))
                    {
                        done = false;
                    }
                    componentGrid[i + 1][frontCol + 2].transform.position = Vector3.MoveTowards(componentGrid[i + 1][frontCol + 2].transform.position, new Vector2(frontCol - 5, -i), 4 * Time.deltaTime);
                    if (componentGrid[i + 1][frontCol + 2].transform.position != new Vector3(frontCol - 5, -i))
                    {
                        done = false;
                    }
                }

                // If all components have been moved, done matching
                if (done)
                {
                    matching = false;
                    enemyDamage(type);
                    firstIteration = true;
                }
            }
            // If finished matching and there are still more matches, match again
            if (matching == false)
            {
                List <String[]> matches = checkMatch();
                if (matches.Count != 0)
                {
                    string[] match = matches[0];
                    updateMatches(componentGrid[getStringRow(match[0])][getStringCol(match[0])].tag);
                    currMatch = match;
                    matching  = true;
                    source.PlayOneShot(s_MagicTwinkle);
                }
            }
        }

        //updates the spell bar
        spellbarUpdate();

        // Check for next level when enemy dies
        if (enemyHealth <= 0.01f)
        {
            nextLevel();
        }
    }