Пример #1
0
    void OnCollisionEnter2D(Collision2D col)
    {
        // If the colliding gameobject is an Enemy...
        if (col.gameObject.tag == "Enemy")
        {
            // ... and if the time exceeds the time of the last hit plus the time between hits...
            if (Time.time > lastHitTime + repeatDamagePeriod)
            {
                // Using Mechanic to find if collision object is Human or not - Text is empty or not
                EnemyShooterMechanic temp = (EnemyShooterMechanic)GameInfo.currentMechanics;
                temp.sendHook(col.gameObject.GetComponentInChildren <TextMesh>().text);

                if ((FrameworkCore.currentContent.wasLastActionValid()))
                {
                    // ... and if the player still has health...
                    if (health > 0f)
                    {
                        // ... take damage and reset the lastHitTime.
                        TakeDamage(col.transform);
                        lastHitTime = Time.time;
                    }
                    // If the player doesn't have health, do some stuff, let him fall into the river to reload the level.
                    else
                    {
                        // Find all of the colliders on the gameobject and set them all to be triggers.
                        Collider2D[] cols = GetComponents <Collider2D>();
                        foreach (Collider2D c in cols)
                        {
                            c.isTrigger = true;
                        }

                        // Move all sprite parts of the player to the front
                        SpriteRenderer[] spr = GetComponentsInChildren <SpriteRenderer>();
                        foreach (SpriteRenderer s in spr)
                        {
                            s.sortingLayerName = "UI";
                        }

                        // ... disable user Player Control script
                        GetComponent <PlayerControl>().enabled = false;

                        // ... disable the Gun script to stop a dead guy shooting a nonexistant bazooka
                        GetComponentInChildren <Gun>().enabled = false;

                        // ... Trigger the 'Die' animation state
                        anim.SetTrigger("Die");
                    }
                }
            }
        }
    }
Пример #2
0
    void Death()
    {
        // Find all of the sprite renderers on this object and it's children.
        SpriteRenderer[] otherRenderers = GetComponentsInChildren <SpriteRenderer>();

        // Disable all of them sprite renderers.
        foreach (SpriteRenderer s in otherRenderers)
        {
            s.enabled = false;
        }

        // Re-enable the main sprite renderer and set it's sprite to the deadEnemy sprite.
        ren.enabled = true;
        ren.sprite  = deadEnemy;

        // Using Mechanic to find if current object is Human or not - Text is empty or not
        EnemyShooterMechanic temp = (EnemyShooterMechanic)GameInfo.currentMechanics;

        temp.sendHook(transform.GetComponentInChildren <TextMesh>().text);

        if ((FrameworkCore.currentContent.wasLastActionValid()))
        {
            // Increase the score by 100 points
            score.score += 100;
            disease      = transform.Find("germText").GetComponentInChildren <TextMesh> ().text;

            if (!PlayerPrefs.GetString("Diseases").Contains(disease))
            {
                PlayerPrefs.SetString("Diseases", PlayerPrefs.GetString("Diseases") + "\n" + disease);
            }
        }
        else
        {
            // Decrease the score by 100 points
            score.score -= 100;
        }
        // Set dead to true.
        dead = true;

        // Allow the enemy to rotate and spin it by adding a torque.
        GetComponent <Rigidbody2D>().fixedAngle = false;
        GetComponent <Rigidbody2D>().AddTorque(Random.Range(deathSpinMin, deathSpinMax));

        // Find all of the colliders on the gameobject and set them all to be triggers.
        Collider2D[] cols = GetComponents <Collider2D>();
        foreach (Collider2D c in cols)
        {
            c.isTrigger = true;
        }

        // Play a random audioclip from the deathClips array.
        int i = Random.Range(0, deathClips.Length);

        AudioSource.PlayClipAtPoint(deathClips[i], transform.position);

        // Create a vector that is just above the enemy.
        Vector3 scorePos;

        scorePos    = transform.position;
        scorePos.y += 1.5f;

        // Instantiate the 100 points prefab at this point.
        Instantiate(hundredPointsUI, scorePos, Quaternion.identity);
    }