LevelAction.ts 13 KB

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