LevelAction.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. }
  122. } else if (show_num == (default_show_layer_num + 1)) {
  123. if (layer_action.layer_status != 2) {
  124. layer_action.set_status(2);
  125. }
  126. } else {
  127. layer_action.set_status(0);
  128. }
  129. }
  130. }
  131. /** 根据车辆类型获取对应的载客量 */
  132. private getCarSeatCount(carType: CarTypes): number {
  133. switch (carType) {
  134. case CarTypes.Sedan: return 4; // 4座
  135. case CarTypes.Minivan: return 6; // 6座
  136. case CarTypes.Bus: return 8; // 8座
  137. default: return 4; // 默认最小
  138. }
  139. }
  140. //每个钉子检测是否被遮挡
  141. private async check_pins_block() {
  142. let layer_arr = this.get_all_layer();
  143. layer_arr.forEach(layer => {
  144. if (layer.layer_status == 1) {
  145. layer.node.children.forEach((element) => {
  146. const pins = element.getComponentsInChildren(PinComponent)!;
  147. pins.forEach(async (pin) => {
  148. const pinCom = pin.getComponent(PinComponent)!;
  149. pinCom.checkBlocking();
  150. })
  151. })
  152. }
  153. });
  154. }
  155. /** 返回顶部面板里的颜色钉子组件数组*/
  156. get_pin_color(): PinComponent[] {
  157. let arr: PinComponent[] = [];
  158. const units = this.node.getComponentsInChildren(UnitAction)!;
  159. // console.log('units.length:', units.length);
  160. units.forEach((unit) => {
  161. unit.get_pin_color(arr);
  162. })
  163. return arr
  164. }
  165. async moveToCar() {
  166. const { isEnd } = LevelManager.instance.levelModel;
  167. if (isEnd) return;
  168. const points = find("Canvas/Scene/Parkings").children
  169. let cars: Array<Node> = []
  170. let isEmpty = false
  171. for (let i = points.length; i--;) {
  172. if (points[i].name === "inuse" && points[i].children.length === 1) {
  173. cars.push(points[i].children[0])
  174. continue
  175. }
  176. if (points[i].name === "inuse" && points[i].children.length === 2) {
  177. cars.push(points[i].children[1])
  178. isEmpty = true
  179. continue
  180. }
  181. if (points[i].name === "empty") {
  182. isEmpty = true
  183. continue
  184. }
  185. }
  186. if (cars.length === 0) {
  187. // console.log("没车了!")
  188. return
  189. }
  190. let pinCom = null;
  191. let layer_arr = this.get_all_layer();
  192. layer_arr.forEach(layer => {
  193. if (layer.layer_status == 1) {
  194. layer.node.children.forEach((element) => {
  195. const pins = element.getComponentsInChildren(PinComponent)!;
  196. pins.forEach(async (pin) => {
  197. pinCom = pin.getComponent(PinComponent)!;
  198. if (pinCom.isBlocked)
  199. return
  200. let selectedCar: Node = null
  201. for (let i = cars.length; i--;) {
  202. const car = cars[i]
  203. const carComp = car.getComponent(CarCarColorsComponent)!;
  204. if (carComp && carComp.isFull)
  205. continue
  206. // 颜色相同
  207. // console.log('车颜色:', carComp.carColor, '钉子颜色:', pinCom.pin_color);
  208. if (carComp && pinCom) {
  209. if (carComp.carColor === pinCom.pin_color) {
  210. if (selectedCar === null) {
  211. selectedCar = car
  212. continue
  213. }
  214. if (selectedCar.getComponent(CarCarColorsComponent).roleNum === 0) {
  215. selectedCar = car
  216. }
  217. }
  218. }
  219. }
  220. // 匹配的车
  221. if (selectedCar !== null) {
  222. const pin_color = pinCom.pin_color
  223. CarColorsGlobalInstance.instance.carSysterm.removeColorFromSeats(pin_color)
  224. console.log(`上车钉子:${CarColorLog[pinCom.pin_color]}`);
  225. if (selectedCar.getComponent(CarCarColorsComponent).addRole(pinCom.node)) {
  226. selectedCar.setParent(find("Canvas/Scene/Levels"), true);
  227. }
  228. }
  229. })
  230. });
  231. }
  232. });
  233. if (!isEmpty) {
  234. this.checkGameOver();
  235. }
  236. }
  237. //检测游戏是否结束
  238. async checkGameOver() {
  239. const checkOver = () => {
  240. const { isEnd } = LevelManager.instance.levelModel;
  241. if (isEnd) return;
  242. const isEmpty = this.checkParkingEmpty();
  243. if (isEmpty) return;
  244. const hasMatchingNails = this.hasMatchingNails();
  245. // console.log('hasMatchingNails:', hasMatchingNails);
  246. if (hasMatchingNails) {
  247. return false;
  248. }
  249. // 游戏结束
  250. const ui = tgxUIMgr.inst.getUI(UI_BattleResult)!;
  251. if (!ui) {
  252. LevelManager.instance.levelModel.isWin = false;
  253. LevelManager.instance.levelModel.isEnd = true;
  254. tgxUIMgr.inst.showUI(UI_BattleResult);
  255. }
  256. };
  257. this.unschedule(checkOver);
  258. this.scheduleOnce(checkOver, 3);
  259. }
  260. // 检查停车场是否为空
  261. private checkParkingEmpty(): boolean {
  262. const points = find("Canvas/Scene/Parkings").children;
  263. for (let index = 0; index < points.length; index++) {
  264. const point = points[index];
  265. const status = this.getParkingStatus(index, point);
  266. if (status === ParkingStatus.Empty) {
  267. return true; // 有空位
  268. }
  269. }
  270. return false; // 停车场已满
  271. }
  272. // 获取停车位状态
  273. private getParkingStatus(index: number, point: Node): ParkingStatus {
  274. if (point.name.startsWith(ParkingStatus.Locked)) {
  275. const barricade = point.getChildByName("Barricade");
  276. if (barricade && !barricade.active) {
  277. return ParkingStatus.Empty; // 已解锁但未停车
  278. }
  279. return ParkingStatus.Locked; // 未解锁
  280. }
  281. if (index <= 3) {
  282. if (point.name === ParkingStatus.InUse && point.children.length > 0) {
  283. return ParkingStatus.InUse; // 已停车
  284. }
  285. } else {
  286. if (point.name === ParkingStatus.InUse && point.children.length > 1) {
  287. return ParkingStatus.InUse; // 已停车
  288. }
  289. }
  290. return ParkingStatus.Empty; // 空闲
  291. }
  292. private hasMatchingNails(): boolean {
  293. const points = find("Canvas/Scene/Parkings").children;
  294. const layer_arr = this.get_all_layer();
  295. for (const point of points) {
  296. if (point.name === 'inuse' && point.children.length > 0) {
  297. const car = point.children[0];
  298. const carComp = car.getComponent(CarCarColorsComponent);
  299. if (!carComp || carComp.isFull) {
  300. continue; // 车辆已满,跳过
  301. }
  302. // 查找匹配的钉子
  303. const carColor = carComp.carColor;
  304. for (const layer of layer_arr) {
  305. if (layer.layer_status !== 1) continue;
  306. for (const element of layer.node.children) {
  307. const pins = element.getComponentsInChildren(PinComponent);
  308. for (const pin of pins) {
  309. const pinCom = pin.getComponent(PinComponent);
  310. if (pinCom.isBlocked) continue; // 如果钉子已被匹配,跳过
  311. // 颜色相同
  312. if (pinCom.pin_color === carColor) {
  313. console.log('匹配成功');
  314. return true; // 找到匹配的钉子
  315. }
  316. }
  317. }
  318. }
  319. }
  320. }
  321. return false; // 没有找到匹配的钉子
  322. }
  323. protected onDestroy(): void {
  324. EventDispatcher.instance.off(GameEvent.EVENT_UPDATE_LAYER, this.hide_element);
  325. this.unscheduleAllCallbacks()
  326. }
  327. update(deltaTime: number) {
  328. // this.moveToCar();
  329. }
  330. }