Exemple #1
0
    public IEnumerator Fight(GridSquare enemy)
    {
        SFXHandler sfx = GameObject.Find("SFX Source").GetComponent <SFXHandler>();

        fighting = true;
        running  = false;
        key_lock = true;
        yield return(new WaitForSeconds(0.5f));

        sfx.playCombat();
        for (int i = 0; i < 4; i++)
        {
            this.gameObject.transform.GetChild(i).GetComponent <Animator>().SetTrigger("fight");
            if (i == 2 || i == 0 || i == 3)
            {
                for (int j = 0; j < 4; j++)
                {
                    this.gameObject.transform.GetChild(i).GetChild(j).GetComponent <Animator>().SetTrigger("fight");
                }
            }
            else if (i == 1)
            {
                for (int j = 0; j < 5; j++)
                {
                    this.gameObject.transform.GetChild(i).GetChild(j).GetComponent <Animator>().SetTrigger("fight");
                }
            }
        }
        string attacker_name = (enemy.item.gameObject.name.Split('('))[0].ToLower();

        enemy.item.gameObject.GetComponent <Animator>().SetTrigger("fight");
        if (attacker_name == "wraith")
        {
            for (int i = 0; i < 3; i++)
            {
                enemy.item.gameObject.transform.GetChild(i).GetComponent <Animator>().SetTrigger("fight");
            }
        }
        else if (attacker_name == "lich")
        {
            for (int i = 0; i < 4; i++)
            {
                enemy.item.gameObject.transform.GetChild(i).GetComponent <Animator>().SetTrigger("fight");
            }
        }
        LogPrint("> The party has encountered a " + attacker_name + "!\n");
        while (HEALTH > 0 && enemy.item.GetComponent <EnemyStats>().GetHealth() > 0)
        {
            // Enemy attacks
            if (Random.Range(0f, 1f) <= enemy.item.GetComponent <EnemyStats>().GetHitrate())
            {
                if (attacker_name == "zombie" || attacker_name == "ghost")
                {
                    enemy.item.gameObject.GetComponent <Animator>().SetTrigger("slap");
                }
                else if (attacker_name == "skeleton")
                {
                    enemy.item.gameObject.GetComponent <Animator>().SetTrigger("slap");
                }
                else if (attacker_name == "wraith")
                {
                    enemy.item.gameObject.GetComponent <Animator>().SetTrigger("cast");
                    for (int i = 0; i < 3; i++)
                    {
                        enemy.item.gameObject.transform.GetChild(i).GetComponent <Animator>().SetTrigger("cast");
                    }
                }
                else if (attacker_name == "lich")
                {
                    enemy.item.gameObject.GetComponent <Animator>().SetTrigger("cast");
                    for (int i = 0; i < 4; i++)
                    {
                        enemy.item.gameObject.transform.GetChild(i).GetComponent <Animator>().SetTrigger("cast");
                    }
                }

                if (Mathf.Abs(x_pos - enemy.item.GetComponent <EnemyStats>().gameObject.transform.position.x) > 1 || Mathf.Abs(z_pos - enemy.item.GetComponent <EnemyStats>().gameObject.transform.position.z) > 1)
                {
                    takeDamage(enemy.item.GetComponent <EnemyStats>().GetRangedDamage());
                    LogPrint("> The party takes " + enemy.item.GetComponent <EnemyStats>().GetRangedDamage() + " damage!\n");
                    GameObject.Find("Health").GetComponent <UnityEngine.UI.Text>().text = "HP: " + HEALTH;
                }
                else
                {
                    takeDamage(enemy.item.GetComponent <EnemyStats>().GetMeleeDamage());
                    LogPrint("> The party takes " + enemy.item.GetComponent <EnemyStats>().GetMeleeDamage() + " damage!\n");
                    GameObject.Find("Health").GetComponent <UnityEngine.UI.Text>().text = "HP: " + HEALTH;
                }
            }

            yield return(new WaitForSeconds(1.5f));

            // Party attacks
            if (Random.Range(0f, 1f) <= 0.67f)
            {
                enemy.item.GetComponent <EnemyStats>().TakeDamage(DAMAGE);
                LogPrint("> The party deals " + DAMAGE + " damage!\n");
                for (int i = 0; i < 4; i++)
                {
                    this.gameObject.transform.GetChild(i).GetComponent <Animator>().SetTrigger(attacks[i]);
                    if (i == 2 || i == 0 || i == 3)
                    {
                        for (int j = 0; j < 4; j++)
                        {
                            this.gameObject.transform.GetChild(i).GetChild(j).GetComponent <Animator>().SetTrigger(attacks[i]);
                        }
                    }
                    else if (i == 1)
                    {
                        for (int j = 0; j < 5; j++)
                        {
                            this.gameObject.transform.GetChild(i).GetChild(j).GetComponent <Animator>().SetTrigger(attacks[i]);
                        }
                    }
                }
                LogPrint("> The enemy now has " + enemy.item.GetComponent <EnemyStats>().GetHealth() + " HP.\n");
            }

            yield return(new WaitForSeconds(2.5f));
        }

        string enemy_name = enemy.item.name;

        if (enemy.item.GetComponent <EnemyStats>().GetHealth() <= 0 || HEALTH <= 0)
        {
            if (!grid.firstSkeleton && enemy_name == "Skeleton(Clone)")
            {
                GameObject.Find("PlotWindow").GetComponent <DialogueHandler>().SkeletonDialogue();
                grid.firstSkeleton = true;
            }

            if (!grid.firstZombie && enemy_name == "Zombie(Clone)")
            {
                GameObject.Find("PlotWindow").GetComponent <DialogueHandler>().ZombieDialogue();
                grid.firstZombie = true;
            }

            if (!grid.firstGhost && enemy_name == "Ghost(Clone)")
            {
                GameObject.Find("PlotWindow").GetComponent <DialogueHandler>().GhostDialogue();
                grid.firstGhost = true;
            }

            if (!grid.firstWraith && enemy_name == "Wraith(Clone)")
            {
                GameObject.Find("PlotWindow").GetComponent <DialogueHandler>().WraithDialogue();
                grid.firstWraith = true;
            }
        }

        if (HEALTH <= 0)
        {
            LogPrint("> The party has died!\n");
            Destroy(this.gameObject);
            sfx.stopCombat();
        }
        else if (enemy.item.GetComponent <EnemyStats>().GetHealth() <= 0)
        {
            LogPrint("> The enemy has been slain!\n");
            EXP += enemy.item.GetComponent <EnemyStats>().GetEXP();
            GameObject.Find("Strength").GetComponent <UnityEngine.UI.Text>().text = "EXP: " + EXP;
            Destroy(enemy.item.gameObject);
            enemy.resetSquare();
            this.gameObject.transform.LookAt(prev_facing);
            sfx.stopCombat();
        }
        key_lock = false;
    }