123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617 |
- /*
- * FUNCITON:
- * - This script holds the global variables that the other scripts are dependent on.
- * - It provides interaction among scripts.
- * - This script controls game states (launch, pause, death etc.)
- *
- * USED BY: This script is a part of the "Player" prefab.
- *
- */
- using UnityEngine;
- using System.Collections;
- using System;
- using UnityEngine.SceneManagement;
- public class InGameScriptCS : MonoBehaviour {
- public static InGameScriptCS Instance { get; private set; }
- public bool Resurected;
- private int CurrentEnergy = 100; //player's energy (set to zero on death)
- private int iLevelScore = 0; //current score (calculated based on distance traveled)
- private int iCurrencyCount = 9000;//
- private bool customMenuEnabled = false;
- public UILabel DistanceGameOver;
- public UILabel GiftsGameOver;
- public UILabel PointsGameOver;
- //script references
- private MenuScriptCS hMenuScriptCS;
- private NGUIMenuScript hNGUIMenuScript;
- private ControllerScriptCS hControllerScriptCS;
- private SoundManagerCS hSoundManagerCS;
- private PowerupsMainControllerCS hPowerupsMainControllerCS;
- private EnemyControllerCS hEnemyControllerCS;
- private CameraControllerCS hCameraControllerCS;
- private MissionsControllerCS hMissionsControllerCS;
- private GlobalAchievementControllerCS hGlobalAchievementControllerCS;
-
- private int iPauseStatus = 0;
- public int iDeathStatus = 0;
- private int iMenuStatus;
-
- private bool bGameOver = false;
- public bool bGamePaused = false;
-
- // Use this for initialization
- void Start ()
- {
- Instance = this;
- PlayerPrefs.SetInt("AdCounter", 0);
- PlayerPrefs.Save();
- //PlayerPrefs.DeleteAll(); //DEBUG
- Application.targetFrameRate = 60; //ceiling the frame rate on 60 (debug only)
-
- RenderSettings.fog = true; //turn on fog on launch
-
- if (GameObject.Find("MenuGroup"))//check while type of menu is active (custom or ngui)
- {
- customMenuEnabled = true;
- hMenuScriptCS = (MenuScriptCS)GameObject.Find("MenuGroup").GetComponent(typeof(MenuScriptCS));
- }
- else
- hNGUIMenuScript = (NGUIMenuScript)GameObject.Find("UI Root (2D)").GetComponent(typeof(NGUIMenuScript));
-
- hSoundManagerCS = (SoundManagerCS)GameObject.Find("SoundManager").GetComponent(typeof(SoundManagerCS));
- hControllerScriptCS = (ControllerScriptCS)this.GetComponent(typeof(ControllerScriptCS));
- hPowerupsMainControllerCS = (PowerupsMainControllerCS)this.GetComponent(typeof(PowerupsMainControllerCS));
- hCameraControllerCS = (CameraControllerCS)GameObject.Find("Main Camera").GetComponent(typeof(CameraControllerCS));
- hEnemyControllerCS = (EnemyControllerCS)this.GetComponent(typeof(EnemyControllerCS));
- hMissionsControllerCS = (MissionsControllerCS)this.GetComponent(typeof(MissionsControllerCS));
- hGlobalAchievementControllerCS = (GlobalAchievementControllerCS)this.GetComponent(typeof(GlobalAchievementControllerCS));
- hPlayerSidesColliderScriptCS = (PlayerSidesColliderScriptCS)GameObject.Find("PlayerSidesCollider").GetComponent(typeof(PlayerSidesColliderScriptCS));
- hPlayerFrontColliderScriptCS = (PlayerFrontColliderScriptCS)GameObject.Find("PlayerFrontCollider").GetComponent(typeof(PlayerFrontColliderScriptCS));
-
- CurrentEnergy = 100;
- iPauseStatus = 0;
- iDeathStatus = 0;
- iMenuStatus = 1;
-
- bGameOver = false;
- bGamePaused = true;
- }
- public void SendEvent(int point)
- {
- switch (InGameScriptCS.Instance.Level)
- {
- case 1:
- KHD.FlurryAnalyticsTest.Instance.GameFinishLevel1(point);
- break;
- case 2:
- KHD.FlurryAnalyticsTest.Instance.GameFinishLevel2(point);
- break;
- case 3:
- KHD.FlurryAnalyticsTest.Instance.GameFinishLevel3(point);
- break;
- }
- }
- public bool isRace()
- {
- return iMenuStatus == 0;
- }
- private PlayerSidesColliderScriptCS hPlayerSidesColliderScriptCS;
- private PlayerFrontColliderScriptCS hPlayerFrontColliderScriptCS;
- public UILabel LabelCompany;
- void Update()
- {
- //if (NGUIMenuScript.Instance.CurStatePopup == NGUIMenuScript.StatesMenuForPopup.None)
- //{
- // //LabelCompany.gameObject.SetActive(false);
- //}
- //else
- //{
- // //LabelCompany.gameObject.SetActive(true);
- //}
- if (iMenuStatus == 0) //normal gameplay
- {
- /*if(startTimer)
- {
- Debug.Log("StartTimer");
- }*/
- }
- else if (iMenuStatus == 1) //display main menu and pause game
- {
- if (isCustomMenuEnabled())
- hMenuScriptCS.setMenuScriptStatus(true);
- else
- hNGUIMenuScript.NGUIMenuScriptEnabled(true);
- bGamePaused = true;
- iMenuStatus = 2;
- }
- //Pause GamePlay
- if(iPauseStatus == 1)//pause game
- {
- if (isCustomMenuEnabled())
- {
- hMenuScriptCS.setMenuScriptStatus(true);
- hMenuScriptCS.displayPauseMenu();
- }
- else
- {
- hNGUIMenuScript.NGUIMenuScriptEnabled(true);
- hNGUIMenuScript.ShowMenu(NGUIMenuScript.NGUIMenus.PauseMenu);
- }
-
- iPauseStatus = 2;
- }
- else if(iPauseStatus==3)//resume game
- {
- if (isCustomMenuEnabled())
- hMenuScriptCS.setMenuScriptStatus(false);
- else
- hNGUIMenuScript.NGUIMenuScriptEnabled(false);
-
- bGamePaused = false;
- iPauseStatus = 0;
- }
- else if (iPauseStatus == 4)
- {
- if (isCustomMenuEnabled())
- hMenuScriptCS.setMenuScriptStatus(true);
- else
- hNGUIMenuScript.NGUIMenuScriptEnabled(true);
- bGamePaused = true;
- LoginManager.Instance.GreatingMessages.Show();
- iPauseStatus = 2;
- }
- if(iDeathStatus==1)//call death menu
- {
- Debug.Log("if(iDeathStatus==1)");
- //LifeManager.Consume();
- hPowerupsMainControllerCS.deactivateAllPowerups(); //deactivate if a powerup is enabled
- //add bg
-
- iDeathStatus = 2;
- Lost = true;
- //end bg
- /*if (LifeManager.Lifes <= 0)
- {
- iDeathStatus = 2;
- }
- else
- {
- Debug.Log(iDeathStatus);
- iDeathStatus = 2;
- }*/
- }
- else if (iDeathStatus == 2)
- {
- if (isCustomMenuEnabled())//if custom menu is in use
- {
- hMenuScriptCS.setMenuScriptStatus(true);
- hMenuScriptCS.displayGameOverMenu(); //display the Game Over menu
- }
- else //if NGUI menu is in use
- {
- hNGUIMenuScript.NGUIMenuScriptEnabled(true);
- //Debug.Log();
- hNGUIMenuScript.ShowMenu(NGUIMenuScript.NGUIMenus.CodeEnter);
- hNGUIMenuScript.CurStatePopup = NGUIMenuScript.StatesMenuForPopup.GameOver;
- if (Lost)
- {
- LostLife();
- Lost = false;
- UIEnergy.Instance.Restore();
- InGameScriptCS.Instance.InvokeResurrection();
- hNGUIMenuScript.toggleHUDGroupState(false);
- //hPlayerSidesColliderScriptCS.InvokeResurrection();
- //hPlayerFrontColliderScriptCS.InvokeResurrection();
- //CurrentEnergy = 500000;
- //PlayerController.Instance.Alive();
- //hControllerScriptCS.Active();
- }
-
- var curFirst = PlayerPrefs.GetInt("EnterFirst");
- curFirst++;
- //Debug.Log(curFirst);
- PlayerPrefs.SetInt("EnterFirst", curFirst);
- PlayerPrefs.Save();
- }
- iDeathStatus = 0;
- }
- else if (iDeathStatus == 3)
- {
-
- //iDeathStatus = 0;
- }
- //Debug.Log("Debug.Log(iDeathStatus);" +iDeathStatus );
- if (bGamePaused == true)
- return;
-
- }//end of Update()
-
- public bool Lost;
- void LostLife()
- {
- Debug.Log("LostLife");
- NotificationCenter.Post(NotificationType.ShowEnterCode);
- }
- /*
- * FUNCTION: Pause the game
- * CALLED BY: ControllerScript.getClicks()
- */
- public void pauseGame()
- {
- if (!hControllerScriptCS.isMechAnimEnabled()) //if legacy animaitons are enabled
- {
- hControllerScriptCS.togglePlayerAnimation(false);//stop character animations
- }
-
-
- bGamePaused = true;//signal all scripts to pause
- iPauseStatus = 1;
-
- hSoundManagerCS.stopAllSounds();//stop all sounds
- PlayerPrefs.Save();//save changes in player prefs
- }
-
- public void Pause()
- {
- bGamePaused = true;//signal all scripts to pause
- }
- public void Resume()
- {
- bGamePaused = false;
- }
- public void LoadStartSanta()
- {
- StartCoroutine(LoadStart());
- }
- private IEnumerator LoadStart()
- {
- yield return new WaitForSeconds(0.0f);
- SceneManager.LoadSceneAsync("Santa");
- }
- public void PendingResurection()
- {
- if (!LifeLost)
- {
- LifeLost = true;
- //Debug.Log("PendingResurection");
- if (LifeManager.Lifes <= 1)
- {
- PlayerControllerLevels.Instance.AllDistance = PlayerControllerLevels.Instance.AllDistance + (int)hControllerScriptCS.getCurrentMileage();
- PlayerControllerLevels.Instance.AllGifts = PlayerControllerLevels.Instance.AllGifts + hPowerupsMainControllerCS.getCurrencyUnits();
- PlayerControllerLevels.Instance.AllPoint = PlayerControllerLevels.Instance.AllDistance + PlayerControllerLevels.Instance.AllGifts * 3 + TooltipLastterController.Instance.PontWords;
- DistanceGameOver.text = PlayerControllerLevels.Instance.AllDistance.ToString() + "m";
- GiftsGameOver.text = PlayerControllerLevels.Instance.AllGifts.ToString();
- PointsGameOver.text = PlayerControllerLevels.Instance.AllPoint.ToString();
- bGamePaused = true;//signal all scripts to pause
- //if (LoginManager.Instance.CountEnterCode <= LoginManager.Instance.MaxEnterCode)
- //if(RegisterCodeMIX.CanRegisterMore)
- //{
- // NotificationCenter.Post(NotificationType.ShowEnterCode);
- // //NGUIMenuScript.Instance.toggleHUDGroupState(true, true);
- //}
- //else
- //{
- // if (PlayerPrefs.GetInt("GetCoupon") == 1)
- // {
- // // Debug.Log("CouponGetEnterNumber.Show()");
- // LoginManager.Instance.CouponGetEnterNumber.Show();
- // NGUIMenuScript.Instance.CurStatePopup = NGUIMenuScript.StatesMenuForPopup.GameOver;
- // hNGUIMenuScript.ShowMenu(NGUIMenuScript.NGUIMenus.GameOver);
- // NGUIMenuScript.Instance.toggleHUDGroupState(false, false);
- // }
- // else
- // {
- // NGUIMenuScript.Instance.CurStatePopup = NGUIMenuScript.StatesMenuForPopup.GameOver;
- // hNGUIMenuScript.ShowMenu(NGUIMenuScript.NGUIMenus.GameOver);
- // NGUIMenuScript.Instance.toggleHUDGroupState(false, false);
- // }
- //}
- var counter = PlayerPrefs.GetInt("AdCounter");
- if (counter < GameConstants.MAX_ADD_LIFE)
- {
- LoginManager.Instance.GameOverVideoAdsLife.Show();
- }
- else
- {
- InGameScriptCS.Instance.UpdateScore();
- NGUIMenuScript.Instance.CurStatePopup = NGUIMenuScript.StatesMenuForPopup.GameOver;
- hNGUIMenuScript.ShowMenu(NGUIMenuScript.NGUIMenus.GameOver);
- PlayerPrefs.SetInt("AdCounter", 0);
- PlayerPrefs.Save();
- NGUIMenuScript.Instance.toggleHUDGroupState(false, false);
- }
- //NGUIMenuScript.Instance.ShowMenu(NGUIMenuScript.NGUIMenus.CodeEnter);
- //NGUIMenuScript.Instance.toggleHUDGroupState(true, true);
- PlayerController.Instance.Death();
- SoundManagerCS.Instance.GameOverMusic();
- LifeManager.Consume();
- hSoundManagerCS.stopAllSounds();//stop all sounds
- PlayerPrefs.Save();//save changes in player prefs
- //var top = GameObject.Find("fbTOP100").GetComponent<fbtop>();
- //var distance = PlayerControllerLevels.Instance.AllDistance;
- //var gifts = PlayerControllerLevels.Instance.AllGifts;
- //top.AddScore(distance, gifts, TooltipLastterController.Instance.PontWords);
- return;
- }
- if (!hControllerScriptCS.isMechAnimEnabled()) //if legacy animaitons are enabled
- {
- hControllerScriptCS.togglePlayerAnimation(false); //stop character animations
- }
- bGamePaused = true;//signal all scripts to pause
- NGUIMenuScript.Instance.ShowMenu(NGUIMenuScript.NGUIMenus.RaceEnd);
- NGUIMenuScript.Instance.toggleHUDGroupState(true, true);
- PlayerController.Instance.Death();
- LifeManager.Consume();
- SoundManagerCS.Instance.GameOverMusic();
- hSoundManagerCS.stopAllSounds();//stop all sounds
- PlayerPrefs.Save();//save changes in player prefs
- }
- StartCoroutine(LifeResurection());
- }
- private IEnumerator LifeResurection()
- {
- yield return new WaitForSeconds(1);
- LifeLost = false;
- }
- public bool LifeLost;
- public void InvokeResurrection()
- {
- Resurected = true;
- bGamePaused = false;
- NGUIMenuScript.Instance.ShowCandle();
- NGUIMenuScript.Instance.toggleHUDGroupState(true,true);
- //Analitics.Rescue();
- }
- /*
- * FUNCTION: start the gameplay and display all related elements
- * CALLED BY: MenuScript.MainMenuGui()
- * MenuScript.MissionsGui()
- */
- public int Level;
- public bool LevelForAchivment;
- public void launchGame()
- {
- GetComponent<ElementsGeneratorCS>().SpawnLetter = true;
- iMenuStatus = 0;
- bGamePaused = true;//tell all scripts to resume
- NGUIMenuScript.Instance.NGUIMenuScriptEnabled(true);
- if (isCustomMenuEnabled())//if custom menu is in use
- hMenuScriptCS.showHUDElements();
- else//if NGUI menu is in use
- hNGUIMenuScript.toggleHUDGroupState(true);
-
- if (PlayerPrefs.GetInt("Level") >= 4)
- {
- LaunchGameStart();
- LevelForAchivment = false;
- }
- else
- {
- if (KHD.FlurryAnalyticsTest.Instance != null)
- {
- switch (Level)
- {
- case 1:
- KHD.FlurryAnalyticsTest.Instance.GameStartLevel1();
- break;
- case 2:
- KHD.FlurryAnalyticsTest.Instance.GameStartLevel2();
- break;
- case 3:
- KHD.FlurryAnalyticsTest.Instance.GameStartLevel3();
- break;
- }
- }
-
- if (Level >= PlayerPrefs.GetInt("Level"))
- {
- LoginManager.Instance.StartLevelMessages.Show();
- LoginManager.Instance.StartLevelMessages.GetComponent<StartLevelMessage>().OnEnable();
- LevelForAchivment = true;
- }
- else
- {
- LaunchGameStart();
- LevelForAchivment = false;
- }
- }
-
- //hControllerScriptCS.launchGame();//tell the ControllerScriptCS to start game
- //hCameraControllerCS.launchGame();
- hNGUIMenuScript.toggleHUDGroupState(true,true);
- //hNGUIMenuScript.startResumeGameCounter();
- //StartCoroutine(LaunchTimerToGame());
- //tell the CameraControllerCS to start game
- //StartCoroutine(LaunchTimerToGame());
- //count how many time the game has started
- KHD.FlurryAnalyticsTest.Instance.GamePlayed();
- }
- public void UpdateScore()
- {
- PlayerControllerLevels.Instance.AllDistance = PlayerControllerLevels.Instance.AllDistance + (int)hControllerScriptCS.getCurrentMileage();
- PlayerControllerLevels.Instance.AllGifts = PlayerControllerLevels.Instance.AllGifts + hPowerupsMainControllerCS.getCurrencyUnits();
- PlayerControllerLevels.Instance.AllPoint = PlayerControllerLevels.Instance.AllDistance + PlayerControllerLevels.Instance.AllGifts * 3 + TooltipLastterController.Instance.PontWords;
- DistanceGameOver.text = PlayerControllerLevels.Instance.AllDistance.ToString() + "m";
- GiftsGameOver.text = PlayerControllerLevels.Instance.AllGifts.ToString();
- PointsGameOver.text = PlayerControllerLevels.Instance.AllPoint.ToString();
- var top = GameObject.Find("fbTOP100").GetComponent<fbtop>();
- var distance = PlayerControllerLevels.Instance.AllDistance;
- var gifts = PlayerControllerLevels.Instance.AllGifts;
- top.AddScore(distance, gifts, TooltipLastterController.Instance.PontWords);
- }
- public void LaunchGameStart()
- {
- hNGUIMenuScript.startResumeGameCounter();
- StartCoroutine(LaunchTimerToGame());
- }
- private bool startTimer;
- IEnumerator LaunchTimerToGame()
- {
- if (iPauseStatus != 4)
- {
-
- }
- startTimer = true;
- hCameraControllerCS.launchGame();
- yield return new WaitForSeconds(3f);
- if (hNGUIMenuScript.getCurrentMenu() != NGUIMenuScript.NGUIMenus.PauseMenu)
- {
- bGamePaused = false;
- startTimer = false;
- NGUIMenuScript.Instance.NGUIMenuScriptEnabled(true);
- hMissionsControllerCS.incrementMissionCount(MissionsControllerCS.MissionTypes.StartGame);
- hGlobalAchievementControllerCS.incrementAchievementCount(GlobalAchievementControllerCS.GlobalAchievementTypes.StartGame);
- hControllerScriptCS.launchGame();
- }
-
- }
- /*
- * FUNCTION: Display death menu and end game
- * CALLED BY: ControllerScript.DeathScene()
- */
- public void setupDeathMenu()
- {
- Debug.Log("setupDeathMenu");
- bGameOver = true;
- bGamePaused = true;
- iDeathStatus = 1;
- UIEnergy.Instance.Save();
- PlayerPrefs.Save();//save changes in player prefs
- }//end of Setup Death Menu
-
- /*
- * FUNCTION: Execute a function based on button press in Pause Menu
- * CALLED BY: MenuScript.PauseMenu()
- */
- public void processClicksPauseMenu(MenuScriptCS.PauseMenuEvents index)
- {
- if (index == MenuScriptCS.PauseMenuEvents.MainMenu)
- Application.LoadLevel("_loader");
- else if (index == MenuScriptCS.PauseMenuEvents.Resume)
- {
- if (isCustomMenuEnabled())
- hMenuScriptCS.showHUDElements();
- else
- hNGUIMenuScript.toggleHUDGroupState(true);
-
- iPauseStatus = 3;
-
- if (!hControllerScriptCS.isMechAnimEnabled())//if legacy animation is enabled
- hControllerScriptCS.togglePlayerAnimation(true);//pause legacy animations
- }
- }//end of process click pause menu
- public GameObject _loader;
- /*
- * FUNCTION: Execute a function based on button press in Death Menu
- * CALLED BY: MenuScript.GameOverMenu()
- */
- public void procesClicksDeathMenu(MenuScriptCS.GameOverMenuEvents index)
- {
- /*if (index == MenuScriptCS.GameOverMenuEvents.Play)
- Application.LoadLevel("Santa");
- else if (index == MenuScriptCS.GameOverMenuEvents.Back)
- Application.LoadLevel("Santa");*/
- var curFirst = PlayerPrefs.GetInt("EnterFirst");
- Debug.Log(curFirst);
- if (curFirst == 1 || curFirst == 2 || curFirst == 3)
- {
- NGUIMenuScript.Instance.ShowMenu(NGUIMenuScript.NGUIMenus.FBInviteFriends);
- NGUIMenuScript.Instance.CloseMenu(NGUIMenuScript.NGUIMenus.GameOver);
- }
- else
- {
- AdManager.Instance.PlayVideoAd();
- }
-
-
- }//end of DM_ProcessClicks
-
- /*
- * FUNCTION: Is called when a collision occurs
- * CALLED BY: PlayerFrontColliderScript.OnCollisionEnter
- * processStumble()
- */
- public void collidedWithObstacle()
- {
- decrementEnergy(100); // deduct energy after collision
- hCameraControllerCS.setCameraShakeImpulseValue(5);
- }//end of Collided With Obstacle
-
- /*
- * FUNCTION: Pause game if application closed/ switched on device
- */
- void OnApplicationPause (bool pause)
- {
- //Debug.Log("Application Paused : "+pause);
- if(Application.isEditor==false)
- {
- if(bGamePaused==false&&pause==false)
- {
- pauseGame();
- }
- }
- }//end of OnApplication function
-
- //paused state
- public bool isGamePaused() { return bGamePaused; }
-
- //score
- public int getLevelScore() { return iLevelScore; }
- public void incrementLevelScore(int iValue) { iLevelScore += iValue; }
-
- //energy
- public int getCurrentEnergy() { return CurrentEnergy; }
- public bool isEnergyZero() { return (CurrentEnergy <= 0 ? true : false); }
- public void decrementEnergy(int iValue) { CurrentEnergy -= iValue; }
- public void EnergyAdd(int iValue) { CurrentEnergy += iValue; }
- //currency
- public int getCurrencyCount() { return iCurrencyCount; }
- public void alterCurrencyCount(int iVal) { iCurrencyCount+=iVal; }//increment or decrement currency
-
- //check if the custom or NGUI is enabled
- public bool isCustomMenuEnabled() { return customMenuEnabled; }
- public bool EnterCode = false;
- }
|