LevelAction.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. import { _decorator, BoxCollider2D, Button, CircleCollider2D, Collider2D, Component, find, Node, NodeEventType } from 'cc';
  2. import { EventDispatcher } from '../../core_tgx/easy_ui_framework/EventDispatcher';
  3. import { CarColorsGlobalInstance } from './CarColorsGlobalInstance';
  4. import { CarCarColorsComponent } from './Components/CarCarColorsComponent';
  5. import { PinComponent } from './Components/PinComponent';
  6. import { GameEvent } from './Enum/GameEvent';
  7. import { LayerAction } from './LayerAction';
  8. import { UnitAction } from './UnitAction';
  9. import { tgxUIMgr } from '../../core_tgx/tgx';
  10. import { UI_BattleResult } from '../../scripts/UIDef';
  11. import { LevelManager } from './Manager/LevelMgr';
  12. import { CarBoxComponent } from './Components/CarBoxComponent';
  13. const { ccclass, property } = _decorator;
  14. @ccclass('LevelAction')
  15. export class LevelAction extends Component {
  16. start() {
  17. this.registerListener();
  18. this.init_level();
  19. this.schedule(this.moveToCar, 1);
  20. }
  21. registerListener() {
  22. EventDispatcher.instance.on(GameEvent.EVENT_UPDATE_LAYER, this.hide_element, this);
  23. }
  24. get_lvl(): number {
  25. let arr = this.node.name.split("_");
  26. // console.log("split get_lvl>>>>>:", arr);
  27. return Number(arr[1].trim());
  28. }
  29. get_hole_num(): number {
  30. let hole_num: number = 0;
  31. this.node.children.forEach(layer_node => {
  32. let num = layer_node.getComponent(UnitAction).get_hole_num();
  33. hole_num += num;
  34. });
  35. return hole_num;
  36. }
  37. public init_level() {
  38. for (let i = 0; i < this.node.children.length; i++) {
  39. const temp = this.node.children[i];
  40. if (temp.getComponent(CarCarColorsComponent) || temp.getComponent(CarBoxComponent)) {
  41. CarColorsGlobalInstance.instance.carSysterm.addCar(this.node.children[i]);
  42. }
  43. }
  44. this.node.children.forEach(unit_node => {
  45. if (unit_node.getComponent(UnitAction)) {
  46. unit_node.getComponent(UnitAction).init_layer();
  47. }
  48. });
  49. const color_pin_arr = CarColorsGlobalInstance.instance.carSysterm.carSeats;
  50. this.node.children.forEach(unit_node => {
  51. if (unit_node.getComponent(UnitAction)) {
  52. unit_node.getComponent(UnitAction)!.init_pin(color_pin_arr);
  53. }
  54. });
  55. this.init_parking();
  56. //默认隐藏一些
  57. this.scheduleOnce(() => {
  58. this.set_default_layer();
  59. }, 0.2)
  60. }
  61. init_parking() {
  62. const points = find("Canvas/Scene/Parkings").children
  63. for (let index = 0; index < points.length; index++) {
  64. const element = points[index];
  65. element.name = 'empty';
  66. if (index > 3) {
  67. element.name = `empty-lock${index}`;
  68. element.getChildByName('Barricade')!.active = true;
  69. const childrenToRemove = element.children.slice(1);
  70. childrenToRemove.forEach(child => child.destroy());
  71. } else {
  72. element.removeAllChildren();
  73. }
  74. }
  75. }
  76. private set_default_layer() {
  77. //默认都是不显示的
  78. let layer_arr = this.get_all_layer();
  79. layer_arr.forEach(layer_action => {
  80. layer_action.set_status(0);
  81. });
  82. this.hide_element();
  83. }
  84. get_all_layer(): LayerAction[] {
  85. let arr: LayerAction[] = [];
  86. //默认都是不显示的
  87. if (!this.node) return;
  88. for (let i = this.node.children.length - 1; i >= 0; i--) {
  89. if (this.node.children[i].getComponent(UnitAction)) {
  90. const unit = this.node.children[i].getComponent(UnitAction)!;
  91. unit.get_layer(arr);
  92. }
  93. }
  94. return arr;
  95. }
  96. private async hide_element() {
  97. let default_show_layer_num = 2;
  98. let show_num = 0;
  99. let layer_arr = this.get_all_layer();
  100. if (!layer_arr) return;
  101. for (let i = 0; i < layer_arr.length; i++) {
  102. let layer_action = layer_arr[i];
  103. if (layer_action.get_element_num() <= 0) {
  104. continue;
  105. }
  106. show_num++;
  107. if (show_num <= default_show_layer_num) {
  108. layer_action.set_status(1);
  109. } else if (show_num == (default_show_layer_num + 1)) {
  110. layer_action.set_status(2);
  111. } else {
  112. layer_action.set_status(0);
  113. }
  114. }
  115. this.check_pins_block();
  116. }
  117. //每个钉子检测是否被遮挡
  118. private async check_pins_block() {
  119. let layer_arr = this.get_all_layer();
  120. layer_arr.forEach(layer => {
  121. if (layer.layer_status == 1) {
  122. layer.node.children.forEach((element) => {
  123. const pins = element.getComponentsInChildren(PinComponent)!;
  124. pins.forEach(async (pin) => {
  125. const pinCom = pin.getComponent(PinComponent)!;
  126. pinCom.checkBlocking();
  127. })
  128. })
  129. }
  130. });
  131. }
  132. /** 返回顶部面板里的颜色钉子组件数组*/
  133. get_pin_color(): PinComponent[] {
  134. let arr: PinComponent[] = [];
  135. for (let i = this.node.children.length - 1; i >= 0; i--) {
  136. let unit_action = this.node.children[i].getComponent(UnitAction);
  137. unit_action?.get_pin_color(arr);
  138. }
  139. return arr
  140. }
  141. async moveToCar() {
  142. const { isEnd } = LevelManager.instance.levelModel;
  143. if (isEnd) return;
  144. const points = find("Canvas/Scene/Parkings").children
  145. let cars: Array<Node> = []
  146. let isEmpty = false
  147. for (let i = points.length; i--;) {
  148. if (points[i].name === "inuse" && points[i].children.length === 1) {
  149. cars.push(points[i].children[0])
  150. continue
  151. }
  152. if (points[i].name === "inuse" && points[i].children.length === 2) {
  153. cars.push(points[i].children[1])
  154. isEmpty = true
  155. continue
  156. }
  157. if (points[i].name === "empty") {
  158. isEmpty = true
  159. continue
  160. }
  161. }
  162. if (cars.length === 0) {
  163. // console.log("没车了!")
  164. return
  165. }
  166. let pinCom = null;
  167. let layer_arr = this.get_all_layer();
  168. layer_arr.forEach(layer => {
  169. if (layer.layer_status == 1) {
  170. layer.node.children.forEach((element) => {
  171. const pins = element.getComponentsInChildren(PinComponent)!;
  172. pins.forEach(async (pin) => {
  173. pinCom = pin.getComponent(PinComponent)!;
  174. if (pinCom.isBlocked)
  175. return
  176. let selectedCar: Node = null
  177. for (let i = cars.length; i--;) {
  178. const car = cars[i]
  179. const carComp = car.getComponent(CarCarColorsComponent)!;
  180. if (carComp && carComp.isFull)
  181. continue
  182. // 颜色相同
  183. // console.log('车颜色:', carComp.carColor, '钉子颜色:', pinCom.pin_color);
  184. if (carComp && pinCom) {
  185. if (carComp.carColor === pinCom.pin_color) {
  186. if (selectedCar === null) {
  187. selectedCar = car
  188. continue
  189. }
  190. if (selectedCar.getComponent(CarCarColorsComponent).roleNum === 0) {
  191. selectedCar = car
  192. }
  193. }
  194. }
  195. }
  196. // 匹配的车
  197. if (selectedCar !== null) {
  198. if (selectedCar.getComponent(CarCarColorsComponent).addRole(pinCom.node)) {
  199. selectedCar.setParent(find("Canvas/Scene/Levels"), true);
  200. }
  201. }
  202. })
  203. });
  204. }
  205. });
  206. if (!isEmpty) {
  207. this.checkGameOver();
  208. }
  209. }
  210. //检测游戏是否结束
  211. checkGameOver() {
  212. let checkOver = function () {
  213. const points = find("Canvas/Scene/Parkings").children
  214. for (let i = points.length; i--;) {
  215. if (points[i].name === "empty") {
  216. return
  217. }
  218. if (points[i].name === "inuse" && points[i].children.length === 0) {
  219. return
  220. }
  221. }
  222. const ui = tgxUIMgr.inst.getUI(UI_BattleResult)!;
  223. if (!ui) {
  224. LevelManager.instance.levelModel.isWin = false;
  225. LevelManager.instance.levelModel.isEnd = true;
  226. tgxUIMgr.inst.showUI(UI_BattleResult);
  227. }
  228. }
  229. this.unschedule(checkOver);
  230. this.scheduleOnce(checkOver, 3);
  231. }
  232. protected onDestroy(): void {
  233. EventDispatcher.instance.off(GameEvent.EVENT_UPDATE_LAYER, this.hide_element);
  234. this.unscheduleAllCallbacks()
  235. }
  236. update(deltaTime: number) {
  237. // this.moveToCar();
  238. }
  239. }