barrierDrag.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /**
  2. * 拖拽障碍物到
  3. */
  4. import EventName, { SofaColor } from "../EventName/EventName";
  5. import AudioManager from "../GameUI/AudioManager";
  6. import HonorManger, { CheckSeatType } from "../GameUI/HonorManger";
  7. import Sdk, { VibrateType } from "../SDK/SDK";
  8. import LocalData, { WorkState } from "../Template/LocalData";
  9. import MyComponent from "../Template/MyComponent";
  10. import { GraySitDownColor, leftButtomStatPos, tableData, tableDataX, tableDataY, tiledSize } from "./DataConfig";
  11. import Role from "./Role";
  12. import Sofa from "./Sofa";
  13. import Game from "./game";
  14. //禁止多点触控
  15. cc.macro.ENABLE_MULTI_TOUCH = false;
  16. const { ccclass, property } = cc._decorator;
  17. @ccclass
  18. export default class BarrierDrag extends MyComponent {
  19. /**
  20. * 是否在地图中
  21. *
  22. */
  23. isInMap: boolean = false
  24. startPos: cc.Vec3
  25. lastPos: cc.Vec3
  26. @property(cc.Node)
  27. barrierLayer: cc.Node = null
  28. //出生时候的位置
  29. BronPos: cc.Vec2 = null
  30. //移动沙发用时
  31. moveSeatHasTime = 0
  32. public Myid: number = 0
  33. SofaComp: Sofa = null
  34. // onLoad () {}
  35. static PlayEditer: boolean = false
  36. //是否在跳跃模式
  37. static PlayJump: boolean = false
  38. //当前跳跃的缓动
  39. NowTween: cc.Tween<cc.Node> = null
  40. //当前选中的缓动
  41. NowMoveTween: cc.Tween<cc.Node> = null
  42. onLoad() {
  43. super.onLoad();
  44. this.SofaComp = this.node.getComponent(Sofa)
  45. this.regEvent(EventName.NowSitDowninSeat, this.NowSitDowninSeat, this)
  46. this.regEvent(EventName.StopNowSitDowninSeatAnim, this.StopNowSitDowninSeatAnim, this)
  47. this.BronPos = this.getNodeMapPos(this.node.x, this.node.y)
  48. }
  49. StopNowSitDowninSeatAnim() {
  50. let Select = this.node.getChildByName("Select")
  51. if (Select) {
  52. Select.active = false
  53. }
  54. if (this.NowTween) {
  55. this.NowTween.stop()
  56. this.node.scale = 1
  57. }
  58. }
  59. cloneNode: cc.Node = null
  60. cloneMe() {
  61. this.deleteCloneMe()
  62. this.cloneNode = cc.instantiate(new cc.Node())
  63. this.cloneNode.scale = this.node.scale
  64. this.cloneNode.anchorX = this.node.anchorX
  65. this.cloneNode.anchorY = this.node.anchorY
  66. this.cloneNode.addComponent(cc.Sprite).spriteFrame = this.node.getComponent(cc.Sprite).spriteFrame
  67. this.cloneNode.setPosition(this.node.position)
  68. this.cloneNode.opacity = 120
  69. this.cloneNode.parent = this.node.parent
  70. if (!this.MyisDouble()) {
  71. this.NowMoveTween = cc.tween(this.node).repeatForever(
  72. cc.tween()
  73. .to(0.21, { scale: 1.05 })
  74. .to(0.21, { scale: 1 })
  75. ).start();
  76. }
  77. }
  78. deleteCloneMe() {
  79. if (cc.isValid(this.cloneNode)) {
  80. this.cloneNode?.destroy()
  81. }
  82. if (!this.MyisDouble()) {
  83. this.NowMoveTween?.stop()
  84. this.node.scale = 1
  85. }
  86. }
  87. NowSitDowninSeat() {
  88. if (this.SofaComp.NowSitDownisok() == false) {
  89. return
  90. }
  91. let color: number[] = []
  92. let RoleQueue1 = cc.Canvas.instance.node.getChildByName("RoleQueue").getChildByName("RoleQueue1")
  93. let RoleQueue2 = cc.Canvas.instance.node.getChildByName("RoleQueue").getChildByName("RoleQueue2")
  94. let a = RoleQueue1.children[0]?.getComponent(Role).MyColor
  95. let b = RoleQueue2.children[0]?.getComponent(Role).MyColor
  96. color.push(a)
  97. color.push(b)
  98. //两个准备上车的乘客
  99. for (let index = 0; index < color.length; index++) {
  100. const element = color[index];
  101. if (element == this.SofaComp.SofaColor || ((this.SofaComp.SofaColor == SofaColor.灰 && (GraySitDownColor == element)))) {
  102. this.NowTween = cc.tween(this.node).repeatForever(
  103. cc.tween()
  104. .to(0.4, { scale: 1.25 })
  105. .to(0.4, { scale: 1 })
  106. ).start();
  107. let Select = this.node.getChildByName("Select")
  108. if (Select) {
  109. Select.active = true
  110. }
  111. return
  112. }
  113. }
  114. }
  115. ShowLock() {
  116. let Lock = this.node.getChildByName("Lock")
  117. if (this.SofaComp.isLock) {
  118. if (Lock) {
  119. Lock.active = true
  120. }
  121. } else {
  122. if (Lock) {
  123. Lock.active = false
  124. }
  125. }
  126. }
  127. onCollisionEnter(a?, b?) {
  128. return
  129. }
  130. lastMoveX = 0
  131. lastMoveY = 0
  132. //玩家手指移动点击的位置
  133. ClickPos: cc.Vec2 = null
  134. //玩家手指点击在节点内的位置
  135. ClickNodePos: cc.Vec2 = null
  136. changeTableData(temp: cc.Vec2, tag: number) {
  137. if (this.MyisDouble()) {
  138. tableData[temp.y][temp.x] = tag
  139. tableData[temp.y][temp.x + 1] = tag
  140. } else {
  141. tableData[temp.y][temp.x] = tag
  142. }
  143. }
  144. start() {
  145. this.changeTableData(this.getNodeMapPos(), 2)
  146. this.node.on(cc.Node.EventType.TOUCH_START, (event: cc.Event.EventTouch) => {
  147. //如果是引导模式
  148. if (LocalData.getInstance().getWorkState() === WorkState.引导) {
  149. if (HonorManger.getInstance().GuideData[HonorManger.getInstance().GuideStep].bronPos.x == this.BronPos.x &&
  150. HonorManger.getInstance().GuideData[HonorManger.getInstance().GuideStep].bronPos.y == this.BronPos.y
  151. ) {
  152. } else {
  153. return
  154. }
  155. }
  156. ///如果在跳跃模式
  157. if (BarrierDrag.PlayJump == true) {
  158. //如果有鸭子目标是这个沙发 那就不能跳跃
  159. if (this.SofaComp.isTarget[0] || this.SofaComp.isTarget[1]) {
  160. this.SofaComp.NoMoveAnim()
  161. return
  162. }
  163. if (this.MyisDouble()) {
  164. if (this.SofaComp.SofaSitDown[0] == 1 && this.SofaComp.SofaSitDown[1] == 1) {
  165. return
  166. }
  167. } else {
  168. if (this.SofaComp.SofaSitDown[0] == 1) {
  169. return
  170. }
  171. }
  172. let isleft = 0
  173. if (this.MyisDouble()) {
  174. const touchLocation = event.getLocation();
  175. const localPos = this.node.convertToNodeSpaceAR(touchLocation);
  176. if (localPos.x >= 40) {
  177. console.log('再右边');
  178. isleft = 2
  179. } else {
  180. console.log('再左边');
  181. isleft = 1
  182. }
  183. }
  184. cc.systemEvent.emit(EventName.NowSitDowninSeatAnim, this.node, isleft, this.SofaComp.SofaColor, this.SofaComp.Sofadir)
  185. return
  186. }
  187. if (this.iSMove() == false) {
  188. return
  189. }
  190. BarrierDrag.PlayEditer = true
  191. this.ClickPos = this.node.parent.convertToNodeSpaceAR(event.getLocation())
  192. this.OKMove = true
  193. this.moveSeatHasTime = cc.sys.now()
  194. event.stopPropagation()
  195. this.startPos = cc.v3(this.node.x, this.node.y)
  196. this.lastPos = cc.v3(this.node.x, this.node.y)
  197. this.MoveIndex = this.getNodeMapPos(this.node.x, this.node.y)
  198. let StartMapPos = this.getNodeMapPos(this.node.x, this.node.y)
  199. this.changeTableData(StartMapPos, 0)
  200. this.isSofaPlaceOK = true
  201. const touchLocation = event.getLocation();
  202. this.ClickNodePos = this.node.convertToNodeSpaceAR(touchLocation);
  203. this.cloneMe()
  204. AudioManager.instance.playEffect(AudioManager.seat_click_start)
  205. Sdk.getInstance().Vibrate(VibrateType.Short, null)
  206. //选中的是最上面的 不应该被别的遮挡
  207. // this.node.zIndex = 200
  208. this.logtableData('开始')
  209. return
  210. })
  211. this.node.on(cc.Node.EventType.TOUCH_MOVE, (event: cc.Event.EventTouch) => {
  212. if (this.iSMove() == false) {
  213. return
  214. }
  215. if (this.OKMove == false) {
  216. return
  217. }
  218. BarrierDrag.PlayEditer = true
  219. cc.systemEvent.emit(EventName.UpdataRender)
  220. let ddddx = event.getLocation().x + event.getDelta().x
  221. let ddddy = event.getLocation().y + event.getDelta().y
  222. this.ClickPos = this.node.parent.convertToNodeSpaceAR(cc.v2(ddddx, ddddy))
  223. })
  224. this.node.on(cc.Node.EventType.TOUCH_END, (event: cc.Event.EventTouch) => {
  225. this.OKMove = false
  226. this.ClickPos = null
  227. if (this.iSMove() == false) {
  228. return
  229. }
  230. event.stopPropagation()
  231. BarrierDrag.PlayEditer = false
  232. this.deleteCloneMe()
  233. this.SofaPlace()
  234. cc.systemEvent.emit(EventName.UpdataRender)
  235. return
  236. })
  237. this.node.on(cc.Node.EventType.TOUCH_CANCEL, (event: cc.Event.EventTouch) => {
  238. this.OKMove = false
  239. this.ClickPos = null
  240. if (this.iSMove() == false) {
  241. return
  242. }
  243. event.stopPropagation()
  244. BarrierDrag.PlayEditer = false
  245. this.deleteCloneMe()
  246. this.SofaPlace()
  247. cc.systemEvent.emit(EventName.UpdataRender)
  248. })
  249. }
  250. //当前是否还可以手指移动障碍物
  251. OKMove = false
  252. //记录手指移动的可以放置的下标
  253. MoveIndex: cc.Vec2 = new cc.Vec2(0, 0)
  254. logtableData(st) {
  255. return
  256. let count = 0
  257. for (let index = 16; index >= 0; index--) {
  258. if (tableData[index]) {
  259. console.log(tableData[index]?.toString() + ' ' + index);
  260. }
  261. }
  262. // 初始化地图中的障碍物
  263. for (let i = 0; i < tableData.length; i++) {
  264. for (let j = 0; j < tableData[0].length; j++) {
  265. let v = tableData[i][j]
  266. if (v > 0) {
  267. count++
  268. }
  269. }
  270. }
  271. console.error('↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑');
  272. console.error(st, '个数:', count);
  273. }
  274. //沙发移动
  275. SofaMove(eventx: number, eventy: number) {
  276. // console.log('xx', eventx);
  277. // console.log('yy', eventy);
  278. if (this.moveYouHua(eventx, eventy)) {
  279. // console.error('左右皆不可移动一丝');
  280. return false
  281. }
  282. //-----------------------------------
  283. //上一次的触点
  284. let endPos = this.getEndPosInMap()
  285. if (!endPos) {
  286. console.log('超出地图范围额了');
  287. this.node.x -= this.lastMoveX
  288. this.node.y -= this.lastMoveY
  289. return false
  290. }
  291. //如果不超出地图范围
  292. this.lastPos = cc.v3(this.node.x, this.node.y)
  293. //开始地图下标
  294. let MoveIndexMapPos = this.getNodeMapPos(endPos.x, endPos.y)
  295. //检测这个点是否可以放置
  296. if (this.MyisDouble()) {
  297. if (tableData[MoveIndexMapPos.y][MoveIndexMapPos.x] == 0 &&
  298. tableData[MoveIndexMapPos.y][MoveIndexMapPos.x + 1] == 0 &&
  299. MoveIndexMapPos.x <= tableData[0].length - 2
  300. ) {
  301. this.MoveIndex.x = MoveIndexMapPos.x
  302. this.MoveIndex.y = MoveIndexMapPos.y
  303. } else {
  304. }
  305. } else {
  306. if (tableData[MoveIndexMapPos.y][MoveIndexMapPos.x] > 0) {
  307. } else {
  308. this.MoveIndex.x = MoveIndexMapPos.x
  309. this.MoveIndex.y = MoveIndexMapPos.y
  310. }
  311. }
  312. return true
  313. }
  314. update(dt: number): void {
  315. if (this.ClickPos) {
  316. let x = this.ClickPos.x - this.node.x - this.ClickNodePos.x
  317. let y = this.ClickPos.y - this.node.y - this.ClickNodePos.y
  318. // let isMOve = this.SofaMove((x / 8), (y / 8))//每次步进 八分之一 不限制速度的版本
  319. //最大步进速度
  320. let step = 15
  321. let xx = (x / 8)
  322. if (xx > 0) {
  323. xx = xx > step ? step : xx
  324. }
  325. if (xx < 0) {
  326. xx = xx < -step ? -step : xx
  327. }
  328. let yy = (y / 8)
  329. if (yy > 0) {
  330. yy = yy > step ? step : yy
  331. }
  332. if (yy < 0) {
  333. yy = yy < -step ? -step : yy
  334. }
  335. // let isMOve = this.SofaMove(xx, yy)//每次步进 八分之一
  336. while (!this.SofaMove(xx, yy)) {
  337. if (xx == 0 && yy == 0) {
  338. break
  339. }
  340. xx = xx / 2
  341. yy = yy / 2
  342. if (xx <= 1) {
  343. xx = 0
  344. }
  345. if (yy <= 1) {
  346. yy = 0
  347. }
  348. }
  349. }
  350. }
  351. moveYouHua(eventx: number, eventy: number) {
  352. if (this.CheckRect(eventx, 0) && !this.CheckRect(0, eventy)) {
  353. this.node.y += eventy
  354. return false
  355. }
  356. if (!this.CheckRect(eventx, 0) && this.CheckRect(0, eventy)) {
  357. this.node.x += eventx
  358. return false
  359. }
  360. if (!this.CheckRect(eventx, 0) && !this.CheckRect(0, eventy)) {
  361. this.node.x += eventx
  362. this.node.y += eventy
  363. return false
  364. }
  365. return true
  366. }
  367. getFivePointsOnLine(point1: cc.Vec2, point2: cc.Vec2): cc.Vec2[] {
  368. const points: cc.Vec2[] = [];
  369. for (let i = 0; i < 10; i++) {//1/3 1/5
  370. const t = i / 20;
  371. const x = point1.x + t * (point2.x - point1.x);
  372. const y = point1.y + t * (point2.y - point1.y);
  373. points.push(new cc.Vec2(x, y));
  374. }
  375. return points;
  376. }
  377. CheckRect(MoveX: number, MoveY: number): boolean {
  378. let xx = this.node.position.x + MoveX
  379. let yy = this.node.position.y + MoveY
  380. if (this.MyisDouble()) {
  381. if (xx < this.node.getContentSize().width / 4) {
  382. // console.log('地图左');
  383. return true
  384. }
  385. if (xx > tableData[0].length * tiledSize.width - ((this.node.getContentSize().width / 4) * 3)) {
  386. // console.log('地图右');
  387. return true
  388. }
  389. if (yy < this.node.getContentSize().height / 2) {
  390. // console.log('地图下');
  391. return true
  392. }
  393. if (yy > tableData.length * tiledSize.height - (this.node.getContentSize().height / 2)) {
  394. // console.log('地图上');
  395. return true
  396. }
  397. } else {
  398. if (xx < this.node.getContentSize().width / 2) {
  399. // console.log('地图左');
  400. return true
  401. }
  402. if (xx > tableData[0].length * tiledSize.width - (this.node.getContentSize().width / 2)) {
  403. // console.log('地图右');
  404. return true
  405. }
  406. if (yy < this.node.getContentSize().height / 2) {
  407. // console.log('地图下');
  408. return true
  409. }
  410. if (yy > tableData.length * tiledSize.height - (this.node.getContentSize().height / 2)) {
  411. // console.log('地图上');
  412. return true
  413. }
  414. }
  415. const pointA = new cc.Vec2(xx, yy);
  416. const pointB = new cc.Vec2(this.lastPos.x, this.lastPos.y);
  417. const fivePoints = this.getFivePointsOnLine(pointA, pointB);
  418. // let pathLayer = cc.Canvas.instance.node.getChildByName("gameLayer").getChildByName("MapLayer").getChildByName("pathLayer")
  419. // let gp = pathLayer.getComponent(cc.Graphics)
  420. // gp.clear()
  421. //////////////////////////////////////////////////////////// 可用版本
  422. const scale = 0.82
  423. const Other_OneNode_scale = 1
  424. for (let kkk = 0; kkk < 3; kkk++) {
  425. const element = fivePoints[kkk];
  426. let MyNode_Rect = new cc.Rect(
  427. element.x + ((1 - scale) * 0.5 * this.node.getContentSize().width),
  428. element.y + ((1 - scale) * 0.5 * this.node.getContentSize().height),
  429. this.node.getContentSize().width * scale,
  430. this.node.getContentSize().height * scale)
  431. for (let index = 0; index < this.node.parent.childrenCount; index++) {
  432. let Other_OneNode = this.node.parent.children[index]
  433. if (Other_OneNode && Other_OneNode != this.node && Other_OneNode != this.cloneNode) {
  434. let Other_OneRect = new cc.Rect(
  435. Other_OneNode.x + ((1 - Other_OneNode_scale) * 0.5 * Other_OneNode.getContentSize().width),
  436. Other_OneNode.y + ((1 - Other_OneNode_scale) * 0.5 * Other_OneNode.getContentSize().height),
  437. Other_OneNode.getContentSize().width * Other_OneNode_scale,
  438. Other_OneNode.getContentSize().height * Other_OneNode_scale)
  439. // gp.fillRect(Other_OneRect.x, Other_OneRect.y, Other_OneRect.width, Other_OneRect.height)
  440. // gp.strokeColor = new cc.Color().fromHEX('#B23FAB');
  441. // gp.fillRect(MyNode_Rect.x, MyNode_Rect.y, MyNode_Rect.width, MyNode_Rect.height)
  442. // gp.strokeColor = new cc.Color().fromHEX('#B23FAB');
  443. // gp.stroke()
  444. if (cc.Intersection.rectRect(MyNode_Rect, Other_OneRect)) {
  445. // console.log("矩形相交", index, kkk);
  446. // // //上下左右
  447. // // let NowMapPos = this.getNodeMapPos(this.node.x, this.node.y)
  448. // // console.log(NowMapPos.x, NowMapPos.y);
  449. // // return true
  450. // let CurrentPos = this.getTargetPosInMap(this.MoveIndex)
  451. // let CurrentRect = new cc.Rect(CurrentPos.x, CurrentPos.y, this.node.getContentSize().width * scale, this.node.getContentSize().height * scale)
  452. // if (cc.Intersection.rectRect(CurrentRect, Other_OneRect)) {
  453. // //相交
  454. // } else {
  455. // // 不相交
  456. // const distance = CurrentPos.subtract(cc.v3(Other_OneRect.x, Other_OneRect.y)).len()
  457. // console.log(distance);
  458. // if (distance < 100) {
  459. // } else {
  460. // this.node.position = this.getTargetPosInMap(this.MoveIndex)
  461. // }
  462. // // this.node.position = CurrentPos
  463. // return true
  464. // }
  465. return true
  466. } else {
  467. // console.log("矩形不相交");
  468. }
  469. }
  470. }
  471. }
  472. return false
  473. }
  474. isSofaPlaceOK = false
  475. //沙发放置
  476. SofaPlace(event?: cc.Event.EventTouch) {
  477. if (this.isSofaPlaceOK == false) {
  478. return
  479. }
  480. this.isSofaPlaceOK = false
  481. let NowMapPos = this.getNodeMapPos(this.node.x, this.node.y)
  482. let startPos = this.getNodeMapPos(this.startPos.x, this.startPos.y)
  483. //如果是引导模式
  484. if (LocalData.getInstance().getWorkState() === WorkState.引导) {
  485. if (HonorManger.getInstance().GuideData[HonorManger.getInstance().GuideStep].bronPos.x == this.BronPos.x &&
  486. HonorManger.getInstance().GuideData[HonorManger.getInstance().GuideStep].bronPos.y == this.BronPos.y &&
  487. HonorManger.getInstance().GuideData[HonorManger.getInstance().GuideStep].NowPos.x == NowMapPos.x &&
  488. HonorManger.getInstance().GuideData[HonorManger.getInstance().GuideStep].NowPos.y == NowMapPos.y
  489. ) {
  490. } else {
  491. this.changeTableData(startPos, 3)
  492. this.node.position = this.getTargetPosInMap(startPos)
  493. AudioManager.instance.playEffect(AudioManager.seat_click_end)
  494. Sdk.getInstance().Vibrate(VibrateType.Short, null)
  495. return
  496. }
  497. }
  498. //移动记录
  499. if (startPos &&
  500. startPos.x == NowMapPos.x &&
  501. startPos.y == NowMapPos.y
  502. ) {
  503. } else {
  504. //移动到新的位置
  505. //1 开始倒计时
  506. cc.systemEvent.emit(EventName.StartCountDown)
  507. //2 检测这个移动用时
  508. let temp: CheckSeatType = {
  509. 'uuid': this.node.uuid,
  510. 'BronPos': this.BronPos,
  511. 'NowPos': NowMapPos,
  512. 'MoveSeatTime': cc.sys.now() - this.moveSeatHasTime,
  513. }
  514. HonorManger.getInstance().UpSeatMove(temp)
  515. }
  516. this.changeTableData(this.MoveIndex, 3)
  517. this.node.position = this.getTargetPosInMap(this.MoveIndex)
  518. AudioManager.instance.playEffect(AudioManager.seat_click_end)
  519. Sdk.getInstance().Vibrate(VibrateType.Short, null)
  520. this.logtableData('结束')
  521. }
  522. getTargetPosInMap(MapPos: cc.Vec2) {
  523. return cc.v3(leftButtomStatPos.x + MapPos.x * tiledSize.width + tiledSize.width / 2, leftButtomStatPos.y + MapPos.y * tiledSize.height + tiledSize.height / 2)
  524. }
  525. /**
  526. * 判断是否可以 放置
  527. * 只能房子 表格的空格中
  528. * */
  529. getEndPosInMap(x?: number, y = 0) {
  530. let MapPos = cc.v2(x, y)
  531. if (x === undefined) {
  532. MapPos = this.getNodeMapPos()
  533. }
  534. if (MapPos) {
  535. return cc.v3(leftButtomStatPos.x + MapPos.x * tiledSize.width + tiledSize.width / 2, leftButtomStatPos.y + MapPos.y * tiledSize.height + tiledSize.height / 2)
  536. }
  537. return null
  538. }
  539. /**
  540. * 判断节点在地图中的位置 不在地图内,返回null
  541. */
  542. getNodeMapPos(x?: number, y?: number): cc.Vec2 | null {
  543. x = x ?? this.node.x
  544. y = y ?? this.node.y
  545. // 注意 *** 先转换为世界坐标 世界坐标跟节点坐标不能简单等价,一定要转换成节点坐标才能用
  546. let woldPos = this.node.parent.convertToWorldSpaceAR(cc.v2(x, y))
  547. // 一定要加上这句
  548. let rootPos = cc.find("Canvas/gameLayer/MapLayer").convertToNodeSpaceAR(woldPos)
  549. x = rootPos.x
  550. y = rootPos.y
  551. // console.log('---tableData.length--', tableData.length, x, y)
  552. // 这样判断,虽然写着复杂,但是比直接算出所有虚拟位置再比较效率高
  553. if (x > leftButtomStatPos.x && x < (leftButtomStatPos.x + tableData[0].length * tiledSize.width) && y > leftButtomStatPos.y && y < leftButtomStatPos.y + tableData.length * tiledSize.height) {
  554. let x1 = Math.floor((x - leftButtomStatPos.x) / tiledSize.width)
  555. let y1 = Math.floor((y - leftButtomStatPos.y) / tiledSize.height)
  556. // cc.log('mapPos=', x1, y1)
  557. return cc.v2(x1, y1)
  558. }
  559. return null
  560. }
  561. audioPlay(path: string, loop = false) {
  562. return new Promise((resolve, reject) => {
  563. resolve(0)
  564. return
  565. })
  566. }
  567. MyisDouble() {
  568. //余数
  569. var yushu = this.Myid % 1000
  570. if (yushu > 10 && yushu <= 20) {
  571. return true
  572. }
  573. return false
  574. }
  575. //不能移动的ID
  576. NotMoveID = [61, 99]
  577. iSMove() {
  578. for (let index = 0; index < this.SofaComp.isTarget.length; index++) {
  579. const element = this.SofaComp.isTarget[index];
  580. if (element) {
  581. this.SofaComp.NoMoveAnim()
  582. return false
  583. }
  584. }
  585. if (this.Myid == 99) {
  586. return false
  587. }
  588. if (this.SofaComp.SofaColor == SofaColor.灰 || this.Myid == 99 || this.Myid == 61 || BarrierDrag.PlayJump == true || this.SofaComp.isLock == true) {
  589. this.SofaComp.NoMoveAnim()
  590. return false
  591. }
  592. return true
  593. }
  594. // update (dt) {}
  595. }