import EventName from "../EventName/EventName"; import AudioManager from "../GameUI/AudioManager"; import GameLevel from "../GameUI/GameLevel"; import HonorManger from "../GameUI/HonorManger"; import LocalData, { WorkState } from "../Template/LocalData"; import MyComponent from "../Template/MyComponent"; import { AStar } from "./aStar"; import BarrierDrag from "./barrierDrag"; import { BusNodeData, BusNodeDataType, BusType, ChangeMapInitOver, leftButtomStatPos, tableData, tiledSize } from "./DataConfig"; import MapLayer from "./mapLayer"; import Sofa from "./Sofa"; const { ccclass, property } = cc._decorator; // 函数装饰器 /** * 绑定事件装饰器函数 * 函数的传参尽量简洁明确 * * @param amount 需要传递的参数数量 */ export function bindShowFindPos(amount?: number): any { return function ( target: any, name: string, descriptor: PropertyDescriptor ): PropertyDescriptor { const oriFn = descriptor.value; descriptor.value = function (...data: any[]) { const ret = oriFn.apply(this, data); // cc.log('----测试装饰函数----',data) return ret; }; return descriptor; }; } @ccclass export default class Game extends MyComponent { @property(cc.Node) fllowNode: cc.Node = null @property(cc.Node) BusMod2Node: cc.Node = null @property(cc.Node) BusMod4Node: cc.Node = null @property(cc.Node) BusMod1Node: cc.Node = null @property(cc.Node) BusMod3Node: cc.Node = null //人物初始位置 playerMapPos = cc.v2(1, 1) targetPos: cc.Vec2 player: cc.Node barrier: cc.Node barrierLayer: cc.Node MapLayerNode: cc.Node pathLayer: cc.Node // onLoad () {} private isShowProcess = false private aStar: AStar count: number = 0 _openList = [] _closeList = [] protected onEnable(): void { //初始化检测游戏勋章系统 console.log('初始化检测游戏勋章系统'); HonorManger.getInstance().GameInit() this.schedule(() => { HonorManger.getInstance().Game601() }, 1) if (LocalData.getInstance().getWorkState() === WorkState.引导) { this.schedule(() => { //引导 HonorManger.getInstance().Guide1() }, 2, 999999, 0.5) } } protected onDisable(): void { this.unscheduleAllCallbacks() } onLoad(): void { super.onLoad(); var manager = cc.director.getCollisionManager(); // manager.enabled = true; // manager.enabledDebugDraw = true; this.regEvent(EventName.BronBus, this.BronBus, this) this.regEvent(EventName.BronMap, this.BronMap, this) this.regEvent(EventName.driveBus, this.driveBus, this) } start() { this.initUI() AudioManager.instance.playGameBg() } //生成车子预制体 async BronBus() { let Pos = BusNodeData[BusType][LocalData.getInstance().getBusSkin() - 1] as BusNodeDataType let Nodesqueue: cc.Node[] = [] Nodesqueue.push(this.BusMod1Node) Nodesqueue.push(this.BusMod2Node) Nodesqueue.push(this.BusMod3Node) Nodesqueue.push(this.BusMod4Node) // Nodesqueue.push(this.fllowNode) for (let index = 0; index < Pos.BusModSp.length; index++) { const element = Pos.BusModSp[index]; if (element) { //先动态加载图片 let res = await this.LoadBusSpriteFrame('/Skin' + LocalData.getInstance().getBusSkin() + '/' + element) as cc.SpriteFrame Nodesqueue[index].getComponent(cc.Sprite).spriteFrame = res } else { Nodesqueue[index].getComponent(cc.Sprite).spriteFrame = null } } let resfllow = await this.LoadBusSpriteFrame('/fllow') as cc.SpriteFrame this.fllowNode.getComponent(cc.Sprite).spriteFrame = resfllow //修改缩放 this.ChangeNodeScale(Pos) //修改位置 this.ChangeNodePos() cc.systemEvent.emit(EventName.BronMap) } ChangeNodeScale(temp: BusNodeDataType) { let Nodesqueue: cc.Node[] = [] Nodesqueue.push(this.BusMod1Node) Nodesqueue.push(this.BusMod2Node) Nodesqueue.push(this.BusMod3Node) Nodesqueue.push(this.BusMod4Node) for (let index = 0; index < Nodesqueue.length; index++) { const element = Nodesqueue[index]; element.scale = temp.BusScale } this.fllowNode.scale = temp.MapScale this.node.scale = temp.MapScale } ChangeNodePos() { let Pos = BusNodeData[BusType][LocalData.getInstance().getBusSkin() - 1] as BusNodeDataType if (Pos.fllow) { this.fllowNode.setPosition(Pos.fllow?.[0], Pos.fllow?.[1]) } if (Pos.BusMod2) { this.BusMod2Node.setPosition(Pos.BusMod2?.[0], Pos.BusMod2?.[1]) this.BusMod2Node.active = true } else { this.BusMod2Node.active = false } if (Pos.BusMod4) { this.BusMod4Node.setPosition(Pos.BusMod4?.[0], Pos.BusMod4?.[1]) this.BusMod4Node.active = true } else { this.BusMod4Node.active = false } if (Pos.BusMod1) { this.BusMod1Node.setPosition(Pos.BusMod1?.[0], Pos.BusMod1?.[1]) this.BusMod1Node.active = true } else { this.BusMod1Node.active = false } if (Pos.BusMod3) { this.BusMod3Node.setPosition(Pos.BusMod3?.[0], Pos.BusMod3?.[1]) this.BusMod3Node.active = true } else { this.BusMod3Node.active = false } if (Pos.gameLayer) { this.node.setPosition(Pos.gameLayer?.[0], Pos.gameLayer?.[1]) } if (Pos.BgImage) { // this.BgImage.spriteFrame = Pos.BgImage } } //加载车子图片 LoadBusSpriteFrame(spName: string) { return new Promise((resolve, reject) => { let LoadPath = 'res/Bus/' + BusType + spName let bundle = cc.assetManager.getBundle("sub"); bundle.load(LoadPath, cc.SpriteFrame, null, (err, Sp: cc.SpriteFrame) => { if (err) { cc.error(`${LoadPath} 加载失败`) reject(err) return } resolve(Sp) }) }) } BronMap() { this.LoadGameLevel() } async LoadGameLevel() { // 初始化地图中的障碍物 for (let i = 0; i < tableData.length; i++) { for (let j = 0; j < tableData[0].length; j++) { let v = tableData[i][j] if (v > 0) { await this.LoadSofaPrefab(v, i, j) } } } this.ShowNode() } refreshShowLabel() { let findexLableLayer = this.node.getChildByName("findexLableLayer") findexLableLayer.removeAllChildren() this.count = 0 } LoadSofaPrefab(BarrierDragtype: number, i: number, j: number,) { return new Promise((resolve, reject) => { // //如果是墙就 掠过 // if (BarrierDragtype == 99) { // resolve(0) // return // } //余数 var yushu = BarrierDragtype % 1000 //千位是不是锁 var q = parseInt((BarrierDragtype % 100000 % 10000 / 1000).toString()) let bundle = cc.assetManager.getBundle("sub"); bundle.load('res/BarrierDrag/' + yushu, cc.Prefab, null, (err, node: cc.Prefab) => { if (err) { cc.error(`${BarrierDragtype} 加载${yushu}失败`) reject(err) return } let newBarrier = cc.instantiate(node) newBarrier.position = this.getNodePosByMapPos(j, i) newBarrier.parent = this.barrierLayer newBarrier.addComponent(BarrierDrag).isInMap = true let BarrierDragComonent = newBarrier.getComponent(BarrierDrag) BarrierDragComonent.Myid = BarrierDragtype let SofaComonent = newBarrier.addComponent(Sofa) SofaComonent.init(BarrierDragtype, yushu, q) BarrierDragComonent.onLoad() BarrierDragComonent.ShowLock() resolve(0) }) }) } initUI() { this.MapLayerNode = this.node.getChildByName("MapLayer") this.barrier = this.MapLayerNode.getChildByName("UILayer").getChildByName("barrier") this.barrierLayer = this.MapLayerNode.getChildByName('barrierLayer') this.pathLayer = this.MapLayerNode.getChildByName('pathLayer') // this.refreshLabe() } refreshLabe() { let MapLayer = this.node.getChildByName("MapLayer") let UILayer = MapLayer.getChildByName("UILayer") UILayer.position = cc.v3(this.getNodePosByMapPos(tableData[0].length + 1, tableData.length - 1)) // this.player.position = cc.v3(this.getNodePosByMapPos(this.playerMapPos.x, this.playerMapPos.y)) } async showPathsAndMove() { this.refreshShowLabel() console.time('findPath') let resultList = await this.aStar.findePaths(this.playerMapPos, this.targetPos) console.timeEnd('findPath') if (resultList[0]) { let mapPosList = <[cc.Vec2]>resultList[1] let gp = this.pathLayer.getComponent(cc.Graphics) //画openList // let closeList=resultList[3] // // cc.log('--openList---',closeList) // for(let i=0;i { // tip.node.active = false // }).start() } // cc.log('------', resultList) } playerMove(paths: [cc.Vec2], gp?: cc.Graphics) { let quance = [] paths.forEach((v) => { let move = cc.moveTo(0.2, cc.v2(this.getNodePosByMapPos(v.x, v.y))) quance.push(move) }) let callBack = cc.callFunc(() => { cc.log('---moveEnd--') let targetPos = paths[paths.length - 1] this.playerMapPos = cc.v2(targetPos) if (gp) { // 如果显示过程不清除 if (this.isShowProcess) return gp.clear() } }) quance.push(callBack) // this.player.stopAllActions() // this.player.runAction(cc.sequence(quance)) } /** * 节点坐标转为 地图坐标 * @param x * @param y * @returns */ getMapPosByNodePos(x?: number, y?: number): cc.Vec2 | null { x = x ?? this.node.x y = y ?? this.node.y // 这样判断,虽然写着复杂,但是比直接算出所有虚拟位置再比较效率高 if (x > leftButtomStatPos.x && x < (leftButtomStatPos.x + tableData[0].length * tiledSize.width) && y > leftButtomStatPos.y && y < leftButtomStatPos.y + tableData.length * tiledSize.height) { let x1 = Math.floor((x - leftButtomStatPos.x) / tiledSize.width) let y1 = Math.floor((y - leftButtomStatPos.y) / tiledSize.height) cc.log('mapPos=', x1, y1) return cc.v2(x1, y1) } return null } /** * 地图坐标转为节点坐标 * @param x * @param y * @returns */ getNodePosByMapPos(x: number, y: number) { x = Math.floor(x) y = Math.floor(y) let nodePos = cc.v3(leftButtomStatPos.x + x * tiledSize.width + tiledSize.width / 2, leftButtomStatPos.y + y * tiledSize.height + tiledSize.height / 2) return nodePos } // 改变 寻路方向 4 向 和8向 changeMoveDriction() { this.aStar.changeDeriction() this.refreshLabe() } changIsShowpross() { this.isShowProcess = !this.isShowProcess this.aStar.setIsShowProcess(this.isShowProcess) } addMapTable() { if (tableData.length < 20) { tableData.forEach((v, index) => { v.push(0) }) //增加竖向 let list = new Array(tableData[0].length).fill(0) tableData.push(list) let mapLayerCom = this.MapLayerNode.getComponent(MapLayer) mapLayerCom.initMap() this.refreshLabe() } console.log('---tableData.length--', tableData.length) } reduceMapTable() { cc.log('----tableData---', tableData) if (tableData.length > 5) { //主角重置 this.playerMapPos = cc.v2(3, 3) tableData.forEach((v, _index) => { let num = v.splice(v.length - 1, 1) if (num[0] > 0) { // 需要删除对应的障碍物 let Pos = cc.v2(v.length, _index) this.barrierLayer.children.forEach((child) => { if (child.name === `${Pos.x}_${Pos.y}`) { child.destroy() } }) } } ) // 横向的也要删除 let list = tableData.splice(tableData.length - 1, 1) list[0].forEach((v, _index) => { if (v > 0) { // 需要删除对应的障碍物 let Pos = cc.v2(_index, tableData.length) this.barrierLayer.children.forEach((child) => { if (child.name === `${Pos.x}_${Pos.y}`) { child.destroy() } }) } }) let mapLayerCom = this.MapLayerNode.getComponent(MapLayer) mapLayerCom.initMap() this.refreshLabe() } console.log('---tableData.length--', tableData.length) } exitGame() { cc.game.end() } ClearMap() { //清除沙发和人物 this?.barrierLayer?.children?.forEach(e => { e?.destroy() }) } ShowNode() { let yPos = 1300//GameLevel.gameLevel let ytime = 1.3 for (let index = 0; index < this.ShowQueueNode.length; index++) { const element = this.ShowQueueNode[index]; this.Run(element, index, ytime, yPos) } } Run(element: cc.Node, index: number, time: number, yPos: number) { cc.tween(element) .by(0, { y: -yPos }) .call(() => { element.opacity = 255 }) .by(time, { y: yPos }) .call(() => { if (index == 0) { cc.systemEvent.emit(EventName.driveBusOver) ChangeMapInitOver(true) } }) .start() } ShowQueueNode = [] driveBus(time = 1.5, NextCoustom = true) { let fllow = this.node.parent.getChildByName("fllow") let BusMod2 = this.node.parent.getChildByName("BusMod2") let BusMod4 = this.node.parent.getChildByName("BusMod4") let BusMod1 = this.node.parent.getChildByName("BusMod1") let BusMod3 = this.node.parent.getChildByName("BusMod3") let gameLayer = this.node.parent.getChildByName("gameLayer") let fff = [] fff.push(fllow) fff.push(BusMod2) fff.push(BusMod4) fff.push(BusMod1) fff.push(BusMod3) fff.push(gameLayer) this.ShowQueueNode = fff for (let index = 0; index < fff.length; index++) { const element = fff[index]; cc.tween(element) .by(time, { y: 1500 }) .call(() => { element.opacity = 0 if (index == 0) { this.ClearMap() if (NextCoustom) { //开车到一半 cc.systemEvent.emit(EventName.driveBusMind) } } }) .start() } } }