LevelAction.ts 12 KB

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