示例#1
0
    void OnTriggerEnter2D(Collider2D target)
    {
        if (this.tag == "Player")
        {
            if ((target.gameObject.tag == "Enemy") && (currentMeleeInvuln >= maxMeleeInvuln))
            {
                float myPosX        = this.transform.position.x;
                float myPosY        = this.transform.position.y;
                float enemyPosX     = target.gameObject.transform.position.x;
                float enemyPosY     = target.gameObject.transform.position.y;
                float knockbackVelX = knockbackForce;
                float knockbackVelY = knockbackForce;

                if (myPosX < enemyPosX)                   //enemy to the right
                {
                    knockbackVelX *= -1;
                }
                if (myPosY < enemyPosY)                 //enemy is above
                {
                    knockbackVelY *= -1;
                }

                body2d.velocity = new Vector2(knockbackVelX, knockbackVelY);

                collisionState.stunned     = true;             //stuns player temporarily
                collisionState.timeStunned = 0f;

                currentHP -= 1;                 //player always takes 1 damage.
                PlayerPrefs.SetInt("HP" + PlayerPrefs.GetInt("SelectedPlayer", 0), currentHP);
                currentMeleeInvuln = 0f;        //make player unable to take damage for a time;
            }
            if (currentHP < 1)
            {
                gameObject.SetActive(false);
                DeathMenu menu = (DeathMenu)deathCanvas.GetComponent("DeathMenu");
                menu.playerDeath();
                PlayerPrefs.SetInt("HP" + PlayerPrefs.GetInt("SelectedPlayer", 0), 2);
            }
        }
        else if (this.tag == "Enemy")
        {
            if ((target.gameObject.tag == "PlayerSword") && (currentMeleeInvuln >= maxMeleeInvuln))
            {
                Sword sword = target.gameObject.transform.parent.gameObject.GetComponent <Sword>();
                damage             = sword.damage;     //get damage amount from sword
                currentHP         -= damage;
                currentMeleeInvuln = 0f;
            }

            //GAA 5 MAR 2016 Note 1: added an if statement for nested enemy structures (e.g. Flying Enemy sprite with multiple colliders)
            if (currentHP < 1)
            {
                if (gameObject.transform.parent != null && gameObject.transform.parent.CompareTag("Enemy"))
                {
                    gameObject.transform.parent.gameObject.SetActive(false);
                }
                else
                {
                    gameObject.SetActive(false);
                }
            }

//			//GAA 5 MAR 2016 Note 2: Previous implementation of deactivating enemy gameobjects
//			if(currentHP < 1){
//				gameObject.SetActive (false);
//			}
        }
        else if (this.tag == "CL2Boss")         //added by ivan on 03/31/2016 for boss Testing
        {
            if ((target.gameObject.tag == "PlayerSword") && (currentMeleeInvuln >= maxMeleeInvuln) && this.gameObject.GetComponent <FrogTongue>().autostart == true)
            {
                Sword sword = target.gameObject.transform.parent.gameObject.GetComponent <Sword>();
                //damage = sword.damage; //get damage amount from sword
                damage = 1;
                this.gameObject.GetComponent <FrogTongue> ().hit = true;
                currentHP -= damage;
                //StartCoroutine ("damageAnimation");
                //StartCoroutine("damageAnim", this.gameObject.transform.parent);
                //damageAnimHandler(this.gameObject.transform.parent.FindChild("sample_boss_head"), "clBoss");
                object[] parameters = new object[3] {
                    this.gameObject.transform.parent.FindChild("sample_boss_head"), Color.red, Color.magenta
                };
                StartCoroutine("damageAnim", parameters);

                currentMeleeInvuln = 0f;
            }
            if (currentHP < 1)
            {
                Debug.Log("Here");
                object[] parameters = new object[4] {
                    this.gameObject.transform.parent.FindChild("sample_boss_head"), Color.white, Color.clear, "death"
                };
                StartCoroutine("damageAnim", parameters);

                /*
                 * if (gameObject.transform.parent != null && gameObject.transform.parent.CompareTag ("CL2Boss")) {
                 *      gameObject.transform.parent.gameObject.SetActive (false);
                 * } else {
                 *      //gameObject.SetActive (false);//disables just tongue
                 *      gameObject.transform.parent.gameObject.SetActive (false);//disables head and tongue
                 * }
                 */
            }
        }
    }