GameManager.ts 10.0 KB

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