123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595 |
- 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=<any[]>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()
- // }
- this.playerMove(mapPosList, gp)
- } else {
- // cc.log('----找不到路径---')
- // let tip = cc.find('Canvas/tips').getComponent(cc.Label)
- // tip.node.active = true
- // tip.node.scale = 0
- // tip.node.opacity = 255
- // tip.string = '此路不通'
- // cc.tween(tip.node)
- // .to(0.3, { scale: 1.2 })
- // .to(0.1, { scale: 1 })
- // .delay(2)
- // .to(0.5, { opacity: 0 })
- // .call(() => {
- // 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()
- }
- }
- }
|