LevelAction.ts 14 KB

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