Beispiel #1
0
    // END OF STATE MACHINE METHODS

    // Manages collision with other rigidbodys.
    // Player manages its own collisions with objects so enemies may collide with them on its own way.
    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.GetComponent <Obstacle>() != null) //Check if the collision was triggered by an obstacle
        {
            Obstacle obstacle = other.gameObject.GetComponent <Obstacle>();
            ObstacleCollide(obstacle);
        }
        if (other.gameObject.GetComponent <AIBehaviour>() != null) //Check if the collision was triggered by an obstacle
        {
            AIBehaviour enemy = other.gameObject.GetComponent <AIBehaviour>();
            enemy.Hitting(this);
        }
        if (other.gameObject.GetComponent <Pickup>() != null) //Check if the collision was triggered by an obstacle
        {
            Pickup objectToPick = other.gameObject.GetComponent <Pickup>();
            objectToPick.Disable();
            if (other.gameObject.GetComponent <Gun>() != null) //Check if the collision was triggered by an obstacle
            {
                int   currentNumGuns = _gunController.getGunNum();
                Gun[] newGuns        = new Gun[currentNumGuns + 1];
                if (currentNumGuns > 0)
                {
                    Gun[] currentGuns = _gunController.getGuns();
                    for (int i = 0; i < currentGuns.Length; i++)
                    {
                        newGuns[i] = currentGuns[i];
                    }
                }
                newGuns[newGuns.Length - 1] = other.gameObject.GetComponent <Gun>();
                _gunController.changeGuns(newGuns);
            }
            else if (other.gameObject.GetComponent <HealthPickup>() != null) //Check if the collision was triggered by a healthpack
            {
                HealthPickup healthPack = other.gameObject.GetComponent <HealthPickup>();
                if (_hp + healthPack.getHeal() > startingHp)
                {
                    _hp = startingHp;
                }
                else
                {
                    _hp += healthPack.getHeal();
                }
                Debug.Log("Player healed for " + healthPack.getHeal());
                Destroy(other.gameObject);
            }
            else
            {
                Destroy(other.gameObject);
            }
        }
    }