123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- import { _decorator, BoxCollider2D, Button, CircleCollider2D, Collider2D, Component, find, Node, NodeEventType } from 'cc';
- import { EventDispatcher } from '../../core_tgx/easy_ui_framework/EventDispatcher';
- import { CarColorsGlobalInstance } from './CarColorsGlobalInstance';
- import { CarCarColorsComponent } from './Components/CarCarColorsComponent';
- import { PinComponent } from './Components/PinComponent';
- import { GameEvent } from './Enum/GameEvent';
- import { LayerAction } from './LayerAction';
- import { UnitAction } from './UnitAction';
- import { tgxUIMgr } from '../../core_tgx/tgx';
- import { UI_BattleResult } from '../../scripts/UIDef';
- import { LevelManager } from './Manager/LevelMgr';
- import { CarBoxComponent } from './Components/CarBoxComponent';
- import { CarColorLog, CarColors, CarTypes } from './CarColorsGlobalTypes';
- import { GameUtil } from './GameUtil';
- import { GtagMgr, GtagType } from '../../core_tgx/base/GtagMgr';
- import { GlobalConfig } from '../../start/Config/GlobalConfig';
- import { AdvertMgr } from '../../core_tgx/base/ad/AdvertMgr';
- const { ccclass, property } = _decorator;
- enum ParkingStatus {
- Empty = "empty", // 空闲
- InUse = "inuse", // 已停车
- Locked = "empty-lock" // 未解锁(后 3 个停车位)
- }
- @ccclass('LevelAction')
- export class LevelAction extends Component {
- start() {
- this.registerListener();
- this.init_level();
- this.schedule(this.moveToCar, 1.2);
- this.reportInformation();
- }
- //上报信息
- reportInformation() {
- const { level } = LevelManager.instance.levelModel;
- GtagMgr.inst.doGameDot(GtagType.level_start, { level });
- if (!GlobalConfig.isDebug) {
- AdvertMgr.instance.showInterstitial();
- }
- }
- registerListener() {
- EventDispatcher.instance.on(GameEvent.EVENT_UPDATE_LAYER, this.hide_element, this);
- }
- get_lvl(): number {
- let arr = this.node.name.split("_");
- // console.log("split get_lvl>>>>>:", arr);
- return Number(arr[1].trim());
- }
- get_hole_num(): number {
- let hole_num: number = 0;
- this.node.children.forEach(layer_node => {
- let num = layer_node.getComponent(UnitAction).get_hole_num();
- hole_num += num;
- });
- return hole_num;
- }
- async init_level() {
- for (let i = 0; i < this.node.children.length; i++) {
- const temp = this.node.children[i];
- if (temp.getComponent(CarCarColorsComponent) || temp.getComponent(CarBoxComponent)) {
- CarColorsGlobalInstance.instance.carSysterm.addCar(this.node.children[i]);
- }
- }
- this.node.children.forEach(unit_node => {
- if (unit_node.getComponent(UnitAction)) {
- unit_node.getComponent(UnitAction).init_layer();
- }
- });
- await GameUtil.delay(0.2);
- CarColorsGlobalInstance.instance.carSysterm.sortCarSeatsByDifficulty();
- const color_pin_arr = CarColorsGlobalInstance.instance.carSysterm.carSeats;
- this.node.children.forEach(unit_node => {
- if (unit_node.getComponent(UnitAction)) {
- unit_node.getComponent(UnitAction)!.init_pin(color_pin_arr);
- }
- });
- this.init_parking();
- //默认隐藏一些
- await GameUtil.delay(0.2);
- this.set_default_layer();
- // this.set_pin_color();
- }
- init_parking() {
- const points = find("Canvas/Scene/Parkings").children
- for (let index = 0; index < points.length; index++) {
- const element = points[index];
- element.name = 'empty';
- if (index > 3) {
- element.name = `empty-lock${index}`;
- element.getChildByName('Barricade')!.active = true;
- element.getChildByName('Barricade')!.getChildByName('Ad')!.active = true;
- const childrenToRemove = element.children.slice(1);
- childrenToRemove.forEach(child => child.destroy());
- } else {
- element.removeAllChildren();
- }
- }
- }
- private set_default_layer() {
- //默认都是不显示的
- let layer_arr = this.get_all_layer();
- layer_arr.forEach(layer_action => {
- layer_action.set_status(0);
- });
- this.hide_element();
- }
- get_all_layer(): LayerAction[] {
- let arr: LayerAction[] = [];
- //默认都是不显示的
- if (!this.node) return;
- for (let i = this.node.children.length - 1; i >= 0; i--) {
- if (this.node.children[i].getComponent(UnitAction)) {
- const unit = this.node.children[i].getComponent(UnitAction)!;
- unit.get_layer(arr);
- }
- }
- return arr;
- }
- private async hide_element() {
- let default_show_layer_num = 2;
- let show_num = 0;
- let layer_arr = this.get_all_layer();
- if (!layer_arr) return;
- for (let i = 0; i < layer_arr.length; i++) {
- let layer_action = layer_arr[i];
- if (layer_action.get_element_num() <= 0) {
- continue;
- }
- show_num++;
- if (show_num <= default_show_layer_num) {
- if (layer_action.layer_status != 1) {
- layer_action.set_status(1);
- // this.check_pins_block();
- }
- } else if (show_num == (default_show_layer_num + 1)) {
- if (layer_action.layer_status != 2) {
- layer_action.set_status(2);
- }
- } else {
- layer_action.set_status(0);
- }
- }
- }
- /** 根据车辆类型获取对应的载客量 */
- private getCarSeatCount(carType: CarTypes): number {
- switch (carType) {
- case CarTypes.Sedan: return 4; // 4座
- case CarTypes.Minivan: return 6; // 6座
- case CarTypes.Bus: return 8; // 8座
- default: return 4; // 默认最小
- }
- }
- //每个钉子检测是否被遮挡
- private async check_pins_block() {
- let layer_arr = this.get_all_layer();
- layer_arr.forEach(layer => {
- if (layer.layer_status == 1) {
- layer.node.children.forEach((element) => {
- const pins = element.getComponentsInChildren(PinComponent)!;
- pins.forEach(async (pin) => {
- const pinCom = pin.getComponent(PinComponent)!;
- pinCom.checkBlocking();
- })
- })
- }
- });
- }
- /** 返回顶部面板里的颜色钉子组件数组*/
- get_pin_color(): PinComponent[] {
- let arr: PinComponent[] = [];
- const units = this.node.getComponentsInChildren(UnitAction)!;
- // console.log('units.length:', units.length);
- units.forEach((unit) => {
- unit.get_pin_color(arr);
- })
- return arr
- }
- async moveToCar() {
- const { isEnd } = LevelManager.instance.levelModel;
- if (isEnd) return;
- const points = find("Canvas/Scene/Parkings").children
- let cars: Array<Node> = []
- let isEmpty = false
- for (let i = points.length; i--;) {
- if (points[i].name === "inuse" && points[i].children.length === 1) {
- cars.push(points[i].children[0])
- continue
- }
- if (points[i].name === "inuse" && points[i].children.length === 2) {
- cars.push(points[i].children[1])
- isEmpty = true
- continue
- }
- if (points[i].name === "empty") {
- isEmpty = true
- continue
- }
- }
- if (cars.length === 0) {
- // console.log("没车了!")
- return
- }
- let pinCom = null;
- let layer_arr = this.get_all_layer();
- layer_arr.forEach(layer => {
- if (layer.layer_status == 1) {
- layer.node.children.forEach((element) => {
- const pins = element.getComponentsInChildren(PinComponent)!;
- pins.forEach(async (pin) => {
- pinCom = pin.getComponent(PinComponent)!;
- if (pinCom.isBlocked)
- return
- let selectedCar: Node = null
- for (let i = cars.length; i--;) {
- const car = cars[i]
- const carComp = car.getComponent(CarCarColorsComponent)!;
- if (carComp && carComp.isFull)
- continue
- // 颜色相同
- // console.log('车颜色:', carComp.carColor, '钉子颜色:', pinCom.pin_color);
- if (carComp && pinCom) {
- if (carComp.carColor === pinCom.pin_color) {
- if (selectedCar === null) {
- selectedCar = car
- continue
- }
- if (selectedCar.getComponent(CarCarColorsComponent).roleNum === 0) {
- selectedCar = car
- }
- }
- }
- }
- // 匹配的车
- if (selectedCar !== null) {
- const pin_color = pinCom.pin_color
- CarColorsGlobalInstance.instance.carSysterm.removeColorFromSeats(pin_color)
- // console.log(`上车钉子:${CarColorLog[pinCom.pin_color]}`);
- if (selectedCar.getComponent(CarCarColorsComponent).addRole(pinCom.node)) {
- selectedCar.setParent(find("Canvas/Scene/Levels"), true);
- }
- }
- })
- });
- }
- });
- if (!isEmpty) {
- this.checkGameOver();
- }
- }
- //检测游戏是否结束
- async checkGameOver() {
- const checkOver = () => {
- const { isEnd } = LevelManager.instance.levelModel;
- if (isEnd) return;
- const isEmpty = this.checkParkingEmpty();
- if (isEmpty) return;
- const hasMatchingNails = this.hasMatchingNails();
- // console.log('hasMatchingNails:', hasMatchingNails);
- if (hasMatchingNails) {
- return false;
- }
- // 游戏结束
- const ui = tgxUIMgr.inst.getUI(UI_BattleResult)!;
- if (!ui) {
- LevelManager.instance.levelModel.isWin = false;
- LevelManager.instance.levelModel.isEnd = true;
- tgxUIMgr.inst.showUI(UI_BattleResult);
- }
- };
- this.unschedule(checkOver);
- this.scheduleOnce(checkOver, 3);
- }
- // 检查停车场是否为空
- private checkParkingEmpty(): boolean {
- const points = find("Canvas/Scene/Parkings").children;
- for (let index = 0; index < points.length; index++) {
- const point = points[index];
- const status = this.getParkingStatus(index, point);
- if (status === ParkingStatus.Empty) {
- return true; // 有空位
- }
- }
- return false; // 停车场已满
- }
- // 获取停车位状态
- private getParkingStatus(index: number, point: Node): ParkingStatus {
- if (point.name.startsWith(ParkingStatus.Locked)) {
- const barricade = point.getChildByName("Barricade");
- if (barricade && !barricade.active) {
- return ParkingStatus.Empty; // 已解锁但未停车
- }
- return ParkingStatus.Locked; // 未解锁
- }
- if (index <= 3) {
- if (point.name === ParkingStatus.InUse && point.children.length > 0) {
- return ParkingStatus.InUse; // 已停车
- }
- } else {
- if (point.name === ParkingStatus.InUse && point.children.length > 1) {
- return ParkingStatus.InUse; // 已停车
- }
- }
- return ParkingStatus.Empty; // 空闲
- }
- private hasMatchingNails(): boolean {
- const points = find("Canvas/Scene/Parkings").children;
- const layer_arr = this.get_all_layer();
- for (const point of points) {
- if (point.name === 'inuse' && point.children.length > 0) {
- const car = point.children[0];
- const carComp = car.getComponent(CarCarColorsComponent);
- if (!carComp || carComp.isFull) {
- continue; // 车辆已满,跳过
- }
- // 查找匹配的钉子
- const carColor = carComp.carColor;
- for (const layer of layer_arr) {
- if (layer.layer_status !== 1) continue;
- for (const element of layer.node.children) {
- const pins = element.getComponentsInChildren(PinComponent);
- for (const pin of pins) {
- const pinCom = pin.getComponent(PinComponent);
- if (pinCom.isBlocked) continue; // 如果钉子已被匹配,跳过
- // 颜色相同
- if (pinCom.pin_color === carColor) {
- // console.log('匹配成功');
- return true; // 找到匹配的钉子
- }
- }
- }
- }
- }
- }
- return false; // 没有找到匹配的钉子
- }
- protected onDestroy(): void {
- EventDispatcher.instance.off(GameEvent.EVENT_UPDATE_LAYER, this.hide_element);
- this.unscheduleAllCallbacks()
- }
- update(deltaTime: number) {
- // this.moveToCar();
- }
- }
|