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. } else {
  157. const curTotalScore = this.gameTotalScore + this.curLevelTotalScore;
  158. if (Global.IsDevelop) {
  159. AudioManage.instance.playSound(Global.Audio_achieve);
  160. this.uiManager.showGameEndTip(this.curLevel, this.curLevelScore, curTotalScore, true, false);
  161. } else {
  162. this.saveDataToAndroid(0);
  163. }
  164. }
  165. this.gameTotalScore += this.curLevelScore;
  166. }
  167. }
  168. //减分
  169. minusScore(score: number) {
  170. if (!this.isStartGame)
  171. return;
  172. this.curLevelScore -= score;
  173. if (this.curLevelScore <= 0) {
  174. this.curLevelScore = 0
  175. }
  176. this.curLevelTotalScore -= score;
  177. if (this.curLevelTotalScore <= 0) {
  178. this.curLevelTotalScore = 0
  179. }
  180. this.uiManager.RefreshScore(this.curLevelScore);
  181. }
  182. gameFail() {
  183. this.GameOver();
  184. const curTotalScore = this.gameTotalScore + this.curLevelTotalScore;
  185. if (Global.IsDevelop) {
  186. AudioManage.instance.playSound(Global.Audio_GameFail);
  187. this.uiManager.showGameEndTip(this.curLevel, this.curLevelScore, curTotalScore, false, false);
  188. } else {
  189. this.saveDataToAndroid(1);
  190. }
  191. }
  192. showTips(text: string) {
  193. this.uiManager.ShowTips(text);
  194. }
  195. //进入下一关
  196. NextLevel() {
  197. this.curLevel++;
  198. this.curLevelScore = 0;
  199. this.curLevelTime = 0;
  200. this.curLevelTotalScore = 0;
  201. this.resetCountTime();
  202. this.uiManager.RefreshLevel(this.curLevel);
  203. this.uiManager.RefreshScore(this.curLevelScore);
  204. this.unscheduleAllCallbacks();
  205. this.StartGame();
  206. }
  207. //重新开始
  208. RestartGame() {
  209. this.curLevelScore = 0;
  210. this.curLevelTime = 0
  211. this.resetCountTime();
  212. this.uiManager.RefreshScore(this.curLevelScore);
  213. AudioManage.instance.playMusic();
  214. this.StartGame();
  215. }
  216. //进入游戏场景
  217. public EnterGameScene() {
  218. this.StartGame();
  219. }
  220. //开始游戏
  221. public StartGame() {
  222. this.isStartGame = true;
  223. const levelManager = LevelManager.getInstance();
  224. if (Global.MaxLv > levelManager.levelData.length) {
  225. Global.MaxLv = levelManager.levelData.length;
  226. }
  227. //得到当前关卡配置数据
  228. this.curLevelData = levelManager.getLevelData(this.curLevel);
  229. this.uiManager.RefreshLevel(this.curLevel);
  230. AudioManage.instance.playMusic();
  231. this.startCountTime();
  232. this.uiManager.GameMain.getComponent(GameMain).initUI();
  233. }
  234. public ContinueGame() {
  235. if (this.isPaused) {
  236. this.ResumeGame();
  237. } else {
  238. this.RestartGame();
  239. }
  240. }
  241. //暂停游戏
  242. public PauseGame() {
  243. this.isPaused = true;
  244. this.isStartGame = false;
  245. AudioManage.instance.pauseMusic();
  246. // game.pause();
  247. // director.pause()
  248. }
  249. //继续游戏
  250. public ResumeGame() {
  251. this.isPaused = false;
  252. this.isStartGame = true;
  253. AudioManage.instance.playMusic();
  254. // game.resume()
  255. // director.resume()
  256. }
  257. //游戏结束
  258. public GameOver() {
  259. this.isStartGame = false;
  260. this.unscheduleAllCallbacks();
  261. this.uiManager.GameOver();
  262. AudioManage.instance.pauseMusic();
  263. }
  264. //退出游戏
  265. public QuitGame() {
  266. this.saveDataToAndroid(2);
  267. }
  268. public PlayBgMusic() {
  269. AudioManage.instance.setBgVolume(1);
  270. }
  271. //保存数据到Android
  272. saveDataToAndroid(ispass: number) {
  273. if (Global.IsDevelop) {
  274. return;
  275. }
  276. const obj = {
  277. curLevel: this.curLevel, //当前关卡
  278. curLevelScore: this.curLevelScore, //当前关卡分数
  279. curLevelTime: this.curLevelTime, //当前关卡时间
  280. isPass: ispass //0成功1失败2退出游戏3通关4超时失败5.暂停
  281. };
  282. const data = JSON.stringify(obj);
  283. let ss: any = window;
  284. if (typeof ss.AndroidInterface !== 'undefined') {
  285. ss.AndroidInterface.performAndroidMethod('saveData:' + data);
  286. } else {
  287. console.log('安卓方法未定义');
  288. }
  289. }
  290. getLevel(){
  291. return this.curLevel;
  292. }
  293. }