GameManager.ts 11 KB

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