GameManager.ts 10 KB

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