跑酷游戏_1
usingUnityEngine;usingSystem.Collections;/**CoroutineDataisaquickclassusedtosaveoffanytiminiginformationneededwhenthegameispaused.*/publicclassCoroutineData{publicfloatstartTime;publicfloatduration;publicCoroutineData(){startTime=0;duration=0;}publicvoidcalcuateNewDuration(){duration-=Time.time-startTime;}}/**Thegamemanagerisasingletonwhichmanagesthegamestate.Itcoordinateswithalloftheotherclassestotellthem*whentostartdifferentgamestatessuchaspausingthegameorendingthegame.*/publicenumGameOverType{Wall,JumpObstacle,DuckObstacle,Pit,Quit};publicclassGameManager:MonoBehaviour{staticpublicGameManagerinstance;publicdelegatevoidPlayerSpawnHandler();publiceventPlayerSpawnHandleronPlayerSpawn;publicdelegatevoidPauseHandler(boolpaused);publiceventPauseHandleronPauseGame;publicboolgodMode;publicboolenableAllPowerUps;publicboolshowTutorial;publicboolrunInBackground;privateintactiveCharacter;privateGameObjectcharacter;privateboolgamePaused;privateboolgameActive;privateInfiniteObjectGeneratorinfiniteObjectGenerator;privatePlayerControllerplayerController;privateGUIManagerguiManager;privateDataManagerdataManager;privateAudioManageraudioManager;privatePowerUpManagerpowerUpManager;privateMissionManagermissionManager;privateInputControllerinputController;privateChaseControllerchaseController;privateCameraControllercameraController;privateCoinGUICollectioncoinGUICollection;publicvoidAwake(){instance=this;}publicvoidStart(){infiniteObjectGenerator=InfiniteObjectGenerator.instance;guiManager=GUIManager.instance;dataManager=DataManager.instance;audioManager=AudioManager.instance;powerUpManager=PowerUpManager.instance;missionManager=MissionManager.instance;inputController=InputController.instance;cameraController=CameraController.instance;coinGUICollection=CoinGUICollection.instance;Application.runInBackground=runInBackground;activeCharacter=-1;spawnCharacter();spawnChaseObject();}privatevoidspawnCharacter(){if(activeCharacter==dataManager.getSelectedCharacter()){return;}if(character!=null){Destroy(character);}activeCharacter=dataManager.getSelectedCharacter();character=GameObject.Instantiate(dataManager.getCharacterPrefab(activeCharacter))asGameObject;playerController=PlayerController.instance;playerController.init();if(onPlayerSpawn!=null){onPlayerSpawn();}}privatevoidspawnChaseObject(){GameObjectprefab;if((prefab=dataManager.getChaseObjectPrefab())!=null){chaseController=(GameObject.Instantiate(prefab)asGameObject).GetComponent<ChaseController>();}}publicvoidstartGame(boolfromRestart){gameActive=true;inputController.startGame();guiManager.showGUI(GUIState.InGame);audioManager.playBackgroundMusic(true);cameraController.startGame(fromRestart);infiniteObjectGenerator.startGame();playerController.startGame();if(chaseController!=null)chaseController.startGame();}publicboolisGameActive(){returngameActive;}publicvoidtoggleTutorial(){showTutorial=!showTutorial;infiniteObjectGenerator.reset();if(showTutorial){infiniteObjectGenerator.showStartupObjects(true);}else{//showthestartupobjectsifthereareanyif(!infiniteObjectGenerator.showStartupObjects(false))infiniteObjectGenerator.spawnObjectRun(false);}infiniteObjectGenerator.readyFromReset();}publicvoidobstacleCollision(ObstacleObjectobstacle,Vector3position){if(!powerUpManager.isPowerUpActive(PowerUpTypes.Invincibility)&&!powerUpManager.isPowerUpActive(PowerUpTypes.SpeedIncrease)&&!godMode&&gameActive){playerController.obstacleCollision(obstacle.getTransform(),position);dataManager.obstacleCollision();if(dataManager.getCollisionCount()==playerController.maxCollisions){gameOver(obstacle.isJump?GameOverType.JumpObstacle:GameOverType.DuckObstacle,true);}else{//thechaseobjectwillendthegameif(playerController.maxCollisions==0&&chaseController!=null){if(chaseController.isVisible()){gameOver(obstacle.isJump?GameOverType.JumpObstacle:GameOverType.DuckObstacle,true);}else{chaseController.approach();audioManager.playSoundEffect(SoundEffects.ObstacleCollisionSoundEffect);}}else{//havethechaseobjectapproachthecharacterwhenthecollisioncountgetscloseif(chaseController!=null&&dataManager.getCollisionCount()==playerController.maxCollisions-1){chaseController.approach();}audioManager.playSoundEffect(SoundEffects.ObstacleCollisionSoundEffect);}}}}//initialcollectionistruewhentheplayerfirstcollectsacoin.ItwillbefalsewhenthecoinisdoneanimatingtothecoinelementontheGUI//returnsthevalueofthecoinwiththedoublecoinpowerupconsideredpublicintcoinCollected(){intcoinValue=(powerUpManager.isPowerUpActive(PowerUpTypes.DoubleCoin)?2:1);audioManager.playSoundEffect(SoundEffects.CoinSoundEffect);returncoinValue;}publicvoidcoinCollected(intcoinValue){dataManager.addToCoins(coinValue);}publicvoidactivatePowerUp(PowerUpTypespowerUpType,boolactivate){if(activate){//deactivatethecurrentpowerup(ifapowerupisactive)andactivatethenewonepowerUpManager.deactivatePowerUp();powerUpManager.activatePowerUp(powerUpType);audioManager.playSoundEffect(SoundEffects.PowerUpSoundEffect);}playerController.activatePowerUp(powerUpType,activate);guiManager.activatePowerUp(powerUpType,activate,dataManager.getPowerUpLength(powerUpType));}publicvoidgameOver(GameOverTypegameOverType,boolwaitForFrame){if(!gameActive&&waitForFrame)return;gameActive=false;if(waitForFrame){//mecanimdoesn'ttriggertheeventifwewaituntiltheframeisoverplayerController.gameOver(gameOverType);StartCoroutine(waitForFrameGameOver(gameOverType));}else{inputController.gameOver();//MissionManager'sgameOvermustbecalledbeforetheDataManager'sgameOversotheDataManagercangrabthe//scoremultiplierfromtheMissionManagertodeterminethefinalscoremissionManager.gameOver();coinGUICollection.gameOver();dataManager.gameOver();if(playerController.enabled)playerController.gameOver(gameOverType);if(chaseController!=null)chaseController.gameOver(gameOverType);audioManager.playBackgroundMusic(false);if(gameOverType!=GameOverType.Quit)audioManager.playSoundEffect(SoundEffects.GameOverSoundEffect);guiManager.gameOver();cameraController.gameOver(gameOverType);}}//GameovermaybecalledfromatriggersowaitforthephysicslooptoendprivateIEnumeratorwaitForFrameGameOver(GameOverTypegameOverType){yieldreturnnewWaitForEndOfFrame();gameOver(gameOverType,false);//WaitasecondfortheendinganimationstoplayyieldreturnnewWaitForSeconds(1.0f);guiManager.showGUI(GUIState.EndGame);}publicvoidrestartGame(boolstart){if(gamePaused){if(onPauseGame!=null)onPauseGame(false);gameOver(GameOverType.Quit,false);}dataManager.reset();infiniteObjectGenerator.reset();powerUpManager.reset();playerController.reset();cameraController.reset();if(chaseController!=null)chaseController.reset();if(showTutorial){infiniteObjectGenerator.showStartupObjects(true);}else{//showthestartupobjectsifthereareanyif(!infiniteObjectGenerator.showStartupObjects(false))infiniteObjectGenerator.spawnObjectRun(false);}infiniteObjectGenerator.readyFromReset();if(start)startGame(true);}publicvoidbackToMainMenu(boolrestart){if(gamePaused){if(onPauseGame!=null)onPauseGame(false);gameOver(GameOverType.Quit,false);}if(restart)restartGame(false);guiManager.showGUI(GUIState.MainMenu);}//activate/deactivatethecharacterwhengoingintothestore.TheGUIManagerwillmanagethepreviewpublicvoidshowStore(boolshow){//ensurethecorrectcharacterisusedif(!show){spawnCharacter();}#ifUNITY_3_5character.SetActiveRecursively(!show);#elseInfiniteRunnerStarterPackUtility.ActiveRecursively(character.transform,!show);#endif}publicvoidpauseGame(boolpause){guiManager.showGUI(pause?GUIState.Pause:GUIState.InGame);audioManager.playBackgroundMusic(!pause);if(onPauseGame!=null)onPauseGame(pause);inputController.enabled=!pause;gamePaused=pause;}publicvoidupgradePowerUp(PowerUpTypespowerUpType){//Can'tupgradeiftheplayercan'taffordthepowerupintcost=dataManager.getPowerUpCost(powerUpType);if(dataManager.getTotalCoins()<cost){return;}dataManager.upgradePowerUp(powerUpType);dataManager.adjustTotalCoins(-cost);}publicvoidselectCharacter(intcharacter){intcharacterCost=dataManager.getCharacterCost(character);if(characterCost==-1){//canonlyselectacharacterifithasbeenpurchasedif(dataManager.getSelectedCharacter()!=character){dataManager.setSelectedCharacter(character);}}}publicvoidpurchaseCharacter(intcharacter){intcost=dataManager.getCharacterCost(character);if(dataManager.getTotalCoins()<cost){return;}dataManager.purchaseCharacter(character);dataManager.setSelectedCharacter(character);dataManager.adjustTotalCoins(-cost);}publicvoidOnApplicationPause(boolpause){if(gamePaused)return;if(onPauseGame!=null)onPauseGame(pause);}}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。