LevelAction.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. const units = this.node.getComponentsInChildren(UnitAction)!;
  137. console.log('units.length:', units.length);
  138. units.forEach((unit) => {
  139. unit.get_pin_color(arr);
  140. })
  141. return arr
  142. }
  143. async moveToCar() {
  144. const { isEnd } = LevelManager.instance.levelModel;
  145. if (isEnd) return;
  146. const points = find("Canvas/Scene/Parkings").children
  147. let cars: Array<Node> = []
  148. let isEmpty = false
  149. for (let i = points.length; i--;) {
  150. if (points[i].name === "inuse" && points[i].children.length === 1) {
  151. cars.push(points[i].children[0])
  152. continue
  153. }
  154. if (points[i].name === "inuse" && points[i].children.length === 2) {
  155. cars.push(points[i].children[1])
  156. isEmpty = true
  157. continue
  158. }
  159. if (points[i].name === "empty") {
  160. isEmpty = true
  161. continue
  162. }
  163. }
  164. if (cars.length === 0) {
  165. // console.log("没车了!")
  166. return
  167. }
  168. let pinCom = null;
  169. let layer_arr = this.get_all_layer();
  170. layer_arr.forEach(layer => {
  171. if (layer.layer_status == 1) {
  172. layer.node.children.forEach((element) => {
  173. const pins = element.getComponentsInChildren(PinComponent)!;
  174. pins.forEach(async (pin) => {
  175. pinCom = pin.getComponent(PinComponent)!;
  176. if (pinCom.isBlocked)
  177. return
  178. let selectedCar: Node = null
  179. for (let i = cars.length; i--;) {
  180. const car = cars[i]
  181. const carComp = car.getComponent(CarCarColorsComponent)!;
  182. if (carComp && carComp.isFull)
  183. continue
  184. // 颜色相同
  185. // console.log('车颜色:', carComp.carColor, '钉子颜色:', pinCom.pin_color);
  186. if (carComp && pinCom) {
  187. if (carComp.carColor === pinCom.pin_color) {
  188. if (selectedCar === null) {
  189. selectedCar = car
  190. continue
  191. }
  192. if (selectedCar.getComponent(CarCarColorsComponent).roleNum === 0) {
  193. selectedCar = car
  194. }
  195. }
  196. }
  197. }
  198. // 匹配的车
  199. if (selectedCar !== null) {
  200. if (selectedCar.getComponent(CarCarColorsComponent).addRole(pinCom.node)) {
  201. selectedCar.setParent(find("Canvas/Scene/Levels"), true);
  202. }
  203. }
  204. })
  205. });
  206. }
  207. });
  208. if (!isEmpty) {
  209. this.checkGameOver();
  210. }
  211. }
  212. //检测游戏是否结束
  213. checkGameOver() {
  214. const checkOver = () => {
  215. const { isEnd } = LevelManager.instance.levelModel;
  216. if (isEnd) return;
  217. // 检查停车位是否都停了车
  218. const points = find("Canvas/Scene/Parkings").children;
  219. let allParkingOccupied = true;
  220. for (let i = points.length; i--;) {
  221. if (points[i].name === "empty") {
  222. allParkingOccupied = false;
  223. break;
  224. }
  225. if (points[i].name === "inuse" && points[i].children.length === 0) {
  226. allParkingOccupied = false;
  227. break;
  228. }
  229. }
  230. console.log('allParkingOccupied:', allParkingOccupied);
  231. if (!allParkingOccupied) {
  232. return; // 还有空车位,游戏未结束
  233. }
  234. // 获取所有停车位上的车子颜色
  235. const carColors = new Set<CarColors>();
  236. for (const point of points) {
  237. if (point.name === "inuse" && point.children.length == 1) {
  238. const car = point.children[0];
  239. const carComp = car.getComponent(CarCarColorsComponent);
  240. if (carComp) {
  241. carColors.add(carComp.carColor);
  242. }
  243. }
  244. if (point.name === "inuse" && point.children.length == 2) {
  245. const car = point.children[1];
  246. const carComp = car.getComponent(CarCarColorsComponent);
  247. if (carComp) {
  248. carColors.add(carComp.carColor);
  249. }
  250. }
  251. }
  252. // 检查当前层级是否有与车子颜色相同的未锁钉子
  253. const layer_arr = this.get_all_layer();
  254. let hasMatchingPins = false;
  255. for (const layer of layer_arr) {
  256. if (layer.layer_status !== 1) continue; // 只检查当前显示的层级
  257. for (const element of layer.node.children) {
  258. const pins = element.getComponentsInChildren(PinComponent);
  259. for (const pin of pins) {
  260. const pinCom = pin.getComponent(PinComponent);
  261. if (!pinCom.isBlocked && carColors.has(pinCom.pin_color)) {
  262. hasMatchingPins = true; // 有与车子颜色相同的未锁钉子
  263. break;
  264. }
  265. }
  266. if (hasMatchingPins) break;
  267. }
  268. if (hasMatchingPins) break;
  269. }
  270. console.log('hasMatchingPins:', hasMatchingPins)
  271. if (hasMatchingPins) {
  272. return; // 还有与车子颜色相同的钉子,游戏未结束
  273. }
  274. // 游戏结束
  275. const ui = tgxUIMgr.inst.getUI(UI_BattleResult)!;
  276. if (!ui) {
  277. LevelManager.instance.levelModel.isWin = false;
  278. LevelManager.instance.levelModel.isEnd = true;
  279. tgxUIMgr.inst.showUI(UI_BattleResult);
  280. }
  281. };
  282. // 取消之前的检测,重新调度
  283. this.unschedule(checkOver);
  284. this.scheduleOnce(checkOver, 2); // 2 秒后检测
  285. }
  286. protected onDestroy(): void {
  287. EventDispatcher.instance.off(GameEvent.EVENT_UPDATE_LAYER, this.hide_element);
  288. this.unscheduleAllCallbacks()
  289. }
  290. update(deltaTime: number) {
  291. // this.moveToCar();
  292. }
  293. }