// Learn TypeScript: // - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html // Learn Attribute: // - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html import EventName, { SofaColor, SofaDir } from "../EventName/EventName"; import Down from "../GameUI/Down"; import GamePause from "../GameUI/GamePause"; import HonorManger from "../GameUI/HonorManger"; import LocalData from "../Template/LocalData"; import MyComponent from "../Template/MyComponent"; import { BusNodeData, BusType, ChangeRoloInitOver, GetRoloQueues, GraySitDownColor, MapRoleQueuePos, MapRoleQueuePosType, MenX, MenXLeft, MenY, MenYLeft, leftButtomStatPos, tableData, tiledSize } from "./DataConfig"; import Role from "./Role"; import Sofa from "./Sofa"; import { AStar } from "./aStar"; import BarrierDrag from "./barrierDrag"; const { ccclass, property } = cc._decorator; @ccclass export default class RoleQueue extends MyComponent { private aStar: AStar // update (dt) {} targetPos: cc.Vec2 = new cc.Vec2(0, 0) selectNode: cc.Node = null @property(cc.Node) barrierLayer: cc.Node = null @property(cc.Node) gameLayer: cc.Node = null //队列类型 RoleQueueType: number = 1 //人物预制体 @property(cc.Prefab) Role: cc.Prefab = null @property(cc.Node) RoleQueue1: cc.Node = null @property(cc.Node) RoleQueue2: cc.Node = null @property(cc.Node) An1: cc.Node = null @property(cc.Node) An2: cc.Node = null //是否发送了一次下一关的表示 。放置 双层队列发送两次 IsSendNextCoustomGameFlag = false onLoad(): void { super.onLoad(); RoleQueue.SitdownRole = 0 this.aStar = new AStar() this.regEvent(EventName.BronRole, this.BronRoles, this) this.regEvent(EventName.RoleStartBus, this.RoleStartBus, this) this.regEvent(EventName.RoleStopBus, this.RoleStopBus, this) this.regEvent(EventName.initRoleQueuePos, this.initRoleQueuePos, this) this.regEvent(EventName.NowSitDowninSeatAnim, this.NowSitDowninSeatAnim, this) this.regEvent(EventName.RestartCurrentLevel, this.RestartCurrentLevel, this) } RestartCurrentLevel() { this.RoleQueue1.children.forEach(e => { e.stopAllActions() e.destroy() }) this.RoleQueue2.children.forEach(e => { e.stopAllActions() e.destroy() }) cc.systemEvent.emit(EventName.RoleStopBus) cc.systemEvent.emit(EventName.NextCoustomGameStop) cc.systemEvent.emit(EventName.CloseDoor) } NowSitDowninSeatAnim(targetNode: cc.Node, isleft: number, SeatColor: SofaColor, Sofadir: SofaDir) { //按照先右边后左边的顺序 let rightRoleColor = this.RoleQueue1.children[0]?.getComponent(Role) let leftRoleColor = this.RoleQueue2.children[0]?.getComponent(Role) if (rightRoleColor?.MyColor == SeatColor || (SeatColor == SofaColor.灰 && GraySitDownColor == rightRoleColor?.MyColor)) { cc.systemEvent.emit(EventName.StopNowSitDowninSeatAnim) BarrierDrag.PlayJump = false rightRoleColor.NowSitDowAni(targetNode, isleft, SeatColor, Sofadir) //更新排队队列 this.UpdateRoleQueue(true) return } if (leftRoleColor?.MyColor == SeatColor || (SeatColor == SofaColor.灰 && GraySitDownColor == leftRoleColor?.MyColor) ) { cc.systemEvent.emit(EventName.StopNowSitDowninSeatAnim) BarrierDrag.PlayJump = false leftRoleColor?.NowSitDowAni(targetNode, isleft, SeatColor, Sofadir) //更新排队队列 this.UpdateRoleQueue(false) } } // 此次管卡有多少乘客 TotalRole = 0; // 现在坐下的乘客数量 static SitdownRole = 0; //初始化队列 initRoleQueuePos() { // console.log('initRoleQueuePos'); this.IsSendNextCoustomGameFlag = false if (this.isValid) { let temp: MapRoleQueuePosType = MapRoleQueuePos[BusType][LocalData.getInstance().getBusSkin() - 1] this.node.scale = BusNodeData[BusType][LocalData.getInstance().getBusSkin() - 1].RoleScale this.node.setPosition(BusNodeData[BusType][LocalData.getInstance().getBusSkin() - 1].gameLayer[0], BusNodeData[BusType][LocalData.getInstance().getBusSkin() - 1].gameLayer[1]) this.RoleQueueType = temp.type //一个队列 if (temp.type == 1) { let RoleQueue1 = this.RoleQueue1 RoleQueue1.setPosition(temp.pos[0], temp.pos[1]) if (!temp?.an) { return } if (temp?.an[0]) { this.An1.setPosition(temp.an[0][0], temp.an[0][1]) this.An1.setScale(temp.an[0][2]) this.An1.active = true } else { this.An1.active = false } if (temp?.an[1]) { this.An2.active = true } else { this.An2.active = false } } //两个队列 if (temp.type == 2) { let RoleQueue1 = this.RoleQueue1 RoleQueue1.setPosition(temp.pos[0], temp.pos[1]) let RoleQueue2 = this.RoleQueue2 RoleQueue2.setPosition(temp.pos1[0], temp.pos1[1]) if (!temp?.an) { return } if (temp?.an[0]) { this.An1.setPosition(temp.an[0][0], temp.an[0][1]) this.An1.setScale(temp.an[0][2]) this.An1.active = true } else { this.An1.active = false } if (temp?.an[1]) { this.An2.setPosition(temp.an[1][0], temp.an[1][1]) this.An2.setScale(temp.an[1][2]) this.An2.active = true } else { this.An2.active = false } } } } BronRoles() { //第一个人出生时间 let First_time = 0 let last_time = 0.2 //第一个人是侧着站立 if (this.RoleQueueType == 1) { //右边的队列 for (let index = 0; index < GetRoloQueues().length; index++) { this.scheduleOnce(() => { const element = GetRoloQueues()[index]; this.BronRole(element, this.RoleQueue1, true, index) if (index == GetRoloQueues().length - 1) { ChangeRoloInitOver(true) } }, index > 0 ? last_time : First_time) } this.TotalRole = GetRoloQueues().length } if (this.RoleQueueType == 2) { //右边的队列 for (let index = 0; index < GetRoloQueues()[0].length; index++) { this.scheduleOnce(() => { const element = GetRoloQueues()[0][index]; this.BronRole(element, this.RoleQueue1, true, index) if (index == GetRoloQueues()[0].length - 1) { ChangeRoloInitOver(true) } }, index > 0 ? last_time : First_time) } //左边的队列 for (let index = 0; index < GetRoloQueues()[1].length; index++) { this.scheduleOnce(() => { const element = GetRoloQueues()[1][index]; this.BronRole(element, this.RoleQueue2, false, index) if (index == GetRoloQueues()[1].length - 1) { ChangeRoloInitOver(true) } }, index > 0 ? last_time : First_time) } this.TotalRole = GetRoloQueues()[0].length + GetRoloQueues()[1].length } RoleQueue.SitdownRole = 0 console.log('开始发车'); } //停止上车 RoleStopBus() { this.unschedule(this.GoBus) } e_first_run_time = 0.15//0.2 e_run_time = 0.15//0.2 //每只乘客上车的间隔 e_GoBus_time = 0.15//0.2 //开始上车 RoleStartBus() { this.schedule(this.GoBus, this.e_GoBus_time, cc.macro.REPEAT_FOREVER) } //上车逻辑 GoBus() { //玩家在编辑中 不上人 if (BarrierDrag.PlayEditer == false || BarrierDrag.PlayJump == true) { //创建完毕 更新一下渲染 cc.systemEvent.emit(EventName.UpdataRender) //第一个人是侧着站立 if (this.RoleQueueType == 1) { //右边的队列 this.StartOneNode(this.RoleQueue1.children[0]?.getComponent(Role).MyColor, true) } if (this.RoleQueueType == 2) { //左边的队列 //左右随机出 // if (this.node.isValid) { // if (Math.random() > 0.5) { // this.StartOneNode(this.RoleQueue1.children[0]?.getComponent(Role).MyColor, true) // } else { // this.StartOneNode(this.RoleQueue2.children[0]?.getComponent(Role).MyColor, false) // } // } //优先出右边的 this.StartOneNode(this.RoleQueue1.children[0]?.getComponent(Role).MyColor, true) this.scheduleOnce(() => { if (this.node.isValid) { this.StartOneNode(this.RoleQueue2.children[0]?.getComponent(Role).MyColor, false) } }, 0.05) } } } onDestroy(): void { super.onDestroy(); this.unscheduleAllCallbacks() } //沙发颜色 SofaColor: SofaColor = SofaColor.红 //生成人物 BronRole(RoleColor: SofaColor, parent: cc.Node, isRightQueue: boolean, index: number) { let tempNode = cc.instantiate(this.Role) if (isRightQueue) { tempNode.parent = parent } else { parent.addChild(tempNode, 0, parent.childrenCount.toString()) } // let x = Math.floor(Math.random() * (41)) - 20 let x = Math.floor(Math.random() * (31)) - 15 tempNode.getComponent(Role).init(RoleColor) tempNode.active = true let setPos let targetpos if (isRightQueue) { setPos = -this.Right_Path - this.Right_Role_interval * (parent.childrenCount - 1) targetpos = - this.Right_Role_interval * (parent.childrenCount - 1) tempNode.setPosition(cc.v2(x, setPos)) tempNode.getComponent(Role).run('上') let move = cc.moveTo(this.Right_Path_time, cc.v2(x, targetpos)) let cb = cc.callFunc(() => { tempNode.getComponent(Role).idle('上') if (index == 0) { this.RoleQueueFirst(this.RoleQueue1, true) } }) tempNode.runAction(cc.sequence(move, cb)) } else { setPos = -this.Left_Path + this.Left_Role_interval * (parent.childrenCount - 1) targetpos = this.Left_Role_interval * (parent.childrenCount - 1) tempNode.setPosition(cc.v2(x, setPos)) tempNode.getComponent(Role).run('下') let move = cc.moveTo(this.Left_Path_time, cc.v2(x, targetpos)) let cb = cc.callFunc(() => { tempNode.getComponent(Role).idle('下') if (index == 0) { this.RoleQueueFirst(this.RoleQueue2, false) } }) tempNode.runAction(cc.sequence(move, cb)) } } async StartOneNode(RoleColor: SofaColor, isRightQueue: boolean) { //如果正在跳跃模式中 if (Down.NowJumping) { return } if (BarrierDrag.PlayEditer == true) { return } if (GamePause.GamePause == true) { return } let MoveNode // console.log(RoleColor, isRightQueue); if (isRightQueue == true) { MoveNode = this.RoleQueue1.children[0] } else { MoveNode = this.RoleQueue2.children[0] } if (isRightQueue == true) { if (tableData[MenY][MenX] != 0) { cc.log('----右侧入口被阻挡---') return } } else { if (tableData[MenYLeft][MenXLeft] != 0) { cc.log('----左侧入口被阻挡---') return } } if (!MoveNode) { if (this.RoleQueue1.children[0] || this.RoleQueue2.children[0]) { } else { if (RoleQueue.SitdownRole == this.TotalRole && this.IsSendNextCoustomGameFlag == false) { this.IsSendNextCoustomGameFlag = true cc.systemEvent.emit(EventName.RoleStopBus) cc.systemEvent.emit(EventName.NextCoustomGameStop) cc.systemEvent.emit(EventName.CloseDoor) } } return } if (!RoleColor) { // cc.log('为空直接退出') return } if (MoveNode.getComponent(Role).MyColor != RoleColor) { console.log('乘客颜色不对'); return } for (let index = 0; index < this.barrierLayer.children.length; index++) { const element = this.barrierLayer.children[index]; if (element.getComponent(Sofa)?.SofaColor == RoleColor || (element.getComponent(Sofa)?.SofaColor == SofaColor.灰 && GraySitDownColor == RoleColor)) { let paths = element.getComponent(Sofa).getSitDownMapIndex() for (let k = 0; k < paths.length; k++) { const SubData = paths[k]; let isOK = this.showPathsAndMove(cc.v2(SubData.x, SubData.y), SubData, MoveNode, isRightQueue) if (await isOK == true) { cc.systemEvent.emit(EventName.StartCountDown) //更新排队队列 this.UpdateRoleQueue(isRightQueue) //如果是单人 if (SubData.isleft == 0) { element.getComponent(Sofa).SofaSitDown = [1] } else { //如果是双人 if (SubData.isleft == 1) { element.getComponent(Sofa).SofaSitDown[0] = 1 } if (SubData.isleft == 2) { element.getComponent(Sofa).SofaSitDown[1] = 1 } } return } } } } } async showPathsAndMove(targetPos: cc.Vec2, SubData, MoveNode: cc.Node, isRightQueue: boolean) { //起始点 // console.time('findPath') let resultList if (isRightQueue == true) { // cc.log('----右侧---') resultList = await this.aStar.findePaths(cc.v2(MenX, MenY), targetPos) } else { // cc.log('----左侧---') resultList = await this.aStar.findePaths(cc.v2(MenXLeft, MenYLeft), targetPos) } if (resultList[0]) { let mapPosList = <[cc.Vec2]>resultList[1] // this.stork(resultList) this.playerMove(mapPosList, null, SubData, MoveNode, isRightQueue) return true } else { // cc.log('----找不到路径---') return null } } stork(resultList) { console.log('66'); if (resultList[0]) { let mapPosList = <[cc.Vec2]>resultList[1] let pathLayer = this.node.parent.getChildByName('MapLayer').getChildByName('pathLayer') let gp = pathLayer.getComponent(cc.Graphics) gp.clear() // 画openList let closeList = resultList[3] // cc.log('--openList---',closeList) for (let i = 0; i < closeList.length; i++) { let nodePos = this.getNodePosByMapPos(closeList[i].pos.x, closeList[i].pos.y) gp.fillRect(nodePos.x - tiledSize.width / 2, nodePos.y - tiledSize.height / 2, tiledSize.width, tiledSize.height) gp.strokeColor = new cc.Color().fromHEX('##B23FAB'); gp.stroke() } // 画路径 for (let i = 0; i < mapPosList.length; i++) { let nodePos = this.getNodePosByMapPos(mapPosList[i].x, mapPosList[i].y) gp.fillRect(nodePos.x - tiledSize.width / 2, nodePos.y - tiledSize.height / 2, tiledSize.width, tiledSize.height) gp.strokeColor = new cc.Color().fromHEX('#4B64BC9A'); gp.stroke() } } else { // cc.log('----找不到路径---') } } UpdateRoleQueue(isright: boolean = true) { if (this.RoleQueueType == 1) { //右边的队列 for (let index = 0; index < this.RoleQueue1.children.length; index++) { this.UpdateRolePos(this.RoleQueue1.children[index], this.RoleQueue1.children[index].position.x, index, true) } //第一个人是侧着站立 this.RoleQueue1.children[0]?.getComponent(Role).QueueFirstIdle(true) } if (this.RoleQueueType == 2) { if (isright) { //右边的队列 for (let index = 0; index < this.RoleQueue1.children.length; index++) { this.UpdateRolePos(this.RoleQueue1.children[index], this.RoleQueue1.children[index].position.x, index, true) } //第一个人是侧着站立 this.RoleQueueFirst(this.RoleQueue1, true) } else { //左边的队列 for (let index = 0; index < this.RoleQueue2.children.length; index++) { this.UpdateRolePos(this.RoleQueue2.children[index], this.RoleQueue2.children[index].position.x, index, false) } //第一个人是侧着站立 this.RoleQueueFirst(this.RoleQueue2, false) } } HonorManger.getInstance().remainingpassengers = this.RoleQueue1.childrenCount + this.RoleQueue2.childrenCount } RoleQueueFirst(RoleQueue: cc.Node, isRightQueue: boolean) { for (let index = 0; index < RoleQueue.children.length; index++) { const element = RoleQueue.children[index]; if (element) { if (index == 0) { element.getComponent(Role).QueueFirstIdle(isRightQueue) } else { element.getComponent(Role).QueueotherIdle(isRightQueue) } } } } getTargetPosInMap(MapPos: cc.Vec2) { return cc.v3(leftButtomStatPos.x + MapPos.x * tiledSize.width + tiledSize.width / 2, leftButtomStatPos.y + MapPos.y * tiledSize.height + tiledSize.height / 2) } //右侧追车路径 Right_Path: number = 1400 //左侧追车路径 Left_Path: number = -1400 //右侧追车时间 Right_Path_time: number = 1 //左侧追车时间 Left_Path_time: number = 1 //右侧排队间隔 Right_Role_interval: number = 50 //左侧排队间隔 Left_Role_interval: number = 100 UpdateRolePos(RoleNode: cc.Node, x, y, isRightQueue: boolean) { if (isRightQueue) { let move = cc.moveTo(this.e_first_run_time, cc.v2(x, - this.Right_Role_interval * y)) RoleNode.runAction(move) } else { let move = cc.moveTo(this.e_first_run_time, cc.v2(x, + this.Left_Role_interval * y)) RoleNode.runAction(move) } } playerMove(paths: [cc.Vec2], gp: cc.Graphics, SubData, MoveNode: cc.Node, isRightQueue: boolean) { let quance = [] let startcallBack = cc.callFunc(() => { switch (SubData.isleft) { case 0: //单人 SubData.slefNode.getComponent(Sofa).isTarget[0] = true break; case 1: //双人左 SubData.slefNode.getComponent(Sofa).isTarget[0] = true break; case 2: //双人右 SubData.slefNode.getComponent(Sofa).isTarget[1] = true break; default: break; } }) quance.push(startcallBack) if (isRightQueue == true) { let name = MoveNode.parent.childrenCount //找到路径了在放置在第一位 MoveNode.parent = this.barrierLayer MoveNode.name = name + '' //障碍物的是 200多 我们 333开始 MoveNode.zIndex = (333 + (name)) } else { let name = MoveNode.parent.childrenCount //找到路径了在放置在第一位 MoveNode.parent = this.barrierLayer MoveNode.name = name + '' //障碍物的是 200多 我们 333开始 MoveNode.zIndex = (333 + (99 - name)) } let pos = this.gameLayer.convertToNodeSpaceAR(MoveNode.convertToWorldSpaceAR(MoveNode.position)) MoveNode.setPosition(pos) if (isRightQueue == true) { quance.push(cc.callFunc(() => { MoveNode.getComponent(Role).setRoleAnimbyRun('左') })) // cc.log('----右侧---') let move = cc.moveTo(this.e_first_run_time, cc.v2(this.getNodePosByMapPos(MenX, MenY))) quance.push(move) } else { quance.push(cc.callFunc(() => { MoveNode.getComponent(Role).setRoleAnimbyRun('右') })) let move = cc.moveTo(this.e_first_run_time, cc.v2(this.getNodePosByMapPos(MenXLeft, MenYLeft))) quance.push(move) } for (let index = 0; index < paths.length; index++) { const path = paths[index]; //添加移动路径 let TargetPos = this.getNodePosByMapPos(path.x, path.y) let move if (index == 0) { move = cc.moveTo(0, cc.v2(TargetPos)) } else { move = cc.moveTo(this.e_run_time, cc.v2(TargetPos)) } //移动方向 let MoveDir = cc.callFunc(() => { let NextPath = paths[index + 1]; if (NextPath) { let NextTargetPos = this.getNodePosByMapPos(NextPath.x, NextPath.y) let xxx = TargetPos.x - NextTargetPos.x let yyy = TargetPos.y - NextTargetPos.y if (Math.abs(xxx) > 10) { if (xxx > 0) { // console.log('左'); MoveNode.getComponent(Role).setRoleAnimbyRun('左') } else { // console.log('右'); MoveNode.getComponent(Role).setRoleAnimbyRun('右') } } if (Math.abs(yyy) > 10) { if (xxx > 0) { // console.log('上'); MoveNode.getComponent(Role).setRoleAnimbyRun('上') } else { // console.log('下'); MoveNode.getComponent(Role).setRoleAnimbyRun('下') } } } }) quance.push(move) quance.push(MoveDir) } let callBack = cc.callFunc(() => { // cc.log('---moveEnd--') //人物节点 MoveNode.getComponent(Role).SitDown(SubData) }) quance.push(callBack) MoveNode.stopAllActions() MoveNode.runAction(cc.sequence(quance)) /////////////////////////////////////////////////////////////////////////////////////// } /** * 地图坐标转为节点坐标 * @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 } }