GameManager.ts 10 KB

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