/** * 拖拽障碍物到 */ import EventName, { SofaColor } from "../EventName/EventName"; import AudioManager from "../GameUI/AudioManager"; import HonorManger, { CheckSeatType } from "../GameUI/HonorManger"; import Sdk, { VibrateType } from "../SDK/SDK"; import LocalData, { WorkState } from "../Template/LocalData"; import MyComponent from "../Template/MyComponent"; import { GraySitDownColor, leftButtomStatPos, tableData, tableDataX, tableDataY, tiledSize } from "./DataConfig"; import Role from "./Role"; import Sofa from "./Sofa"; import Game from "./game"; //禁止多点触控 cc.macro.ENABLE_MULTI_TOUCH = false; const { ccclass, property } = cc._decorator; @ccclass export default class BarrierDrag extends MyComponent { /** * 是否在地图中 * */ isInMap: boolean = false startPos: cc.Vec3 lastPos: cc.Vec3 @property(cc.Node) barrierLayer: cc.Node = null //出生时候的位置 BronPos: cc.Vec2 = null //移动沙发用时 moveSeatHasTime = 0 public Myid: number = 0 SofaComp: Sofa = null // onLoad () {} static PlayEditer: boolean = false //是否在跳跃模式 static PlayJump: boolean = false //当前跳跃的缓动 NowTween: cc.Tween = null //当前选中的缓动 NowMoveTween: cc.Tween = null onLoad() { super.onLoad(); this.SofaComp = this.node.getComponent(Sofa) this.regEvent(EventName.NowSitDowninSeat, this.NowSitDowninSeat, this) this.regEvent(EventName.StopNowSitDowninSeatAnim, this.StopNowSitDowninSeatAnim, this) this.BronPos = this.getNodeMapPos(this.node.x, this.node.y) } StopNowSitDowninSeatAnim() { let Select = this.node.getChildByName("Select") if (Select) { Select.active = false } if (this.NowTween) { this.NowTween.stop() this.node.scale = 1 } } cloneNode: cc.Node = null cloneMe() { this.deleteCloneMe() this.cloneNode = cc.instantiate(new cc.Node()) this.cloneNode.scale = this.node.scale this.cloneNode.anchorX = this.node.anchorX this.cloneNode.anchorY = this.node.anchorY this.cloneNode.addComponent(cc.Sprite).spriteFrame = this.node.getComponent(cc.Sprite).spriteFrame this.cloneNode.setPosition(this.node.position) this.cloneNode.opacity = 120 this.cloneNode.parent = this.node.parent if (!this.MyisDouble()) { this.NowMoveTween = cc.tween(this.node).repeatForever( cc.tween() .to(0.21, { scale: 1.05 }) .to(0.21, { scale: 1 }) ).start(); } } deleteCloneMe() { if (cc.isValid(this.cloneNode)) { this.cloneNode?.destroy() } if (!this.MyisDouble()) { this.NowMoveTween?.stop() this.node.scale = 1 } } NowSitDowninSeat() { if (this.SofaComp.NowSitDownisok() == false) { return } let color: number[] = [] let RoleQueue1 = cc.Canvas.instance.node.getChildByName("RoleQueue").getChildByName("RoleQueue1") let RoleQueue2 = cc.Canvas.instance.node.getChildByName("RoleQueue").getChildByName("RoleQueue2") let a = RoleQueue1.children[0]?.getComponent(Role).MyColor let b = RoleQueue2.children[0]?.getComponent(Role).MyColor color.push(a) color.push(b) //两个准备上车的乘客 for (let index = 0; index < color.length; index++) { const element = color[index]; if (element == this.SofaComp.SofaColor || ((this.SofaComp.SofaColor == SofaColor.灰 && (GraySitDownColor == element)))) { this.NowTween = cc.tween(this.node).repeatForever( cc.tween() .to(0.4, { scale: 1.25 }) .to(0.4, { scale: 1 }) ).start(); let Select = this.node.getChildByName("Select") if (Select) { Select.active = true } return } } } ShowLock() { let Lock = this.node.getChildByName("Lock") if (this.SofaComp.isLock) { if (Lock) { Lock.active = true } } else { if (Lock) { Lock.active = false } } } onCollisionEnter(a?, b?) { return } lastMoveX = 0 lastMoveY = 0 //玩家手指移动点击的位置 ClickPos: cc.Vec2 = null //玩家手指点击在节点内的位置 ClickNodePos: cc.Vec2 = null changeTableData(temp: cc.Vec2, tag: number) { if (this.MyisDouble()) { tableData[temp.y][temp.x] = tag tableData[temp.y][temp.x + 1] = tag } else { tableData[temp.y][temp.x] = tag } } start() { this.changeTableData(this.getNodeMapPos(), 2) this.node.on(cc.Node.EventType.TOUCH_START, (event: cc.Event.EventTouch) => { //如果是引导模式 if (LocalData.getInstance().getWorkState() === WorkState.引导) { if (HonorManger.getInstance().GuideData[HonorManger.getInstance().GuideStep].bronPos.x == this.BronPos.x && HonorManger.getInstance().GuideData[HonorManger.getInstance().GuideStep].bronPos.y == this.BronPos.y ) { } else { return } } ///如果在跳跃模式 if (BarrierDrag.PlayJump == true) { //如果有鸭子目标是这个沙发 那就不能跳跃 if (this.SofaComp.isTarget[0] || this.SofaComp.isTarget[1]) { this.SofaComp.NoMoveAnim() return } if (this.MyisDouble()) { if (this.SofaComp.SofaSitDown[0] == 1 && this.SofaComp.SofaSitDown[1] == 1) { return } } else { if (this.SofaComp.SofaSitDown[0] == 1) { return } } let isleft = 0 if (this.MyisDouble()) { const touchLocation = event.getLocation(); const localPos = this.node.convertToNodeSpaceAR(touchLocation); if (localPos.x >= 40) { console.log('再右边'); isleft = 2 } else { console.log('再左边'); isleft = 1 } } cc.systemEvent.emit(EventName.NowSitDowninSeatAnim, this.node, isleft, this.SofaComp.SofaColor, this.SofaComp.Sofadir) return } if (this.iSMove() == false) { return } BarrierDrag.PlayEditer = true this.ClickPos = this.node.parent.convertToNodeSpaceAR(event.getLocation()) this.OKMove = true this.moveSeatHasTime = cc.sys.now() event.stopPropagation() this.startPos = cc.v3(this.node.x, this.node.y) this.lastPos = cc.v3(this.node.x, this.node.y) this.MoveIndex = this.getNodeMapPos(this.node.x, this.node.y) let StartMapPos = this.getNodeMapPos(this.node.x, this.node.y) this.changeTableData(StartMapPos, 0) this.isSofaPlaceOK = true const touchLocation = event.getLocation(); this.ClickNodePos = this.node.convertToNodeSpaceAR(touchLocation); this.cloneMe() AudioManager.instance.playEffect(AudioManager.seat_click_start) Sdk.getInstance().Vibrate(VibrateType.Short, null) //选中的是最上面的 不应该被别的遮挡 // this.node.zIndex = 200 this.logtableData('开始') return }) this.node.on(cc.Node.EventType.TOUCH_MOVE, (event: cc.Event.EventTouch) => { if (this.iSMove() == false) { return } if (this.OKMove == false) { return } BarrierDrag.PlayEditer = true cc.systemEvent.emit(EventName.UpdataRender) let ddddx = event.getLocation().x + event.getDelta().x let ddddy = event.getLocation().y + event.getDelta().y this.ClickPos = this.node.parent.convertToNodeSpaceAR(cc.v2(ddddx, ddddy)) }) this.node.on(cc.Node.EventType.TOUCH_END, (event: cc.Event.EventTouch) => { this.OKMove = false this.ClickPos = null if (this.iSMove() == false) { return } event.stopPropagation() BarrierDrag.PlayEditer = false this.deleteCloneMe() this.SofaPlace() cc.systemEvent.emit(EventName.UpdataRender) return }) this.node.on(cc.Node.EventType.TOUCH_CANCEL, (event: cc.Event.EventTouch) => { this.OKMove = false this.ClickPos = null if (this.iSMove() == false) { return } event.stopPropagation() BarrierDrag.PlayEditer = false this.deleteCloneMe() this.SofaPlace() cc.systemEvent.emit(EventName.UpdataRender) }) } //当前是否还可以手指移动障碍物 OKMove = false //记录手指移动的可以放置的下标 MoveIndex: cc.Vec2 = new cc.Vec2(0, 0) logtableData(st) { return let count = 0 for (let index = 16; index >= 0; index--) { if (tableData[index]) { console.log(tableData[index]?.toString() + ' ' + index); } } // 初始化地图中的障碍物 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) { count++ } } } console.error('↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑'); console.error(st, '个数:', count); } //沙发移动 SofaMove(eventx: number, eventy: number) { // console.log('xx', eventx); // console.log('yy', eventy); if (this.moveYouHua(eventx, eventy)) { // console.error('左右皆不可移动一丝'); return false } //----------------------------------- //上一次的触点 let endPos = this.getEndPosInMap() if (!endPos) { console.log('超出地图范围额了'); this.node.x -= this.lastMoveX this.node.y -= this.lastMoveY return false } //如果不超出地图范围 this.lastPos = cc.v3(this.node.x, this.node.y) //开始地图下标 let MoveIndexMapPos = this.getNodeMapPos(endPos.x, endPos.y) //检测这个点是否可以放置 if (this.MyisDouble()) { if (tableData[MoveIndexMapPos.y][MoveIndexMapPos.x] == 0 && tableData[MoveIndexMapPos.y][MoveIndexMapPos.x + 1] == 0 && MoveIndexMapPos.x <= tableData[0].length - 2 ) { this.MoveIndex.x = MoveIndexMapPos.x this.MoveIndex.y = MoveIndexMapPos.y } else { } } else { if (tableData[MoveIndexMapPos.y][MoveIndexMapPos.x] > 0) { } else { this.MoveIndex.x = MoveIndexMapPos.x this.MoveIndex.y = MoveIndexMapPos.y } } return true } update(dt: number): void { if (this.ClickPos) { let x = this.ClickPos.x - this.node.x - this.ClickNodePos.x let y = this.ClickPos.y - this.node.y - this.ClickNodePos.y // let isMOve = this.SofaMove((x / 8), (y / 8))//每次步进 八分之一 不限制速度的版本 //最大步进速度 let step = 15 let xx = (x / 8) if (xx > 0) { xx = xx > step ? step : xx } if (xx < 0) { xx = xx < -step ? -step : xx } let yy = (y / 8) if (yy > 0) { yy = yy > step ? step : yy } if (yy < 0) { yy = yy < -step ? -step : yy } // let isMOve = this.SofaMove(xx, yy)//每次步进 八分之一 while (!this.SofaMove(xx, yy)) { if (xx == 0 && yy == 0) { break } xx = xx / 2 yy = yy / 2 if (xx <= 1) { xx = 0 } if (yy <= 1) { yy = 0 } } } } moveYouHua(eventx: number, eventy: number) { if (this.CheckRect(eventx, 0) && !this.CheckRect(0, eventy)) { this.node.y += eventy return false } if (!this.CheckRect(eventx, 0) && this.CheckRect(0, eventy)) { this.node.x += eventx return false } if (!this.CheckRect(eventx, 0) && !this.CheckRect(0, eventy)) { this.node.x += eventx this.node.y += eventy return false } return true } getFivePointsOnLine(point1: cc.Vec2, point2: cc.Vec2): cc.Vec2[] { const points: cc.Vec2[] = []; for (let i = 0; i < 10; i++) {//1/3 1/5 const t = i / 20; const x = point1.x + t * (point2.x - point1.x); const y = point1.y + t * (point2.y - point1.y); points.push(new cc.Vec2(x, y)); } return points; } CheckRect(MoveX: number, MoveY: number): boolean { let xx = this.node.position.x + MoveX let yy = this.node.position.y + MoveY if (this.MyisDouble()) { if (xx < this.node.getContentSize().width / 4) { // console.log('地图左'); return true } if (xx > tableData[0].length * tiledSize.width - ((this.node.getContentSize().width / 4) * 3)) { // console.log('地图右'); return true } if (yy < this.node.getContentSize().height / 2) { // console.log('地图下'); return true } if (yy > tableData.length * tiledSize.height - (this.node.getContentSize().height / 2)) { // console.log('地图上'); return true } } else { if (xx < this.node.getContentSize().width / 2) { // console.log('地图左'); return true } if (xx > tableData[0].length * tiledSize.width - (this.node.getContentSize().width / 2)) { // console.log('地图右'); return true } if (yy < this.node.getContentSize().height / 2) { // console.log('地图下'); return true } if (yy > tableData.length * tiledSize.height - (this.node.getContentSize().height / 2)) { // console.log('地图上'); return true } } const pointA = new cc.Vec2(xx, yy); const pointB = new cc.Vec2(this.lastPos.x, this.lastPos.y); const fivePoints = this.getFivePointsOnLine(pointA, pointB); // let pathLayer = cc.Canvas.instance.node.getChildByName("gameLayer").getChildByName("MapLayer").getChildByName("pathLayer") // let gp = pathLayer.getComponent(cc.Graphics) // gp.clear() //////////////////////////////////////////////////////////// 可用版本 const scale = 0.82 const Other_OneNode_scale = 1 for (let kkk = 0; kkk < 3; kkk++) { const element = fivePoints[kkk]; let MyNode_Rect = new cc.Rect( element.x + ((1 - scale) * 0.5 * this.node.getContentSize().width), element.y + ((1 - scale) * 0.5 * this.node.getContentSize().height), this.node.getContentSize().width * scale, this.node.getContentSize().height * scale) for (let index = 0; index < this.node.parent.childrenCount; index++) { let Other_OneNode = this.node.parent.children[index] if (Other_OneNode && Other_OneNode != this.node && Other_OneNode != this.cloneNode) { let Other_OneRect = new cc.Rect( Other_OneNode.x + ((1 - Other_OneNode_scale) * 0.5 * Other_OneNode.getContentSize().width), Other_OneNode.y + ((1 - Other_OneNode_scale) * 0.5 * Other_OneNode.getContentSize().height), Other_OneNode.getContentSize().width * Other_OneNode_scale, Other_OneNode.getContentSize().height * Other_OneNode_scale) // gp.fillRect(Other_OneRect.x, Other_OneRect.y, Other_OneRect.width, Other_OneRect.height) // gp.strokeColor = new cc.Color().fromHEX('#B23FAB'); // gp.fillRect(MyNode_Rect.x, MyNode_Rect.y, MyNode_Rect.width, MyNode_Rect.height) // gp.strokeColor = new cc.Color().fromHEX('#B23FAB'); // gp.stroke() if (cc.Intersection.rectRect(MyNode_Rect, Other_OneRect)) { // console.log("矩形相交", index, kkk); // // //上下左右 // // let NowMapPos = this.getNodeMapPos(this.node.x, this.node.y) // // console.log(NowMapPos.x, NowMapPos.y); // // return true // let CurrentPos = this.getTargetPosInMap(this.MoveIndex) // let CurrentRect = new cc.Rect(CurrentPos.x, CurrentPos.y, this.node.getContentSize().width * scale, this.node.getContentSize().height * scale) // if (cc.Intersection.rectRect(CurrentRect, Other_OneRect)) { // //相交 // } else { // // 不相交 // const distance = CurrentPos.subtract(cc.v3(Other_OneRect.x, Other_OneRect.y)).len() // console.log(distance); // if (distance < 100) { // } else { // this.node.position = this.getTargetPosInMap(this.MoveIndex) // } // // this.node.position = CurrentPos // return true // } return true } else { // console.log("矩形不相交"); } } } } return false } isSofaPlaceOK = false //沙发放置 SofaPlace(event?: cc.Event.EventTouch) { if (this.isSofaPlaceOK == false) { return } this.isSofaPlaceOK = false let NowMapPos = this.getNodeMapPos(this.node.x, this.node.y) let startPos = this.getNodeMapPos(this.startPos.x, this.startPos.y) //如果是引导模式 if (LocalData.getInstance().getWorkState() === WorkState.引导) { if (HonorManger.getInstance().GuideData[HonorManger.getInstance().GuideStep].bronPos.x == this.BronPos.x && HonorManger.getInstance().GuideData[HonorManger.getInstance().GuideStep].bronPos.y == this.BronPos.y && HonorManger.getInstance().GuideData[HonorManger.getInstance().GuideStep].NowPos.x == NowMapPos.x && HonorManger.getInstance().GuideData[HonorManger.getInstance().GuideStep].NowPos.y == NowMapPos.y ) { } else { this.changeTableData(startPos, 3) this.node.position = this.getTargetPosInMap(startPos) AudioManager.instance.playEffect(AudioManager.seat_click_end) Sdk.getInstance().Vibrate(VibrateType.Short, null) return } } //移动记录 if (startPos && startPos.x == NowMapPos.x && startPos.y == NowMapPos.y ) { } else { //移动到新的位置 //1 开始倒计时 cc.systemEvent.emit(EventName.StartCountDown) //2 检测这个移动用时 let temp: CheckSeatType = { 'uuid': this.node.uuid, 'BronPos': this.BronPos, 'NowPos': NowMapPos, 'MoveSeatTime': cc.sys.now() - this.moveSeatHasTime, } HonorManger.getInstance().UpSeatMove(temp) } this.changeTableData(this.MoveIndex, 3) this.node.position = this.getTargetPosInMap(this.MoveIndex) AudioManager.instance.playEffect(AudioManager.seat_click_end) Sdk.getInstance().Vibrate(VibrateType.Short, null) this.logtableData('结束') } 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) } /** * 判断是否可以 放置 * 只能房子 表格的空格中 * */ getEndPosInMap(x?: number, y = 0) { let MapPos = cc.v2(x, y) if (x === undefined) { MapPos = this.getNodeMapPos() } if (MapPos) { return cc.v3(leftButtomStatPos.x + MapPos.x * tiledSize.width + tiledSize.width / 2, leftButtomStatPos.y + MapPos.y * tiledSize.height + tiledSize.height / 2) } return null } /** * 判断节点在地图中的位置 不在地图内,返回null */ getNodeMapPos(x?: number, y?: number): cc.Vec2 | null { x = x ?? this.node.x y = y ?? this.node.y // 注意 *** 先转换为世界坐标 世界坐标跟节点坐标不能简单等价,一定要转换成节点坐标才能用 let woldPos = this.node.parent.convertToWorldSpaceAR(cc.v2(x, y)) // 一定要加上这句 let rootPos = cc.find("Canvas/gameLayer/MapLayer").convertToNodeSpaceAR(woldPos) x = rootPos.x y = rootPos.y // console.log('---tableData.length--', tableData.length, x, 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 } audioPlay(path: string, loop = false) { return new Promise((resolve, reject) => { resolve(0) return }) } MyisDouble() { //余数 var yushu = this.Myid % 1000 if (yushu > 10 && yushu <= 20) { return true } return false } //不能移动的ID NotMoveID = [61, 99] iSMove() { for (let index = 0; index < this.SofaComp.isTarget.length; index++) { const element = this.SofaComp.isTarget[index]; if (element) { this.SofaComp.NoMoveAnim() return false } } if (this.Myid == 99) { return false } if (this.SofaComp.SofaColor == SofaColor.灰 || this.Myid == 99 || this.Myid == 61 || BarrierDrag.PlayJump == true || this.SofaComp.isLock == true) { this.SofaComp.NoMoveAnim() return false } return true } // update (dt) {} }