LevelAction.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 { CarColorLog, CarColors, CarTypes } from './CarColorsGlobalTypes';
  14. import { GameUtil } from './GameUtil';
  15. const { ccclass, property } = _decorator;
  16. enum ParkingStatus {
  17. Empty = "empty", // 空闲
  18. InUse = "inuse", // 已停车
  19. Locked = "empty-lock" // 未解锁(后 3 个停车位)
  20. }
  21. @ccclass('LevelAction')
  22. export class LevelAction extends Component {
  23. start() {
  24. this.registerListener();
  25. this.init_level();
  26. this.schedule(this.moveToCar, 1.2);
  27. }
  28. registerListener() {
  29. EventDispatcher.instance.on(GameEvent.EVENT_UPDATE_LAYER, this.hide_element, this);
  30. }
  31. get_lvl(): number {
  32. let arr = this.node.name.split("_");
  33. // console.log("split get_lvl>>>>>:", arr);
  34. return Number(arr[1].trim());
  35. }
  36. get_hole_num(): number {
  37. let hole_num: number = 0;
  38. this.node.children.forEach(layer_node => {
  39. let num = layer_node.getComponent(UnitAction).get_hole_num();
  40. hole_num += num;
  41. });
  42. return hole_num;
  43. }
  44. async init_level() {
  45. for (let i = 0; i < this.node.children.length; i++) {
  46. const temp = this.node.children[i];
  47. if (temp.getComponent(CarCarColorsComponent) || temp.getComponent(CarBoxComponent)) {
  48. CarColorsGlobalInstance.instance.carSysterm.addCar(this.node.children[i]);
  49. }
  50. }
  51. this.node.children.forEach(unit_node => {
  52. if (unit_node.getComponent(UnitAction)) {
  53. unit_node.getComponent(UnitAction).init_layer();
  54. }
  55. });
  56. await GameUtil.delay(0.2);
  57. CarColorsGlobalInstance.instance.carSysterm.sortCarSeatsByDifficulty();
  58. const color_pin_arr = CarColorsGlobalInstance.instance.carSysterm.carSeats;
  59. this.node.children.forEach(unit_node => {
  60. if (unit_node.getComponent(UnitAction)) {
  61. unit_node.getComponent(UnitAction)!.init_pin(color_pin_arr);
  62. }
  63. });
  64. this.init_parking();
  65. //默认隐藏一些
  66. await GameUtil.delay(0.2);
  67. this.set_default_layer();
  68. // this.set_pin_color();
  69. }
  70. init_parking() {
  71. const points = find("Canvas/Scene/Parkings").children
  72. for (let index = 0; index < points.length; index++) {
  73. const element = points[index];
  74. element.name = 'empty';
  75. if (index > 3) {
  76. element.name = `empty-lock${index}`;
  77. element.getChildByName('Barricade')!.active = true;
  78. element.getChildByName('Barricade')!.getChildByName('Ad')!.active = true;
  79. const childrenToRemove = element.children.slice(1);
  80. childrenToRemove.forEach(child => child.destroy());
  81. } else {
  82. element.removeAllChildren();
  83. }
  84. }
  85. }
  86. private set_default_layer() {
  87. //默认都是不显示的
  88. let layer_arr = this.get_all_layer();
  89. layer_arr.forEach(layer_action => {
  90. layer_action.set_status(0);
  91. });
  92. this.hide_element();
  93. }
  94. get_all_layer(): LayerAction[] {
  95. let arr: LayerAction[] = [];
  96. //默认都是不显示的
  97. if (!this.node) return;
  98. for (let i = this.node.children.length - 1; i >= 0; i--) {
  99. if (this.node.children[i].getComponent(UnitAction)) {
  100. const unit = this.node.children[i].getComponent(UnitAction)!;
  101. unit.get_layer(arr);
  102. }
  103. }
  104. return arr;
  105. }
  106. private async hide_element() {
  107. let default_show_layer_num = 2;
  108. let show_num = 0;
  109. let layer_arr = this.get_all_layer();
  110. if (!layer_arr) return;
  111. for (let i = 0; i < layer_arr.length; i++) {
  112. let layer_action = layer_arr[i];
  113. if (layer_action.get_element_num() <= 0) {
  114. continue;
  115. }
  116. show_num++;
  117. if (show_num <= default_show_layer_num) {
  118. if (layer_action.layer_status != 1) {
  119. layer_action.set_status(1);
  120. this.check_pins_block();
  121. // this.set_pin_color();
  122. }
  123. } else if (show_num == (default_show_layer_num + 1)) {
  124. if (layer_action.layer_status != 2) {
  125. layer_action.set_status(2);
  126. }
  127. } else {
  128. layer_action.set_status(0);
  129. }
  130. }
  131. }
  132. //动态给钉子上色
  133. private async set_pin_color() {
  134. const carSysterm = CarColorsGlobalInstance.instance.carSysterm;
  135. let layer_arr = this.get_all_layer();
  136. let canOutCars = carSysterm.findCanOutCars(); // 找到所有无障碍物的车子
  137. if (canOutCars.length === 0) {
  138. console.warn("没有可以开出的车,无法分配颜色");
  139. return;
  140. }
  141. let allPins: any[] = [];
  142. // 获取所有可见钉子
  143. layer_arr.forEach(layer => {
  144. if (layer.layer_status === 1) {
  145. let pinArr: any[] = [];
  146. layer.get_pin_color(pinArr);
  147. allPins.push(...pinArr);
  148. }
  149. });
  150. console.log("总可见钉子数量:", allPins.length);
  151. if (allPins.length === 0) return;
  152. let pinIndex = 0; // 记录当前分配到哪个钉子
  153. for (let car of canOutCars) {
  154. let carComp = car.getComponent(CarCarColorsComponent);
  155. let carColor = carComp.carColor;
  156. let seatCount = this.getCarSeatCount(carComp.carType); // 获取车的载客量
  157. // 遍历当前车的座位数,依次给钉子赋值
  158. for (let i = 0; i < seatCount; i++) {
  159. if (pinIndex >= allPins.length) return; // 说明钉子已经全部分配完毕
  160. allPins[pinIndex].pin_color = carColor;
  161. pinIndex++;
  162. }
  163. }
  164. }
  165. /** 根据车辆类型获取对应的载客量 */
  166. private getCarSeatCount(carType: CarTypes): number {
  167. switch (carType) {
  168. case CarTypes.Sedan: return 4; // 4座
  169. case CarTypes.Minivan: return 6; // 6座
  170. case CarTypes.Bus: return 8; // 8座
  171. default: return 4; // 默认最小
  172. }
  173. }
  174. //每个钉子检测是否被遮挡
  175. private async check_pins_block() {
  176. let layer_arr = this.get_all_layer();
  177. layer_arr.forEach(layer => {
  178. if (layer.layer_status == 1) {
  179. layer.node.children.forEach((element) => {
  180. const pins = element.getComponentsInChildren(PinComponent)!;
  181. pins.forEach(async (pin) => {
  182. const pinCom = pin.getComponent(PinComponent)!;
  183. pinCom.checkBlocking();
  184. })
  185. })
  186. }
  187. });
  188. }
  189. /** 返回顶部面板里的颜色钉子组件数组*/
  190. get_pin_color(): PinComponent[] {
  191. let arr: PinComponent[] = [];
  192. const units = this.node.getComponentsInChildren(UnitAction)!;
  193. // console.log('units.length:', units.length);
  194. units.forEach((unit) => {
  195. unit.get_pin_color(arr);
  196. })
  197. return arr
  198. }
  199. async moveToCar() {
  200. const { isEnd } = LevelManager.instance.levelModel;
  201. if (isEnd) return;
  202. const points = find("Canvas/Scene/Parkings").children
  203. let cars: Array<Node> = []
  204. let isEmpty = false
  205. for (let i = points.length; i--;) {
  206. if (points[i].name === "inuse" && points[i].children.length === 1) {
  207. cars.push(points[i].children[0])
  208. continue
  209. }
  210. if (points[i].name === "inuse" && points[i].children.length === 2) {
  211. cars.push(points[i].children[1])
  212. isEmpty = true
  213. continue
  214. }
  215. if (points[i].name === "empty") {
  216. isEmpty = true
  217. continue
  218. }
  219. }
  220. if (cars.length === 0) {
  221. // console.log("没车了!")
  222. return
  223. }
  224. let pinCom = null;
  225. let layer_arr = this.get_all_layer();
  226. layer_arr.forEach(layer => {
  227. if (layer.layer_status == 1) {
  228. layer.node.children.forEach((element) => {
  229. const pins = element.getComponentsInChildren(PinComponent)!;
  230. pins.forEach(async (pin) => {
  231. pinCom = pin.getComponent(PinComponent)!;
  232. if (pinCom.isBlocked)
  233. return
  234. let selectedCar: Node = null
  235. for (let i = cars.length; i--;) {
  236. const car = cars[i]
  237. const carComp = car.getComponent(CarCarColorsComponent)!;
  238. if (carComp && carComp.isFull)
  239. continue
  240. // 颜色相同
  241. // console.log('车颜色:', carComp.carColor, '钉子颜色:', pinCom.pin_color);
  242. if (carComp && pinCom) {
  243. if (carComp.carColor === pinCom.pin_color) {
  244. if (selectedCar === null) {
  245. selectedCar = car
  246. continue
  247. }
  248. if (selectedCar.getComponent(CarCarColorsComponent).roleNum === 0) {
  249. selectedCar = car
  250. }
  251. }
  252. }
  253. }
  254. // 匹配的车
  255. if (selectedCar !== null) {
  256. const pin_color = pinCom.pin_color
  257. CarColorsGlobalInstance.instance.carSysterm.removeColorFromSeats(pin_color)
  258. console.log(`上车钉子:${CarColorLog[pinCom.pin_color]}`);
  259. if (selectedCar.getComponent(CarCarColorsComponent).addRole(pinCom.node)) {
  260. selectedCar.setParent(find("Canvas/Scene/Levels"), true);
  261. }
  262. }
  263. })
  264. });
  265. }
  266. });
  267. if (!isEmpty) {
  268. this.checkGameOver();
  269. }
  270. }
  271. //检测游戏是否结束
  272. async checkGameOver() {
  273. const checkOver = () => {
  274. const { isEnd } = LevelManager.instance.levelModel;
  275. if (isEnd) return;
  276. const isEmpty = this.checkParkingEmpty();
  277. if (isEmpty) return;
  278. const hasMatchingNails = this.hasMatchingNails();
  279. // console.log('hasMatchingNails:', hasMatchingNails);
  280. if (hasMatchingNails) {
  281. return false;
  282. }
  283. // 游戏结束
  284. const ui = tgxUIMgr.inst.getUI(UI_BattleResult)!;
  285. if (!ui) {
  286. LevelManager.instance.levelModel.isWin = false;
  287. LevelManager.instance.levelModel.isEnd = true;
  288. tgxUIMgr.inst.showUI(UI_BattleResult);
  289. }
  290. };
  291. this.unschedule(checkOver);
  292. this.scheduleOnce(checkOver, 3);
  293. }
  294. // 检查停车场是否为空
  295. private checkParkingEmpty(): boolean {
  296. const points = find("Canvas/Scene/Parkings").children;
  297. for (let index = 0; index < points.length; index++) {
  298. const point = points[index];
  299. const status = this.getParkingStatus(index, point);
  300. if (status === ParkingStatus.Empty) {
  301. return true; // 有空位
  302. }
  303. }
  304. return false; // 停车场已满
  305. }
  306. // 获取停车位状态
  307. private getParkingStatus(index: number, point: Node): ParkingStatus {
  308. if (point.name.startsWith(ParkingStatus.Locked)) {
  309. const barricade = point.getChildByName("Barricade");
  310. if (barricade && !barricade.active) {
  311. return ParkingStatus.Empty; // 已解锁但未停车
  312. }
  313. return ParkingStatus.Locked; // 未解锁
  314. }
  315. if (index <= 3) {
  316. if (point.name === ParkingStatus.InUse && point.children.length > 0) {
  317. return ParkingStatus.InUse; // 已停车
  318. }
  319. } else {
  320. if (point.name === ParkingStatus.InUse && point.children.length > 1) {
  321. return ParkingStatus.InUse; // 已停车
  322. }
  323. }
  324. return ParkingStatus.Empty; // 空闲
  325. }
  326. private hasMatchingNails(): boolean {
  327. const points = find("Canvas/Scene/Parkings").children;
  328. const layer_arr = this.get_all_layer();
  329. for (const point of points) {
  330. if (point.name === 'inuse' && point.children.length > 0) {
  331. const car = point.children[0];
  332. const carComp = car.getComponent(CarCarColorsComponent);
  333. if (!carComp || carComp.isFull) {
  334. continue; // 车辆已满,跳过
  335. }
  336. // 查找匹配的钉子
  337. const carColor = carComp.carColor;
  338. for (const layer of layer_arr) {
  339. if (layer.layer_status !== 1) continue;
  340. for (const element of layer.node.children) {
  341. const pins = element.getComponentsInChildren(PinComponent);
  342. for (const pin of pins) {
  343. const pinCom = pin.getComponent(PinComponent);
  344. if (pinCom.isBlocked) continue; // 如果钉子已被匹配,跳过
  345. // 颜色相同
  346. if (pinCom.pin_color === carColor) {
  347. console.log('匹配成功');
  348. return true; // 找到匹配的钉子
  349. }
  350. }
  351. }
  352. }
  353. }
  354. }
  355. return false; // 没有找到匹配的钉子
  356. }
  357. protected onDestroy(): void {
  358. EventDispatcher.instance.off(GameEvent.EVENT_UPDATE_LAYER, this.hide_element);
  359. this.unscheduleAllCallbacks()
  360. }
  361. update(deltaTime: number) {
  362. // this.moveToCar();
  363. }
  364. }