MainGame.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import { _decorator, Component, EventTouch, Prefab, Node, instantiate, AudioSource, AudioClip, math } from 'cc';
  2. import { GameManager } from './Manager/GameManager';
  3. import LevelManager from './Manager/LevelManager';
  4. import { EventCustom } from './Common/EventCustom';
  5. import { Global } from './Common/Global';
  6. import { GameConfig } from './Game/GameConfig';
  7. import { EventUtils } from './Game/EventUtils';
  8. import { PlayerItem } from './Game/PlayerItem';
  9. const { ccclass, property } = _decorator;
  10. @ccclass('MainGame')
  11. export class MainGame extends Component {
  12. @property(Prefab)
  13. playerItemPrefab: Prefab;
  14. @property(Node)
  15. middle: Node = null;
  16. @property(Node)
  17. top: Node = null;
  18. @property(Node)
  19. bottom: Node = null;
  20. @property(Node)
  21. left: Node = null;
  22. @property(Node)
  23. right: Node = null;
  24. @property(Node)
  25. gameRoot: Node = null;
  26. @property(Node)
  27. nextPolice: Node = null;
  28. @property(Node)
  29. nextThief: Node = null;
  30. @property(Node)
  31. tmiddle: Node = null;
  32. @property(Node)
  33. gameMask: Node = null;
  34. @property(AudioClip)
  35. audioClipList: AudioClip[] = [];
  36. gameSaveData: object = {
  37. gender: 0,
  38. thiefDir: 0,
  39. theMassesDir: []
  40. };
  41. audioSource: AudioSource = null;
  42. _currentLevel: number = 1;
  43. theMassesNum: number = 1;
  44. onLoad() {
  45. this.node.on(Global.EventTypeSelectPolice, (e) => {
  46. this.selectPoliceComplete(e.data);
  47. e.propagationStopped = false;
  48. });
  49. this.node.on(Global.EventTypeSelectThief, (e) => {
  50. this.selectThiefComplete(e.data);
  51. e.propagationStopped = false;
  52. });
  53. this.node.on(Global.EventGameContinue, (e) => {
  54. this.gameContinue(e.data);
  55. e.propagationStopped = false;
  56. });
  57. EventUtils.on("GameStart", this.onGameStart, this);
  58. EventUtils.on("SelectEffect", this.selectEffect, this);
  59. this.audioSource = this.node.getComponent(AudioSource);
  60. }
  61. onGameStart(levelData) {
  62. this.theMassesNum = levelData["theMassesNum"] + 1;
  63. this.generateCurrentGameData(this.theMassesNum);
  64. }
  65. start() {
  66. }
  67. onDestroy() {
  68. EventUtils.off("GameStart", this.onGameStart, this);
  69. EventUtils.off("SelectEffect", this.selectEffect, this);
  70. }
  71. generateCurrentGameData(theMassesNum) {
  72. this.showTips("请记住警察和小偷和平民出现的方向")
  73. let gender: number = Math.random() > 0.5 ? 5 : 6;
  74. let randomArray = this.randomData(theMassesNum);
  75. let targetID: number = randomArray[math.randomRangeInt(0, randomArray.length)];
  76. GameConfig.gameSaveData = {
  77. gender: gender,
  78. thiefDir: randomArray[0],
  79. theMassesDir: randomArray.filter(item => { return item != randomArray[0] }),
  80. targetDir: targetID
  81. }
  82. this.generaPlayer();
  83. setTimeout(() => {
  84. this.gameRoot.active = false;
  85. this.nextThief.active = false;
  86. this.gameMask.active = false;
  87. this.nextPolice.active = true;
  88. this.showTips("请选择刚才出现的警察");
  89. }, 2000 + (5000 - (GameManager.getInstance().curLevel * 200 + Global.DifficultyRate * 100)));
  90. }
  91. generaPlayer() {
  92. this.clearNode();
  93. let middleNode = instantiate(this.playerItemPrefab);
  94. this.middle.addChild(middleNode);
  95. middleNode.emit("init", GameConfig.PlayerType.Police, GameConfig.gameSaveData["gender"]);
  96. let tmiddleNode = instantiate(this.playerItemPrefab);
  97. this.tmiddle.addChild(tmiddleNode);
  98. tmiddleNode.getComponent(PlayerItem).init(GameConfig.PlayerType.Police, GameConfig.gameSaveData["gender"]);
  99. // tmiddleNode.emit("init", GameConfig.PlayerType.Police, GameConfig.gameSaveData["gender"]);
  100. let thiefNode = instantiate(this.playerItemPrefab);
  101. let thiefParent = this.getTargetNode(GameConfig.gameSaveData["thiefDir"]);
  102. thiefParent.addChild(thiefNode);
  103. thiefNode.emit("init", GameConfig.PlayerType.Thief);
  104. let allTheMassesData = GameConfig.gameSaveData["theMassesDir"];
  105. for (let i = 0; i < allTheMassesData.length; ++i) {
  106. let item = instantiate(this.playerItemPrefab);
  107. let itemParent = this.getTargetNode(allTheMassesData[i]);
  108. if (itemParent) {
  109. itemParent.addChild(item);
  110. item.emit("init", GameConfig.PlayerType.TheMasses);
  111. }
  112. }
  113. }
  114. clearNode() {
  115. this.middle.destroyAllChildren();
  116. this.tmiddle.destroyAllChildren();
  117. this.top.destroyAllChildren();
  118. this.bottom.destroyAllChildren();
  119. this.left.destroyAllChildren();
  120. this.right.destroyAllChildren();
  121. this.gameRoot.active = true;
  122. this.nextThief.active = false;
  123. this.gameMask.active = false;
  124. this.nextPolice.active = false;
  125. }
  126. getTargetNode(index) {
  127. let targetNode: Node = null;
  128. switch (index) {
  129. case 1:
  130. targetNode = this.top;
  131. break;
  132. case 2:
  133. targetNode = this.right;
  134. break;
  135. case 3:
  136. targetNode = this.bottom;
  137. break;
  138. case 4:
  139. targetNode = this.left;
  140. break;
  141. }
  142. return targetNode;
  143. }
  144. randomData(num) {
  145. let arr = [];
  146. while (arr.length < num) {
  147. let randomNumber = Math.floor(Math.random() * 4) + 1;
  148. if (arr.indexOf(randomNumber) == -1) {
  149. arr.push(randomNumber);
  150. }
  151. }
  152. return arr;
  153. }
  154. onTouchStart(event: EventTouch) {
  155. }
  156. getTheMassesNumFromServer() {
  157. let currentLevelData = LevelManager.getInstance().getLevelData(this._currentLevel);
  158. return currentLevelData.theMassesNum || 1;
  159. }
  160. selectEffect() {
  161. this.audioSource.playOneShot(this.audioClipList[0], 1);
  162. }
  163. selectPoliceComplete(data) {
  164. if (data == GameConfig.selectType.Success) {
  165. this.gameRoot.active = false;
  166. this.nextThief.active = true;
  167. this.nextPolice.active = false;
  168. this.gameMask.active = false;
  169. let targetParent = this.getTargetNode(GameConfig.gameSaveData["targetDir"]);
  170. if (targetParent) {
  171. let playerItemSc = targetParent.getComponentInChildren(PlayerItem);
  172. if (playerItemSc.curPlayerType == GameConfig.PlayerType.Thief) {
  173. this.showTips("请选择小偷出现的方向");
  174. } else {
  175. switch (playerItemSc.massesID) {
  176. case 0:
  177. this.showTips("请选择小摊车出现的方向");
  178. break;
  179. case 1:
  180. this.showTips("请选择老爷爷和老奶奶出现的方向");
  181. break;
  182. case 2:
  183. this.showTips("请选择悲伤老爷爷出现的方向");
  184. break;
  185. case 3:
  186. this.showTips("请选择卖煎饼师傅出现的方向");
  187. break;
  188. case 4:
  189. this.showTips("请选择微笑老爷爷出现的方向");
  190. break;
  191. }
  192. }
  193. }
  194. } else {
  195. this.gameRoot.active = false;
  196. this.nextThief.active = false;
  197. this.nextPolice.active = false;
  198. this.gameMask.active = true;
  199. this.gameMask.emit("init", GameConfig.selectType.Failed);
  200. }
  201. }
  202. selectThiefComplete(data) {
  203. if (data == GameConfig.selectType.Success) {
  204. this.gameRoot.active = false;
  205. this.nextThief.active = false;
  206. this.nextPolice.active = false;
  207. this.gameMask.active = true;
  208. this.gameMask.emit("init", GameConfig.selectType.Success);
  209. GameManager.getInstance().addScore(20);
  210. setTimeout(() => { this.audioSource.playOneShot(this.audioClipList[1], 1); }, 200);
  211. } else {
  212. this.gameRoot.active = false;
  213. this.nextThief.active = false;
  214. this.nextPolice.active = false;
  215. this.gameMask.active = true;
  216. this.gameMask.emit("init", GameConfig.selectType.Failed);
  217. }
  218. }
  219. gameContinue(data) {
  220. this.gameRoot.active = true;
  221. this.nextThief.active = false;
  222. this.nextPolice.active = false;
  223. this.gameMask.active = false;
  224. if (GameManager.getInstance().isStartGame) {
  225. this.generateCurrentGameData(this.theMassesNum);
  226. }
  227. }
  228. showTips(str: string) {
  229. let tip = new EventCustom(Global.EventType_ShowTips, true, str);
  230. this.node.dispatchEvent(tip);
  231. }
  232. update(deltaTime: number) {
  233. }
  234. }