// Use this for initialization
 void Start()
 {
     //Find the player ship -Adam
     mPlayer = FindObjectOfType<PlayerShipController>();
     //Find the second player's ship ~Adam
     if(mPlayerTwoUI && mPlayer.mPlayerTwo != null)
     {
         mPlayerTwo = mPlayer.mPlayerTwo;
     }
 }
    // Use this for initialization
    void Start()
    {
        swirlStartY = mPowerTimerSwirl.transform.position.y;

        //Find the player ship -Adam
        mPlayer = FindObjectOfType<PlayerShipController>();
        //Find the second player's ship ~Adam
        if(mPlayerTwoUI && mPlayer.mPlayerTwo != null)
        {
            mPlayerTwo = mPlayer.mPlayerTwo;
        }
    }
 // Use this for initialization
 void Start()
 {
     //Find the score manager and the player ships ~Adam
     if(FindObjectOfType<ScoreManager>() != null)
     {
         mScoreMan = FindObjectOfType<ScoreManager>();
     }
     if(FindObjectOfType<PlayerShipController>() != null)
     {
         mP1Ship = FindObjectOfType<PlayerShipController>();
     }
     if(FindObjectOfType<PlayerTwoShipController>() != null)
     {
         mP2Ship = FindObjectOfType<PlayerTwoShipController>();
     }
 }
    // Update is called once per frame
    void Update()
    {
        if(mSteamUp == null)
        {
            mSteamUp = GameObject.Find(mSteamUpName).GetComponent<ParticleSystem>();
        }
        else
        {
            if(mPlayer.heatLevel/mPlayer.maxHeatLevel < 0.9f && !mSteamUp.GetComponent<AudioSource>().isPlaying)
            {
                mCanPlaySteamNoise = true;
            }
        }
        if(mSteamDown == null)
        {
            mSteamDown = GameObject.Find(mSteamDownName).GetComponent<ParticleSystem>();
        }

        //Read Heat Level, Maximum Heat Level, and overheat status from either player 1 or player 2 ~Adam
        if(!mPlayerTwoUI && mPlayer != null)
        {
            mHeatLevel = mPlayer.heatLevel;
            mMaxHeat = mPlayer.maxHeatLevel;
            mIsOverheated = mPlayer.isOverheated;
        }
        else if (mPlayerTwoUI && mPlayerTwo != null)
        {
            mHeatLevel = mPlayerTwo.heatLevel;
            mMaxHeat = mPlayerTwo.maxHeatLevel;
            mIsOverheated = mPlayerTwo.isOverheated;
        }

        //Safety in case the player ship connection is lost -Adam
        if(mPlayer == null)
        {
            mPlayer = FindObjectOfType<PlayerShipController>();
        }

        else if (mPlayerTwo == null && mPlayerTwoUI && mPlayer.mPlayerTwo != null)
        {
            mPlayerTwo = mPlayer.mPlayerTwo;
        }

        else if(GetComponent<Image>().canvas.isActiveAndEnabled) //Only do stuff when the canvas is actually turned on
        {
            //Make the bar move up and down
            mOverHeatBar.GetComponent<RectTransform>().localScale = new Vector3(1f, mHeatLevel/mMaxHeat, 1f);

            //Display overlay when overheated
            if(mIsOverheated)
            {
                mOverHeatOverlay.enabled = true;
                GetComponent<Animator>().speed = 0f;
                if(mSteamUp != null && mSteamDown != null)
                {
                    mSteamUp.Play();
                    mSteamUp.GetComponentInChildren<ParticleSystem>().Play();
                    mSteamDown.Stop();
                }
                mRedBulb.enabled = false;
                mBlankBulb.enabled = false;
            }
            else
            {
                if(mSteamUp != null & mSteamDown != null)
                {
                    mSteamDown.Play();
                    mSteamUp.Stop();
                    mSteamUp.GetComponentInChildren<ParticleSystem>().Stop();
                }
                mOverHeatOverlay.enabled = false;
                if(mHeatLevel/mMaxHeat > 0.9f)
                {
                    mRedBulb.enabled = true;
                    mBlankBulb.enabled = false;

                    GetComponent<Animator>().speed = 5f;
                    if(mSteamDown != null)
                    {
                        mSteamDown.startSpeed = 5f;
                        mSteamDown.startLifetime = 0.5f;
                        mSteamDown.emissionRate = 50f;
                        if(mSteamUp != null && mCanPlaySteamNoise)
                        {
                            mSteamUp.GetComponent<AudioSource>().Play();
                            mCanPlaySteamNoise = false;
                        }
                    }
                }
                else
                {
                    mRedBulb.enabled = false;
                    mBlankBulb.enabled = true;

                    GetComponent<Animator>().speed = 1f;
                    if(mSteamDown != null)
                    {
                        mSteamDown.startSpeed = 1f;
                        mSteamDown.startLifetime = 2f;
                        mSteamDown.emissionRate = 10f;
                    }
                }
            }
        }
        //Hide the steam if the canvas it turned off or the ship is missing
        else
        {
            if(mSteamUp != null & mSteamDown != null)
            {
                mSteamUp.Stop();
                mSteamUp.GetComponentInChildren<ParticleSystem>().Stop();
                mSteamDown.Stop();
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        if(mScoreMan != null)
        {
            mHealthValue = mScoreMan.mLivesRemaining/(mScoreMan.mMaxLives+.00001f);

            //Flash the ship parts ~Adam
            if(mScoreMan.mPlayerSafeTime >0f)
            {
                mShipHull.GetComponent<Animator>().SetInteger("UIFlashState", 1);
                mShipLeftWing.GetComponent<Animator>().SetInteger("UIFlashState", 1);
                mShipRightWing.GetComponent<Animator>().SetInteger("UIFlashState", 1);
                mShipLeftClaw.GetComponent<Animator>().SetInteger("UIFlashState", 1);
                mShipRightClaw.GetComponent<Animator>().SetInteger("UIFlashState", 1);
            }
            else
            {
                mShipHull.GetComponent<Animator>().SetInteger("UIFlashState", 0);
                mShipLeftWing.GetComponent<Animator>().SetInteger("UIFlashState", 0);
                mShipRightWing.GetComponent<Animator>().SetInteger("UIFlashState", 0);
                mShipLeftClaw.GetComponent<Animator>().SetInteger("UIFlashState", 0);
                mShipRightClaw.GetComponent<Animator>().SetInteger("UIFlashState", 0);
            }
        }

        if(mHealthValue<0.8f)
        {
            mShipRightClaw.GetComponent<Animator>().SetInteger("UIFlashState", 2);
        }
        if(mHealthValue<0.6f)
        {
            mShipLeftClaw.GetComponent<Animator>().SetInteger("UIFlashState", 2);
        }
        if(mHealthValue<0.4f)
        {
            mShipRightWing.GetComponent<Animator>().SetInteger("UIFlashState", 2);
        }
        if(mHealthValue<0.2f)
        {
            mShipLeftWing.GetComponent<Animator>().SetInteger("UIFlashState", 2);
        }
        if(mHealthValue < 0f)
        {
            mHealthValue = 0f;
        }

        if(!mP2UI && mP1Ship != null)
        {
            //Adjust the meters ~Adam
            mOverheatValue = mP1Ship.heatLevel/mP1Ship.maxHeatLevel;
            if(mOverheatValue < 0f)
            {
                mOverheatValue = 0f;
            }

            mTripleTimerValue = mP1Ship.mThreeBulletTimer/30f;
            if(mTripleTimerValue < 0f)
            {
                mTripleTimerValue = 0f;
            }

            //Flash the ship gun parts ~Adam
            if(mScoreMan != null)
            {
                if(mP1Ship.mThreeBullet)
                {
                    if(mScoreMan.mPlayerSafeTime >0f)
                    {
                        mShipLeftGun.GetComponent<Animator>().SetInteger("UIFlashState", 1);
                        mShipRightGun.GetComponent<Animator>().SetInteger("UIFlashState", 1);
                    }
                    else
                    {
                        mShipLeftGun.GetComponent<Animator>().SetInteger("UIFlashState", 0);
                        mShipRightGun.GetComponent<Animator>().SetInteger("UIFlashState", 0);
                    }
                }
                else
                {
                    mShipLeftGun.GetComponent<Animator>().SetInteger("UIFlashState", 2);
                    mShipRightGun.GetComponent<Animator>().SetInteger("UIFlashState", 2);
                }
            }
            //Show this player's individual score ~Adam
            mScoreText.text = "P1 Score: " + mScoreMan.mP1Score;
        }
        else if(mP2UI && mP2Ship != null)
        {
            //Adjust the meters ~Adam
            mOverheatValue = mP2Ship.heatLevel/mP2Ship.maxHeatLevel;
            if(mOverheatValue < 0f)
            {
                mOverheatValue = 0f;
            }

            mTripleTimerValue = mP2Ship.mThreeBulletTimer/30f;
            if(mTripleTimerValue < 0f)
            {
                mTripleTimerValue = 0f;
            }

            //Flash the ship gun parts ~Adam
            if(mScoreMan != null)
            {
                if(mP2Ship.mThreeBullet)
                {
                    if(mScoreMan.mPlayerSafeTime >0f)
                    {
                        mShipLeftGun.GetComponent<Animator>().SetInteger("UIFlashState", 1);
                        mShipRightGun.GetComponent<Animator>().SetInteger("UIFlashState", 1);
                    }
                    else
                    {
                        mShipLeftGun.GetComponent<Animator>().SetInteger("UIFlashState", 0);
                        mShipRightGun.GetComponent<Animator>().SetInteger("UIFlashState", 0);
                    }
                }
                else
                {
                    mShipLeftGun.GetComponent<Animator>().SetInteger("UIFlashState", 2);
                    mShipRightGun.GetComponent<Animator>().SetInteger("UIFlashState", 2);
                }
            }

            //Show this player's individual score ~Adam
            mScoreText.text = "P2 Score: " + mScoreMan.mP2Score;
        }

        //Control the overheat whistle noise
        if(mOverheatValue  < 0.9f && GetComponent<AudioSource>().isPlaying)
        {
            mCanPlaySteamNoise = true;
        }
        else if (mOverheatValue > 0.9f && mCanPlaySteamNoise)
        {
            GetComponent<AudioSource>().Play();
            mCanPlaySteamNoise = false;
        }

        //Find ships if they're null
        else if (!mP2UI && mP1Ship == null)
        {
            if(FindObjectOfType<PlayerShipController>() != null)
            {
                mP1Ship = FindObjectOfType<PlayerShipController>();
            }
        }
        else if (mP2UI && mP2Ship == null)
        {
            if(FindObjectOfType<PlayerTwoShipController>() != null)
            {
                mP2Ship = FindObjectOfType<PlayerTwoShipController>();
            }
        }

        //Set the bar sizes ~Adam
        mHealthBar.rectTransform.localScale = new Vector3(mHealthValue, 1f,1f);
        mOverheatBar.rectTransform.localScale = new Vector3(mOverheatValue, 1f,1f);
        mTripleTimerBar.rectTransform.localScale = new Vector3(mTripleTimerValue, 1f,1f);
    }