public void TakeDamage(float damageAmount, string damageElement, bool crit)
    {
        string enemyElement = enemy.enemyType.ToString().ToUpper();
        string audioPath    = "audio/eq_hero_attack";

        //environment damage factor
        float environmentDamage = 1.0f;

        if (enemyElement.Equals(battleSM.environmentElement))
        {
            environmentDamage = 0.8f; //20% dmg reduction on matching element w environment
        }

        //stacked element factor
        float  elementDamage  = elementalDamage(damageElement, enemyElement);
        string lastHitElement = enemy.lastHitType;

        if (damageElement.Equals(lastHitElement) && elementDamage != 1f)
        {
            elementDamage *= elementDamage; //square the elemental damage if the same element was used back to back
            stackText.SetActive(true);
        }
        enemy.lastHitType = damageElement;

        float calculatedDamage = damageAmount * environmentDamage * elementDamage;
        float roundedDamage    = Mathf.RoundToInt(calculatedDamage);

        damageText.GetComponent <TextMesh>().text = roundedDamage.ToString();
        damageText.SetActive(true);
        if (crit)
        {
            critText.SetActive(true);
            audioPath = "audio/eq_hero_crit";
        }
        if (elementDamage > 1)
        {
            weakText.SetActive(true);
            audioPath = "audio/eq_magic_weak";
        }
        else if (elementDamage < 1)
        {
            resistText.SetActive(true);
            audioPath = "audio/eq_magic_resist";
        }

        AudioClip enemyAttackAudio = (AudioClip)Resources.Load(audioPath);

        battleSM.GetComponent <AudioSource>().PlayOneShot(enemyAttackAudio, 0.5f);

        enemy.currHP -= roundedDamage;
        if (enemy.currHP <= 0)
        {
            enemy.currHP = 0;
            currentState = TurnState.DEAD;
        }

        StartCoroutine(HideDamageText(0.7f));
    }
    /// <summary>
    /// Checks collider to know what the hero is interacting with and acts accordingly.
    /// </summary>
    /// <param name = "other">Collided object</param>
    void OnTriggerEnter2D(Collider2D other)
    {
        if (!isInFight)
        {
            // for map transition

            /*if (other.tag == "Teleporter") {
             *  GameManager.instance.nextSpawnPoint = col.spawnPointName;
             *  GameManager.instance.sceneToLoad = col.sceneToLoad;
             * }*/
            // for interaction with items
            if (other.tag == "Item")
            {
            }
            // for battle
            if (other.tag == "Enemy")
            {
                //isInFight = true;
                GameObject.Find("Blende").GetComponent <BlendenController>().blenden();
                isInFight = true;
                GameManager.instance.gui.SetActive(true);
                BattleStateMachine  bsm   = GameObject.Find("BattleManager").GetComponent <BattleStateMachine>();
                CharacterAmountType enemy = other.gameObject.GetComponent <CharacterAmountType>();
                CharacterAmountType hero  = this.gameObject.GetComponent <CharacterAmountType>();
                bsm.SpawnCharacters(enemy.characters.Count, enemy.characters, spawnPointsEnemies, false);
                bsm.SpawnCharacters(hero.characters.Count, hero.characters, spawnPointsHeros, true);
                this.gameObject.SetActive(false);
                other.gameObject.SetActive(false);
                GameManager.instance.collidedEnemy = other.gameObject;
                GameManager.PlayMusic(bsm.GetComponent <AudioSource>());
            }
        }
    }
Beispiel #3
0
    /// <summary>
    /// Checks collider to know what the hero is interacting with and acts accordingly.
    /// </summary>
    /// <param name = "other">Collided object</param>
    void OnTriggerEnter2D(Collider2D other)
    {
        if (!isInFight)
        {
            // for map transition

            /*if (other.tag == "Teleporter") {
             *  GameManager.instance.nextSpawnPoint = col.spawnPointName;
             *  GameManager.instance.sceneToLoad = col.sceneToLoad;
             * }*/
            // for interaction with items
            if (other.tag == "Item")
            {
            }
            if (other.tag == "NextLevel")
            {
                GameManager gm = GameObject.Find("Game Manager").GetComponent <GameManager>();
                gm.LoadNextScene("DesertLevel");
            }
            // for battle
            if (other.tag == "Enemy")
            {
                GameObject.Find("Blende").GetComponent <BlendenController>().blenden();
                isInFight = true;
                GameManager.instance.gui.SetActive(true);
                BattleStateMachine  bsm   = GameObject.Find("BattleManager").GetComponent <BattleStateMachine>();
                CharacterAmountType enemy = other.gameObject.GetComponent <CharacterAmountType>();
                CharacterAmountType hero  = this.gameObject.GetComponent <CharacterAmountType>();
                bsm.SpawnCharacters(enemy.characters.Count, enemy.characters, spawnPointsEnemies, false);
                bsm.SpawnCharacters(hero.characters.Count, hero.characters, spawnPointsHeros, true);
                this.gameObject.SetActive(false);
                other.gameObject.SetActive(false);
                GameManager.instance.collidedEnemy = other.gameObject;

                if (other.gameObject.name == "JnR_Robot(Clone)")
                {
                    AudioClip audio = (AudioClip)Resources.Load("Audio/Music/Battle_Boss_Loop");
                    bsm.GetComponent <AudioSource>().clip = audio;
                    GameManager.PlayMusic(bsm.GetComponent <AudioSource>());
                }
                else
                {
                    GameManager.PlayMusic(bsm.GetComponent <AudioSource>());
                }
            }
        }
    }
    private IEnumerator TimeForAction()
    {
        if (actionStarted)
        {
            yield break;
        }

        actionStarted = true;

        string                 attackingElement = battleSM.performList[0].chosenAttack.attackName.ToUpper();
        bool                   showParticles    = true;
        ParticleSystem         elementParticles = gameObject.GetComponentInParent <ParticleSystem>();
        ParticleSystemRenderer elementRender    = elementParticles.GetComponent <ParticleSystemRenderer>();
        ParticleSystem         damageParticles  = elementDamage.GetComponentInParent <ParticleSystem>();
        ParticleSystemRenderer damageRender     = damageParticles.GetComponent <ParticleSystemRenderer>();

        switch (attackingElement)
        {
        case "WOOD":
            elementRender.material = new Material(Resources.Load <Material>("WoodParticle"));
            damageRender.material  = new Material(Resources.Load <Material>("WoodParticle"));
            break;

        case "FIRE":
            elementRender.material = new Material(Resources.Load <Material>("FireParticle"));
            damageRender.material  = new Material(Resources.Load <Material>("FireParticle"));
            break;

        case "EARTH":
            elementRender.material = new Material(Resources.Load <Material>("EarthParticle"));
            damageRender.material  = new Material(Resources.Load <Material>("EarthParticle"));
            break;

        case "METAL":
            elementRender.material = new Material(Resources.Load <Material>("MetalParticle"));
            damageRender.material  = new Material(Resources.Load <Material>("MetalParticle"));
            break;

        case "WATER":
            elementRender.material = new Material(Resources.Load <Material>("WaterParticle"));
            damageRender.material  = new Material(Resources.Load <Material>("WaterParticle"));
            break;

        default:
            showParticles = false;
            break;
        }

        //turn on particle effect
        if (showParticles)
        {
            elementParticles.Play();
            damageParticles.Stop();
            AudioClip enemyAttackAudio = (AudioClip)Resources.Load("audio/eq_hero_magic");
            battleSM.GetComponent <AudioSource>().PlayOneShot(enemyAttackAudio, 0.5f);
        }

        //animate the enemy near the target to attack
        Vector3 targetPosition = new Vector3(actionTarget.transform.position.x + 1.5f, actionTarget.transform.position.y, actionTarget.transform.position.z);

        while (MoveTowardsTarget(targetPosition))
        {
            yield return(null);
        }

        if (showParticles)
        {
            damageParticles.Play();
        }

        //wait
        yield return(new WaitForSeconds(0.6f));

        if (showParticles)
        {
            damageParticles.Stop();
        }

        //do damage
        DoDamage();

        //turn off particle effect
        if (showParticles)
        {
            elementParticles.Stop();
        }

        //animate back to start position
        Vector3 firstPosition = startPosition;

        while (MoveTowardsTarget(firstPosition))
        {
            yield return(null);
        }

        //remove this performer from the list in battleSM
        battleSM.performList.RemoveAt(0);

        //reset the battleSM
        if (battleSM.battleState != BattleStateMachine.PerformAction.WIN && battleSM.battleState != BattleStateMachine.PerformAction.LOSE)
        {
            battleSM.battleState = BattleStateMachine.PerformAction.WAIT;
            battleSM.UpdateTurnOrder();
            //reset this state
            float delayFactor = 5f - delayValue;
            currCooldown = Mathf.Clamp(delayFactor, 0, 4);
            currentState = TurnState.PROCESSING;
        }
        else
        {
            currentState = TurnState.WAITING;
        }

        //end coroutine
        actionStarted = false;
    }