GameManager.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. import { _decorator, Component, Label, Node, EventTarget, game, director } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. import LevelData from '../Data/LevelData';
  4. import LevelManager from './LevelManager';
  5. import { Global } from '../Common/Global';
  6. import { AudioManage } from './AudioManage';
  7. import { GamePanel } from '../UiPanel/GamePanel';
  8. import GameEventManager from '../Common/Base/GameEventManager';
  9. import { GameEvent } from '../Common/Base/GameEventConfig';
  10. import { GameMain } from '../Game/GameMain';
  11. @ccclass('GameManager')
  12. export class GameManager extends Component {
  13. private static _instance: GameManager = null;
  14. static getInstance(): GameManager {
  15. return GameManager._instance;
  16. }
  17. @property(GamePanel)
  18. uiManager: GamePanel = null;
  19. @property({ displayName: "是否为开发版本" })
  20. public IsDevelop = false;
  21. curLevel: number = 1; //当前关卡
  22. curLevelScore: number = 0; //当前关卡分数
  23. curLevelTime: number = 0; //当前关卡所玩时间
  24. curLevelData: LevelData = null;
  25. curLevelTotalScore: number = 0;
  26. gameTotalTime: number = 0; //游戏总的时间
  27. gameTotalScore: number = 0; //游戏总分数
  28. playClass: string; //关卡分类 0代表可以继续下一关1代表只有1关
  29. timer: number = 120; // 2分钟,单位为秒
  30. //游戏状态
  31. isPaused: boolean = false;
  32. isStartGame: boolean = false;
  33. //消息收发
  34. eventTarget = new EventTarget();
  35. gameMains:GameMain = null;
  36. onLoad() {
  37. if (GameManager._instance) {
  38. this.node.destroy();
  39. return;
  40. }
  41. Global.IsDevelop = this.IsDevelop;
  42. const isDevelopStr = this.getParameterByName('isDevelop') || true;
  43. if (isDevelopStr === "true") {
  44. Global.IsDevelop = true;
  45. } else if (isDevelopStr === "false") {
  46. Global.IsDevelop = false;
  47. }
  48. GameManager._instance = this;
  49. if (!Global.IsDevelop) {
  50. this.playClass = this.getParameterByName('playClass');
  51. Global.StartLv = parseInt(this.getParameterByName('startLv')) || 1;
  52. Global.MaxLv = parseInt(this.getParameterByName('maxLv')) || 10;
  53. Global.GameSetTime = parseInt(this.getParameterByName('gameSetTime')) || 120;
  54. Global.DifficultyRate = parseInt(this.getParameterByName('difficultyRate'));
  55. // this.gameTotalTime = parseInt(this.getParameterByName('totalTime'));
  56. this.gameTotalScore = parseInt(this.getParameterByName('totalScore'));
  57. }
  58. let window1: any = window;
  59. window1.CallNextLevel = () => {
  60. this.NextLevel();
  61. }
  62. window1.CallRestartGame = () => {
  63. this.RestartGame();
  64. }
  65. window1.CallPauseGame = () => {
  66. this.PauseGame();
  67. }
  68. window1.CallContinueGame = () => {
  69. this.ContinueGame();
  70. }
  71. window1.CallQuitGame = () => {
  72. this.QuitGame();
  73. }
  74. window1.CallStartGame = () => {
  75. this.StartGame();
  76. }
  77. window1.CallPlayBgMusic = () => {
  78. this.PlayBgMusic();
  79. }
  80. }
  81. start() {
  82. this.initialize();
  83. if (Global.IsDevelop) {
  84. this.StartGame();
  85. } else {
  86. const levelManager = LevelManager.getInstance();
  87. }
  88. }
  89. update(deltaTime: number) {
  90. }
  91. //初始化
  92. initialize() {
  93. this.timer = Global.GameSetTime;
  94. this.curLevel = Global.StartLv;
  95. }
  96. //得到url参数值
  97. getParameterByName(name) {
  98. let url = window.location.href;
  99. name = name.replace(/[\[\]]/g, '\\$&');
  100. let regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
  101. results = regex.exec(url);
  102. if (!results) return null;
  103. if (!results[2]) return '';
  104. return decodeURIComponent(results[2].replace(/\+/g, ' '));
  105. }
  106. //开始计时
  107. startCountTime() {
  108. this.schedule(this.updateTimer, 1);
  109. }
  110. updateTimer() {
  111. if (this.isStartGame) {
  112. GameEventManager.getInstance().dispathcGameEvent(GameEvent.UpdateTimer);
  113. this.timer--;
  114. this.uiManager.RefreshTime(this.timer);
  115. this.curLevelTime++;
  116. this.gameTotalTime++;
  117. if (this.timer <= 0) {
  118. this.unschedule(this.updateTimer);
  119. this.GameOver();
  120. //保存数据
  121. const curTotalScore = this.gameTotalScore + this.curLevelTotalScore;
  122. if (Global.IsDevelop) {
  123. AudioManage.instance.playSound(Global.Audio_TimeOutFail);
  124. this.uiManager.showGameEndTip(this.curLevel, this.curLevelScore, curTotalScore, false, true);
  125. } else {
  126. if (this.playClass === 'A') {
  127. if (this.curLevelScore > this.curLevelData.targetScore) {
  128. this.saveDataToAndroid(0);
  129. } else {
  130. this.saveDataToAndroid(1);
  131. }
  132. } else {
  133. this.saveDataToAndroid(4);
  134. }
  135. }
  136. // 倒计时结束游戏失败
  137. GameEventManager.getInstance().dispathcGameEvent(GameEvent.TimeOver);
  138. }
  139. }
  140. }
  141. //重置时间
  142. resetCountTime() {
  143. this.timer = Global.GameSetTime;
  144. }
  145. //加分
  146. addScore(score: number) {
  147. if (!this.isStartGame)
  148. return;
  149. this.curLevelScore += score;
  150. this.curLevelTotalScore += score;
  151. this.uiManager.RefreshScore(this.curLevelScore);
  152. if (this.curLevelScore >= this.curLevelData.targetScore) {
  153. //保存数据
  154. this.GameOver();
  155. if (this.curLevel === Global.MaxLv) {
  156. if(Global.IsDevelop){
  157. const curTotalScore = this.gameTotalScore + this.curLevelTotalScore;
  158. this.uiManager.showPlayAendTip(curTotalScore)
  159. }else{
  160. this.saveDataToAndroid(3);
  161. }
  162. } else {
  163. const curTotalScore = this.gameTotalScore + this.curLevelTotalScore;
  164. if (Global.IsDevelop) {
  165. AudioManage.instance.playSound(Global.Audio_Success);
  166. this.uiManager.showGameEndTip(this.curLevel, this.curLevelScore, curTotalScore, true, false);
  167. } else {
  168. this.saveDataToAndroid(0);
  169. }
  170. }
  171. this.gameTotalScore += this.curLevelScore;
  172. }
  173. }
  174. //减分
  175. minusScore(score: number) {
  176. if (!this.isStartGame)
  177. return;
  178. this.curLevelScore -= score;
  179. if (this.curLevelScore <= 0) {
  180. this.curLevelScore = 0
  181. GameEventManager.getInstance().dispathcGameEvent(GameEvent.ScoreFail);
  182. }
  183. this.curLevelTotalScore -= score;
  184. if (this.curLevelTotalScore <= 0) {
  185. this.curLevelTotalScore = 0
  186. }
  187. this.uiManager.RefreshScore(this.curLevelScore);
  188. // if (this.curLevelScore <= 0) {
  189. // this.GameOver();
  190. // const curTotalScore = this.gameTotalScore + this.curLevelTotalScore;
  191. // if (Global.IsDevelop) {
  192. // this.uiManager.showGameEndTip(this.curLevel, this.curLevelScore, curTotalScore, false,false);
  193. // } else {
  194. // this.saveDataToAndroid(1);
  195. // }
  196. // }
  197. }
  198. gameFail() {
  199. this.GameOver();
  200. const curTotalScore = this.gameTotalScore + this.curLevelTotalScore;
  201. if (Global.IsDevelop) {
  202. AudioManage.instance.playSound(Global.Audio_GameFail);
  203. this.uiManager.showGameEndTip(this.curLevel, this.curLevelScore, curTotalScore, false, false);
  204. } else {
  205. this.saveDataToAndroid(1);
  206. }
  207. }
  208. showTips(text: string) {
  209. this.uiManager.ShowTips(text);
  210. }
  211. //进入下一关
  212. NextLevel() {
  213. this.curLevel++;
  214. this.curLevelScore = 0;
  215. this.curLevelTime = 0;
  216. this.curLevelTotalScore = 0;
  217. this.resetCountTime();
  218. this.uiManager.RefreshLevel(this.curLevel);
  219. this.uiManager.RefreshScore(this.curLevelScore);
  220. this.unscheduleAllCallbacks();
  221. GameEventManager.getInstance().dispathcGameEvent(GameEvent.NextLevel,this.curLevel);
  222. this.StartGame();
  223. this.gameMains.initUI()
  224. }
  225. //重新开始
  226. RestartGame() {
  227. this.curLevelScore = 0;
  228. this.curLevelTime = 0
  229. this.resetCountTime();
  230. this.uiManager.RefreshScore(this.curLevelScore);
  231. AudioManage.instance.playMusic();
  232. this.StartGame();
  233. this.gameMains.initUI()
  234. }
  235. //进入游戏场景
  236. public EnterGameScene() {
  237. this.StartGame();
  238. }
  239. //开始游戏
  240. public StartGame() {
  241. this.isStartGame = true;
  242. const levelManager = LevelManager.getInstance();
  243. if (Global.MaxLv > levelManager.levelData.length) {
  244. Global.MaxLv = levelManager.levelData.length;
  245. }
  246. //得到当前关卡配置数据
  247. this.curLevelData = levelManager.getLevelData(this.curLevel);
  248. this.uiManager.RefreshLevel(this.curLevel);
  249. AudioManage.instance.playMusic();
  250. this.startCountTime();
  251. }
  252. public ContinueGame() {
  253. if (this.isPaused) {
  254. this.ResumeGame();
  255. } else {
  256. this.RestartGame();
  257. }
  258. }
  259. //暂停游戏
  260. public PauseGame() {
  261. this.isPaused = true;
  262. this.isStartGame = false;
  263. AudioManage.instance.pauseMusic();
  264. //暂停后处理游戏内暂停
  265. GameEventManager.getInstance().dispathcGameEvent(GameEvent.PauseGame);
  266. // game.pause();
  267. // director.pause()
  268. }
  269. //继续游戏
  270. public ResumeGame() {
  271. this.isPaused = false;
  272. this.isStartGame = true;
  273. AudioManage.instance.playMusic();
  274. GameEventManager.getInstance().dispathcGameEvent(GameEvent.ResumeGame);
  275. // game.resume()
  276. // director.resume()
  277. }
  278. //游戏结束
  279. public GameOver() {
  280. this.isStartGame = false;
  281. this.unscheduleAllCallbacks();
  282. this.uiManager.GameOver();
  283. AudioManage.instance.pauseMusic();
  284. }
  285. //退出游戏
  286. public QuitGame() {
  287. this.saveDataToAndroid(2);
  288. }
  289. public PlayBgMusic() {
  290. AudioManage.instance.setBgVolume(1);
  291. }
  292. //保存数据到Android
  293. saveDataToAndroid(ispass: number) {
  294. if (Global.IsDevelop) {
  295. return;
  296. }
  297. const obj = {
  298. curLevel: this.curLevel, //当前关卡
  299. curLevelScore: this.curLevelScore, //当前关卡分数
  300. curLevelTime: this.curLevelTime, //当前关卡时间
  301. isPass: ispass //0成功1失败2退出游戏3通关4超时失败5.暂停
  302. };
  303. const data = JSON.stringify(obj);
  304. let ss: any = window;
  305. if (typeof ss.AndroidInterface !== 'undefined') {
  306. ss.AndroidInterface.performAndroidMethod('saveData:' + data);
  307. } else {
  308. console.log('安卓方法未定义');
  309. }
  310. }
  311. getLevel(){
  312. return this.curLevel;
  313. }
  314. }