LevelAction.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. import { CarColors } from './CarColorsGlobalTypes';
  14. const { ccclass, property } = _decorator;
  15. @ccclass('LevelAction')
  16. export class LevelAction extends Component {
  17. start() {
  18. this.registerListener();
  19. this.init_level();
  20. this.schedule(this.moveToCar, 1);
  21. }
  22. registerListener() {
  23. EventDispatcher.instance.on(GameEvent.EVENT_UPDATE_LAYER, this.hide_element, this);
  24. }
  25. get_lvl(): number {
  26. let arr = this.node.name.split("_");
  27. // console.log("split get_lvl>>>>>:", arr);
  28. return Number(arr[1].trim());
  29. }
  30. get_hole_num(): number {
  31. let hole_num: number = 0;
  32. this.node.children.forEach(layer_node => {
  33. let num = layer_node.getComponent(UnitAction).get_hole_num();
  34. hole_num += num;
  35. });
  36. return hole_num;
  37. }
  38. public init_level() {
  39. for (let i = 0; i < this.node.children.length; i++) {
  40. const temp = this.node.children[i];
  41. if (temp.getComponent(CarCarColorsComponent) || temp.getComponent(CarBoxComponent)) {
  42. CarColorsGlobalInstance.instance.carSysterm.addCar(this.node.children[i]);
  43. }
  44. }
  45. this.node.children.forEach(unit_node => {
  46. if (unit_node.getComponent(UnitAction)) {
  47. unit_node.getComponent(UnitAction).init_layer();
  48. }
  49. });
  50. const color_pin_arr = CarColorsGlobalInstance.instance.carSysterm.carSeats;
  51. this.node.children.forEach(unit_node => {
  52. if (unit_node.getComponent(UnitAction)) {
  53. unit_node.getComponent(UnitAction)!.init_pin(color_pin_arr);
  54. }
  55. });
  56. this.init_parking();
  57. //默认隐藏一些
  58. this.scheduleOnce(() => {
  59. this.set_default_layer();
  60. }, 0.2)
  61. }
  62. init_parking() {
  63. const points = find("Canvas/Scene/Parkings").children
  64. for (let index = 0; index < points.length; index++) {
  65. const element = points[index];
  66. element.name = 'empty';
  67. if (index > 3) {
  68. element.name = `empty-lock${index}`;
  69. element.getChildByName('Barricade')!.active = true;
  70. const childrenToRemove = element.children.slice(1);
  71. childrenToRemove.forEach(child => child.destroy());
  72. } else {
  73. element.removeAllChildren();
  74. }
  75. }
  76. }
  77. private set_default_layer() {
  78. //默认都是不显示的
  79. let layer_arr = this.get_all_layer();
  80. layer_arr.forEach(layer_action => {
  81. layer_action.set_status(0);
  82. });
  83. this.hide_element();
  84. }
  85. get_all_layer(): LayerAction[] {
  86. let arr: LayerAction[] = [];
  87. //默认都是不显示的
  88. if (!this.node) return;
  89. for (let i = this.node.children.length - 1; i >= 0; i--) {
  90. if (this.node.children[i].getComponent(UnitAction)) {
  91. const unit = this.node.children[i].getComponent(UnitAction)!;
  92. unit.get_layer(arr);
  93. }
  94. }
  95. return arr;
  96. }
  97. private async hide_element() {
  98. let default_show_layer_num = 2;
  99. let show_num = 0;
  100. let layer_arr = this.get_all_layer();
  101. if (!layer_arr) return;
  102. for (let i = 0; i < layer_arr.length; i++) {
  103. let layer_action = layer_arr[i];
  104. if (layer_action.get_element_num() <= 0) {
  105. continue;
  106. }
  107. show_num++;
  108. if (show_num <= default_show_layer_num) {
  109. layer_action.set_status(1);
  110. } else if (show_num == (default_show_layer_num + 1)) {
  111. layer_action.set_status(2);
  112. } else {
  113. layer_action.set_status(0);
  114. }
  115. }
  116. this.check_pins_block();
  117. }
  118. //每个钉子检测是否被遮挡
  119. private async check_pins_block() {
  120. let layer_arr = this.get_all_layer();
  121. layer_arr.forEach(layer => {
  122. if (layer.layer_status == 1) {
  123. layer.node.children.forEach((element) => {
  124. const pins = element.getComponentsInChildren(PinComponent)!;
  125. pins.forEach(async (pin) => {
  126. const pinCom = pin.getComponent(PinComponent)!;
  127. pinCom.checkBlocking();
  128. })
  129. })
  130. }
  131. });
  132. }
  133. /** 返回顶部面板里的颜色钉子组件数组*/
  134. get_pin_color(): PinComponent[] {
  135. let arr: PinComponent[] = [];
  136. for (let i = this.node.children.length - 1; i >= 0; i--) {
  137. let unit_action = this.node.children[i].getComponent(UnitAction);
  138. unit_action?.get_pin_color(arr);
  139. }
  140. return arr
  141. }
  142. async moveToCar() {
  143. const { isEnd } = LevelManager.instance.levelModel;
  144. if (isEnd) return;
  145. const points = find("Canvas/Scene/Parkings").children
  146. let cars: Array<Node> = []
  147. let isEmpty = false
  148. for (let i = points.length; i--;) {
  149. if (points[i].name === "inuse" && points[i].children.length === 1) {
  150. cars.push(points[i].children[0])
  151. continue
  152. }
  153. if (points[i].name === "inuse" && points[i].children.length === 2) {
  154. cars.push(points[i].children[1])
  155. isEmpty = true
  156. continue
  157. }
  158. if (points[i].name === "empty") {
  159. isEmpty = true
  160. continue
  161. }
  162. }
  163. if (cars.length === 0) {
  164. // console.log("没车了!")
  165. return
  166. }
  167. let pinCom = null;
  168. let layer_arr = this.get_all_layer();
  169. layer_arr.forEach(layer => {
  170. if (layer.layer_status == 1) {
  171. layer.node.children.forEach((element) => {
  172. const pins = element.getComponentsInChildren(PinComponent)!;
  173. pins.forEach(async (pin) => {
  174. pinCom = pin.getComponent(PinComponent)!;
  175. if (pinCom.isBlocked)
  176. return
  177. let selectedCar: Node = null
  178. for (let i = cars.length; i--;) {
  179. const car = cars[i]
  180. const carComp = car.getComponent(CarCarColorsComponent)!;
  181. if (carComp && carComp.isFull)
  182. continue
  183. // 颜色相同
  184. // console.log('车颜色:', carComp.carColor, '钉子颜色:', pinCom.pin_color);
  185. if (carComp && pinCom) {
  186. if (carComp.carColor === pinCom.pin_color) {
  187. if (selectedCar === null) {
  188. selectedCar = car
  189. continue
  190. }
  191. if (selectedCar.getComponent(CarCarColorsComponent).roleNum === 0) {
  192. selectedCar = car
  193. }
  194. }
  195. }
  196. }
  197. // 匹配的车
  198. if (selectedCar !== null) {
  199. if (selectedCar.getComponent(CarCarColorsComponent).addRole(pinCom.node)) {
  200. selectedCar.setParent(find("Canvas/Scene/Levels"), true);
  201. }
  202. }
  203. })
  204. });
  205. }
  206. });
  207. if (!isEmpty) {
  208. this.checkGameOver();
  209. }
  210. }
  211. //检测游戏是否结束
  212. checkGameOver() {
  213. const checkOver = () => {
  214. const { isEnd } = LevelManager.instance.levelModel;
  215. if (isEnd) return;
  216. // 检查停车位是否都停了车
  217. const points = find("Canvas/Scene/Parkings").children;
  218. let allParkingOccupied = true;
  219. for (let i = points.length; i--;) {
  220. if (points[i].name === "empty") {
  221. allParkingOccupied = false;
  222. break;
  223. }
  224. if (points[i].name === "inuse" && points[i].children.length === 0) {
  225. allParkingOccupied = false;
  226. break;
  227. }
  228. }
  229. console.log('allParkingOccupied:', allParkingOccupied);
  230. if (!allParkingOccupied) {
  231. return; // 还有空车位,游戏未结束
  232. }
  233. // 获取所有停车位上的车子颜色
  234. const carColors = new Set<CarColors>();
  235. for (const point of points) {
  236. if (point.name === "inuse" && point.children.length == 1) {
  237. const car = point.children[0];
  238. const carComp = car.getComponent(CarCarColorsComponent);
  239. if (carComp) {
  240. carColors.add(carComp.carColor);
  241. }
  242. }
  243. if (point.name === "inuse" && point.children.length == 2) {
  244. const car = point.children[1];
  245. const carComp = car.getComponent(CarCarColorsComponent);
  246. if (carComp) {
  247. carColors.add(carComp.carColor);
  248. }
  249. }
  250. }
  251. // 检查当前层级是否有与车子颜色相同的未锁钉子
  252. const layer_arr = this.get_all_layer();
  253. let hasMatchingPins = false;
  254. for (const layer of layer_arr) {
  255. if (layer.layer_status !== 1) continue; // 只检查当前显示的层级
  256. for (const element of layer.node.children) {
  257. const pins = element.getComponentsInChildren(PinComponent);
  258. for (const pin of pins) {
  259. const pinCom = pin.getComponent(PinComponent);
  260. if (!pinCom.isBlocked && carColors.has(pinCom.pin_color)) {
  261. hasMatchingPins = true; // 有与车子颜色相同的未锁钉子
  262. break;
  263. }
  264. }
  265. if (hasMatchingPins) break;
  266. }
  267. if (hasMatchingPins) break;
  268. }
  269. console.log('hasMatchingPins:', hasMatchingPins)
  270. if (hasMatchingPins) {
  271. return; // 还有与车子颜色相同的钉子,游戏未结束
  272. }
  273. // 游戏结束
  274. const ui = tgxUIMgr.inst.getUI(UI_BattleResult)!;
  275. if (!ui) {
  276. LevelManager.instance.levelModel.isWin = false;
  277. LevelManager.instance.levelModel.isEnd = true;
  278. tgxUIMgr.inst.showUI(UI_BattleResult);
  279. }
  280. };
  281. // 取消之前的检测,重新调度
  282. this.unschedule(checkOver);
  283. this.scheduleOnce(checkOver, 2); // 2 秒后检测
  284. }
  285. protected onDestroy(): void {
  286. EventDispatcher.instance.off(GameEvent.EVENT_UPDATE_LAYER, this.hide_element);
  287. this.unscheduleAllCallbacks()
  288. }
  289. update(deltaTime: number) {
  290. // this.moveToCar();
  291. }
  292. }