GameManager.ts 10 KB

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