// Use this for initialization
    void Awake()
    {
        print("ball state is DEAD");
        ballState          = SuperBallState.DEAD;
        xZeroVelocityCount = 0;
        yZeroVelocityCount = 0;
        zZeroVelocityCount = 0;
        forward.Normalize();

        CannonBarrel = GameObject.Find("Cannon");
        rBody        = this.GetComponent <Rigidbody>();

        GameObject.Find("RoomCamera").GetComponent <Camera>().enabled = false;
        currentVelocityIncrement       = 0;
        isLastObjectBreakable          = true;
        isCurrentObjectBreakable       = true;
        liveStateOverridesFallingCheck = false;

        accumFloorDur = 0f;

        emptyGameObject      = GameObject.Find("CollisionPoints");
        collisionFolder      = Instantiate(emptyGameObject, emptyGameObject.transform.position, Quaternion.identity) as GameObject;
        collisionFolder.name = "CollisionFolder";
        collisionID          = 0;
    }
 void LaunchBall()
 {
     ExecuteEvents.Execute <IGameEventHandler>(GameObject.Find("GameManager"), null, (x, y) => x.FiredCannon());
     print("Adding force!");
     ballState = SuperBallState.LIVE;
     rBody.AddForce(CannonBarrel.transform.up.normalized * velocity * 0.6f, ForceMode.Impulse);
     lastCollisionLocation = this.transform.position;
     print("Current ball velocity: " + rBody.velocity.magnitude);
 }
 public void StartupBallCannon()
 {
     print("Startup ball");
     ballState                      = SuperBallState.ATREST;
     rBody.useGravity               = false;
     isLastObjectBreakable          = true;
     isCurrentObjectBreakable       = true;
     liveStateOverridesFallingCheck = false;
     accumFloorDur                  = 0f;
     currentVelocityIncrement       = 0;
 }
    void XZVelocityDecay()
    {
        if (Mathf.Abs(rBody.velocity.y) >= 0.1f)
        {
            return;
        }

        accumFloorDur += Time.deltaTime;
        print(accumFloorDur);
        Mathf.Lerp(rBody.velocity.x, 0f, accumFloorDur);
        Mathf.Lerp(rBody.velocity.z, 0f, accumFloorDur);

        if (accumFloorDur >= 1.0f) //alternatively use if(rBody.velocity.x == 0f) or if(rBody.velocity.z == 0f)
        {
            ballState      = SuperBallState.DEAD;
            rBody.velocity = Vector3.zero;
            ExecuteEvents.Execute <IGameEventHandler>(GameObject.Find("GameManager"), null, (x, y) => x.GameIsOver());
        }
    }
    private void HandleBreakableObjectCollision(float increment, Collision collision)
    {
        ExecuteEvents.Execute <IGameEventHandler>(GameObject.Find("GameManager"), null, (x, y) => x.UpdateScore(points, isCurrentObjectBreakable));

        /*Debug.Log((collisionID-1).ToString() + " BREAKABLE Object! At Position " + lastCollisionLocation +
         *  " Against object " + collision.transform.gameObject.name + "\n" +
         *  this.name + "'s speed: " + this.GetComponent<Rigidbody>().velocity.magnitude +
         *  " vector: " + GetComponent<Rigidbody>().velocity.ToString());*/

        if (ballState == SuperBallState.FALLING)
        {
            ballState        = SuperBallState.LIVE;
            rBody.useGravity = false;
            Debug.Log("Exiting FALLEN state, reentering LIVE state!");
            liveStateOverridesFallingCheck = true;
            //the velocity might be < gravityActivationThreshold and the state could reset to FALLING
            //this boolean allows us to override that.
        }

        if (rBody.velocity.magnitude < maxSpeed)
        {
            //print("increasing speed! Magnitude: " + rBody.velocity.magnitude + " maxSpeed: " + maxSpeed);
            this.IncrementVelocityFixed(increment);
        }
        else if (rBody.velocity.magnitude >= maxSpeed)
        {
            //Debug.Log("Speed is unchanged");
            return;
        }
        else if (rBody.velocity.magnitude + increment > maxSpeed)
        {
            //Debug.Log("Capping velocity");
            // Debug.Log("magnitude: " + rBody.velocity.magnitude);
            this.IncrementVelocityFixed(maxSpeed - rBody.velocity.magnitude);
        }

        /*Debug.Log("New adjusted stats: " + this.name + "'s speed: " +
         *  this.GetComponent<Rigidbody>().velocity.magnitude + " vector: " +
         *  GetComponent<Rigidbody>().velocity.ToString());*/
    }
 private void SwitchToFallingState()
 {
     ballState        = SuperBallState.FALLING;
     rBody.useGravity = true;
 }