game.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. import EventName from "../EventName/EventName";
  2. import AudioManager from "../GameUI/AudioManager";
  3. import GameLevel from "../GameUI/GameLevel";
  4. import HonorManger from "../GameUI/HonorManger";
  5. import LocalData, { WorkState } from "../Template/LocalData";
  6. import MyComponent from "../Template/MyComponent";
  7. import { AStar } from "./aStar";
  8. import BarrierDrag from "./barrierDrag";
  9. import { BusNodeData, BusNodeDataType, BusType, ChangeMapInitOver, leftButtomStatPos, tableData, tiledSize } from "./DataConfig";
  10. import MapLayer from "./mapLayer";
  11. import Sofa from "./Sofa";
  12. const { ccclass, property } = cc._decorator;
  13. // 函数装饰器
  14. /**
  15. * 绑定事件装饰器函数
  16. * 函数的传参尽量简洁明确
  17. *
  18. * @param amount 需要传递的参数数量
  19. */
  20. export function bindShowFindPos(amount?: number): any {
  21. return function (
  22. target: any,
  23. name: string,
  24. descriptor: PropertyDescriptor
  25. ): PropertyDescriptor {
  26. const oriFn = descriptor.value;
  27. descriptor.value = function (...data: any[]) {
  28. const ret = oriFn.apply(this, data);
  29. // cc.log('----测试装饰函数----',data)
  30. return ret;
  31. };
  32. return descriptor;
  33. };
  34. }
  35. @ccclass
  36. export default class Game extends MyComponent {
  37. @property(cc.Node)
  38. fllowNode: cc.Node = null
  39. @property(cc.Node)
  40. BusMod2Node: cc.Node = null
  41. @property(cc.Node)
  42. BusMod4Node: cc.Node = null
  43. @property(cc.Node)
  44. BusMod1Node: cc.Node = null
  45. @property(cc.Node)
  46. BusMod3Node: cc.Node = null
  47. //人物初始位置
  48. playerMapPos = cc.v2(1, 1)
  49. targetPos: cc.Vec2
  50. player: cc.Node
  51. barrier: cc.Node
  52. barrierLayer: cc.Node
  53. MapLayerNode: cc.Node
  54. pathLayer: cc.Node
  55. // onLoad () {}
  56. private isShowProcess = false
  57. private aStar: AStar
  58. count: number = 0
  59. _openList = []
  60. _closeList = []
  61. protected onEnable(): void {
  62. //初始化检测游戏勋章系统
  63. console.log('初始化检测游戏勋章系统');
  64. HonorManger.getInstance().GameInit()
  65. this.schedule(() => {
  66. HonorManger.getInstance().Game601()
  67. }, 1)
  68. if (LocalData.getInstance().getWorkState() === WorkState.引导) {
  69. this.schedule(() => {
  70. //引导
  71. HonorManger.getInstance().Guide1()
  72. }, 2, 999999, 0.5)
  73. }
  74. }
  75. protected onDisable(): void {
  76. this.unscheduleAllCallbacks()
  77. }
  78. onLoad(): void {
  79. super.onLoad();
  80. var manager = cc.director.getCollisionManager();
  81. // manager.enabled = true;
  82. // manager.enabledDebugDraw = true;
  83. this.regEvent(EventName.BronBus, this.BronBus, this)
  84. this.regEvent(EventName.BronMap, this.BronMap, this)
  85. this.regEvent(EventName.driveBus, this.driveBus, this)
  86. }
  87. start() {
  88. this.initUI()
  89. AudioManager.instance.playGameBg()
  90. }
  91. //生成车子预制体
  92. async BronBus() {
  93. let Pos = BusNodeData[BusType][LocalData.getInstance().getBusSkin() - 1] as BusNodeDataType
  94. let Nodesqueue: cc.Node[] = []
  95. Nodesqueue.push(this.BusMod1Node)
  96. Nodesqueue.push(this.BusMod2Node)
  97. Nodesqueue.push(this.BusMod3Node)
  98. Nodesqueue.push(this.BusMod4Node)
  99. // Nodesqueue.push(this.fllowNode)
  100. for (let index = 0; index < Pos.BusModSp.length; index++) {
  101. const element = Pos.BusModSp[index];
  102. if (element) {
  103. //先动态加载图片
  104. let res = await this.LoadBusSpriteFrame('/Skin' + LocalData.getInstance().getBusSkin() + '/' + element) as cc.SpriteFrame
  105. Nodesqueue[index].getComponent(cc.Sprite).spriteFrame = res
  106. } else {
  107. Nodesqueue[index].getComponent(cc.Sprite).spriteFrame = null
  108. }
  109. }
  110. let resfllow = await this.LoadBusSpriteFrame('/fllow') as cc.SpriteFrame
  111. this.fllowNode.getComponent(cc.Sprite).spriteFrame = resfllow
  112. //修改缩放
  113. this.ChangeNodeScale(Pos)
  114. //修改位置
  115. this.ChangeNodePos()
  116. cc.systemEvent.emit(EventName.BronMap)
  117. }
  118. ChangeNodeScale(temp: BusNodeDataType) {
  119. let Nodesqueue: cc.Node[] = []
  120. Nodesqueue.push(this.BusMod1Node)
  121. Nodesqueue.push(this.BusMod2Node)
  122. Nodesqueue.push(this.BusMod3Node)
  123. Nodesqueue.push(this.BusMod4Node)
  124. for (let index = 0; index < Nodesqueue.length; index++) {
  125. const element = Nodesqueue[index];
  126. element.scale = temp.BusScale
  127. }
  128. this.fllowNode.scale = temp.MapScale
  129. this.node.scale = temp.MapScale
  130. }
  131. ChangeNodePos() {
  132. let Pos = BusNodeData[BusType][LocalData.getInstance().getBusSkin() - 1] as BusNodeDataType
  133. if (Pos.fllow) {
  134. this.fllowNode.setPosition(Pos.fllow?.[0], Pos.fllow?.[1])
  135. }
  136. if (Pos.BusMod2) {
  137. this.BusMod2Node.setPosition(Pos.BusMod2?.[0], Pos.BusMod2?.[1])
  138. this.BusMod2Node.active = true
  139. } else {
  140. this.BusMod2Node.active = false
  141. }
  142. if (Pos.BusMod4) {
  143. this.BusMod4Node.setPosition(Pos.BusMod4?.[0], Pos.BusMod4?.[1])
  144. this.BusMod4Node.active = true
  145. } else {
  146. this.BusMod4Node.active = false
  147. }
  148. if (Pos.BusMod1) {
  149. this.BusMod1Node.setPosition(Pos.BusMod1?.[0], Pos.BusMod1?.[1])
  150. this.BusMod1Node.active = true
  151. } else {
  152. this.BusMod1Node.active = false
  153. }
  154. if (Pos.BusMod3) {
  155. this.BusMod3Node.setPosition(Pos.BusMod3?.[0], Pos.BusMod3?.[1])
  156. this.BusMod3Node.active = true
  157. } else {
  158. this.BusMod3Node.active = false
  159. }
  160. if (Pos.gameLayer) {
  161. this.node.setPosition(Pos.gameLayer?.[0], Pos.gameLayer?.[1])
  162. }
  163. if (Pos.BgImage) {
  164. // this.BgImage.spriteFrame = Pos.BgImage
  165. }
  166. }
  167. //加载车子图片
  168. LoadBusSpriteFrame(spName: string) {
  169. return new Promise((resolve, reject) => {
  170. let LoadPath = 'res/Bus/' + BusType + spName
  171. let bundle = cc.assetManager.getBundle("sub");
  172. bundle.load(LoadPath, cc.SpriteFrame, null, (err, Sp: cc.SpriteFrame) => {
  173. if (err) {
  174. cc.error(`${LoadPath} 加载失败`)
  175. reject(err)
  176. return
  177. }
  178. resolve(Sp)
  179. })
  180. })
  181. }
  182. BronMap() {
  183. this.LoadGameLevel()
  184. }
  185. async LoadGameLevel() {
  186. // 初始化地图中的障碍物
  187. for (let i = 0; i < tableData.length; i++) {
  188. for (let j = 0; j < tableData[0].length; j++) {
  189. let v = tableData[i][j]
  190. if (v > 0) {
  191. await this.LoadSofaPrefab(v, i, j)
  192. }
  193. }
  194. }
  195. this.ShowNode()
  196. }
  197. refreshShowLabel() {
  198. let findexLableLayer = this.node.getChildByName("findexLableLayer")
  199. findexLableLayer.removeAllChildren()
  200. this.count = 0
  201. }
  202. LoadSofaPrefab(BarrierDragtype: number, i: number, j: number,) {
  203. return new Promise((resolve, reject) => {
  204. // //如果是墙就 掠过
  205. // if (BarrierDragtype == 99) {
  206. // resolve(0)
  207. // return
  208. // }
  209. //余数
  210. var yushu = BarrierDragtype % 1000
  211. //千位是不是锁
  212. var q = parseInt((BarrierDragtype % 100000 % 10000 / 1000).toString())
  213. let bundle = cc.assetManager.getBundle("sub");
  214. bundle.load('res/BarrierDrag/' + yushu, cc.Prefab, null, (err, node: cc.Prefab) => {
  215. if (err) {
  216. cc.error(`${BarrierDragtype} 加载${yushu}失败`)
  217. reject(err)
  218. return
  219. }
  220. let newBarrier = cc.instantiate(node)
  221. newBarrier.position = this.getNodePosByMapPos(j, i)
  222. newBarrier.parent = this.barrierLayer
  223. newBarrier.addComponent(BarrierDrag).isInMap = true
  224. let BarrierDragComonent = newBarrier.getComponent(BarrierDrag)
  225. BarrierDragComonent.Myid = BarrierDragtype
  226. let SofaComonent = newBarrier.addComponent(Sofa)
  227. SofaComonent.init(BarrierDragtype, yushu, q)
  228. BarrierDragComonent.onLoad()
  229. BarrierDragComonent.ShowLock()
  230. resolve(0)
  231. })
  232. })
  233. }
  234. initUI() {
  235. this.MapLayerNode = this.node.getChildByName("MapLayer")
  236. this.barrier = this.MapLayerNode.getChildByName("UILayer").getChildByName("barrier")
  237. this.barrierLayer = this.MapLayerNode.getChildByName('barrierLayer')
  238. this.pathLayer = this.MapLayerNode.getChildByName('pathLayer')
  239. // this.refreshLabe()
  240. }
  241. refreshLabe() {
  242. let MapLayer = this.node.getChildByName("MapLayer")
  243. let UILayer = MapLayer.getChildByName("UILayer")
  244. UILayer.position = cc.v3(this.getNodePosByMapPos(tableData[0].length + 1, tableData.length - 1))
  245. // this.player.position = cc.v3(this.getNodePosByMapPos(this.playerMapPos.x, this.playerMapPos.y))
  246. }
  247. async showPathsAndMove() {
  248. this.refreshShowLabel()
  249. console.time('findPath')
  250. let resultList = await this.aStar.findePaths(this.playerMapPos, this.targetPos)
  251. console.timeEnd('findPath')
  252. if (resultList[0]) {
  253. let mapPosList = <[cc.Vec2]>resultList[1]
  254. let gp = this.pathLayer.getComponent(cc.Graphics)
  255. //画openList
  256. // let closeList=<any[]>resultList[3]
  257. // // cc.log('--openList---',closeList)
  258. // for(let i=0;i<closeList.length;i++){
  259. // let nodePos=this.getNodePosByMapPos(closeList[i].pos.x,closeList[i].pos.y)
  260. // gp.fillRect(nodePos.x-tiledSize.width/2,nodePos.y-tiledSize.height/2,tiledSize.width,tiledSize.height)
  261. // gp.strokeColor = new cc.Color().fromHEX('##B23FAB');
  262. // gp.stroke()
  263. // }
  264. // 画路径
  265. // for (let i = 0; i < mapPosList.length; i++) {
  266. // let nodePos = this.getNodePosByMapPos(mapPosList[i].x, mapPosList[i].y)
  267. // gp.fillRect(nodePos.x - tiledSize.width / 2, nodePos.y - tiledSize.height / 2, tiledSize.width, tiledSize.height)
  268. // gp.strokeColor = new cc.Color().fromHEX('#4B64BC9A');
  269. // gp.stroke()
  270. // }
  271. this.playerMove(mapPosList, gp)
  272. } else {
  273. // cc.log('----找不到路径---')
  274. // let tip = cc.find('Canvas/tips').getComponent(cc.Label)
  275. // tip.node.active = true
  276. // tip.node.scale = 0
  277. // tip.node.opacity = 255
  278. // tip.string = '此路不通'
  279. // cc.tween(tip.node)
  280. // .to(0.3, { scale: 1.2 })
  281. // .to(0.1, { scale: 1 })
  282. // .delay(2)
  283. // .to(0.5, { opacity: 0 })
  284. // .call(() => {
  285. // tip.node.active = false
  286. // }).start()
  287. }
  288. // cc.log('------', resultList)
  289. }
  290. playerMove(paths: [cc.Vec2], gp?: cc.Graphics) {
  291. let quance = []
  292. paths.forEach((v) => {
  293. let move = cc.moveTo(0.2, cc.v2(this.getNodePosByMapPos(v.x, v.y)))
  294. quance.push(move)
  295. })
  296. let callBack = cc.callFunc(() => {
  297. cc.log('---moveEnd--')
  298. let targetPos = paths[paths.length - 1]
  299. this.playerMapPos = cc.v2(targetPos)
  300. if (gp) {
  301. // 如果显示过程不清除
  302. if (this.isShowProcess) return
  303. gp.clear()
  304. }
  305. })
  306. quance.push(callBack)
  307. // this.player.stopAllActions()
  308. // this.player.runAction(cc.sequence(quance))
  309. }
  310. /**
  311. * 节点坐标转为 地图坐标
  312. * @param x
  313. * @param y
  314. * @returns
  315. */
  316. getMapPosByNodePos(x?: number, y?: number): cc.Vec2 | null {
  317. x = x ?? this.node.x
  318. y = y ?? this.node.y
  319. // 这样判断,虽然写着复杂,但是比直接算出所有虚拟位置再比较效率高
  320. if (x > leftButtomStatPos.x && x < (leftButtomStatPos.x + tableData[0].length * tiledSize.width) && y > leftButtomStatPos.y && y < leftButtomStatPos.y + tableData.length * tiledSize.height) {
  321. let x1 = Math.floor((x - leftButtomStatPos.x) / tiledSize.width)
  322. let y1 = Math.floor((y - leftButtomStatPos.y) / tiledSize.height)
  323. cc.log('mapPos=', x1, y1)
  324. return cc.v2(x1, y1)
  325. }
  326. return null
  327. }
  328. /**
  329. * 地图坐标转为节点坐标
  330. * @param x
  331. * @param y
  332. * @returns
  333. */
  334. getNodePosByMapPos(x: number, y: number) {
  335. x = Math.floor(x)
  336. y = Math.floor(y)
  337. let nodePos = cc.v3(leftButtomStatPos.x + x * tiledSize.width + tiledSize.width / 2, leftButtomStatPos.y + y * tiledSize.height + tiledSize.height / 2)
  338. return nodePos
  339. }
  340. // 改变 寻路方向 4 向 和8向
  341. changeMoveDriction() {
  342. this.aStar.changeDeriction()
  343. this.refreshLabe()
  344. }
  345. changIsShowpross() {
  346. this.isShowProcess = !this.isShowProcess
  347. this.aStar.setIsShowProcess(this.isShowProcess)
  348. }
  349. addMapTable() {
  350. if (tableData.length < 20) {
  351. tableData.forEach((v, index) => {
  352. v.push(0)
  353. })
  354. //增加竖向
  355. let list = new Array(tableData[0].length).fill(0)
  356. tableData.push(list)
  357. let mapLayerCom = this.MapLayerNode.getComponent(MapLayer)
  358. mapLayerCom.initMap()
  359. this.refreshLabe()
  360. }
  361. console.log('---tableData.length--', tableData.length)
  362. }
  363. reduceMapTable() {
  364. cc.log('----tableData---', tableData)
  365. if (tableData.length > 5) {
  366. //主角重置
  367. this.playerMapPos = cc.v2(3, 3)
  368. tableData.forEach((v, _index) => {
  369. let num = v.splice(v.length - 1, 1)
  370. if (num[0] > 0) {
  371. // 需要删除对应的障碍物
  372. let Pos = cc.v2(v.length, _index)
  373. this.barrierLayer.children.forEach((child) => {
  374. if (child.name === `${Pos.x}_${Pos.y}`) {
  375. child.destroy()
  376. }
  377. })
  378. }
  379. }
  380. )
  381. // 横向的也要删除
  382. let list = tableData.splice(tableData.length - 1, 1)
  383. list[0].forEach((v, _index) => {
  384. if (v > 0) {
  385. // 需要删除对应的障碍物
  386. let Pos = cc.v2(_index, tableData.length)
  387. this.barrierLayer.children.forEach((child) => {
  388. if (child.name === `${Pos.x}_${Pos.y}`) {
  389. child.destroy()
  390. }
  391. })
  392. }
  393. })
  394. let mapLayerCom = this.MapLayerNode.getComponent(MapLayer)
  395. mapLayerCom.initMap()
  396. this.refreshLabe()
  397. }
  398. console.log('---tableData.length--', tableData.length)
  399. }
  400. exitGame() {
  401. cc.game.end()
  402. }
  403. ClearMap() {
  404. //清除沙发和人物
  405. this?.barrierLayer?.children?.forEach(e => { e?.destroy() })
  406. }
  407. ShowNode() {
  408. let yPos = 1300//GameLevel.gameLevel
  409. let ytime = 1.3
  410. for (let index = 0; index < this.ShowQueueNode.length; index++) {
  411. const element = this.ShowQueueNode[index];
  412. this.Run(element, index, ytime, yPos)
  413. }
  414. }
  415. Run(element: cc.Node, index: number, time: number, yPos: number) {
  416. cc.tween(element)
  417. .by(0, { y: -yPos })
  418. .call(() => {
  419. element.opacity = 255
  420. })
  421. .by(time, { y: yPos })
  422. .call(() => {
  423. if (index == 0) {
  424. cc.systemEvent.emit(EventName.driveBusOver)
  425. ChangeMapInitOver(true)
  426. }
  427. })
  428. .start()
  429. }
  430. ShowQueueNode = []
  431. driveBus(time = 1.5, NextCoustom = true) {
  432. let fllow = this.node.parent.getChildByName("fllow")
  433. let BusMod2 = this.node.parent.getChildByName("BusMod2")
  434. let BusMod4 = this.node.parent.getChildByName("BusMod4")
  435. let BusMod1 = this.node.parent.getChildByName("BusMod1")
  436. let BusMod3 = this.node.parent.getChildByName("BusMod3")
  437. let gameLayer = this.node.parent.getChildByName("gameLayer")
  438. let fff = []
  439. fff.push(fllow)
  440. fff.push(BusMod2)
  441. fff.push(BusMod4)
  442. fff.push(BusMod1)
  443. fff.push(BusMod3)
  444. fff.push(gameLayer)
  445. this.ShowQueueNode = fff
  446. for (let index = 0; index < fff.length; index++) {
  447. const element = fff[index];
  448. cc.tween(element)
  449. .by(time, { y: 1500 })
  450. .call(() => {
  451. element.opacity = 0
  452. if (index == 0) {
  453. this.ClearMap()
  454. if (NextCoustom) {
  455. //开车到一半
  456. cc.systemEvent.emit(EventName.driveBusMind)
  457. }
  458. }
  459. })
  460. .start()
  461. }
  462. }
  463. }