GameManager.ts 10 KB

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