GameManager.ts 10 KB

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