// 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 { SofaColor, SofaDir } from "../EventName/EventName"; import Sdk, { VibrateType } from "../SDK/SDK"; import { tableData } from "./DataConfig"; import BarrierDrag from "./barrierDrag"; const { ccclass, property } = cc._decorator; /** 按钮点击限制节流 防抖 节流 @param lockTime 阻塞时间 @param callBackFun 节流回调 多次点击的时候给一个回调函数提示用户不要多次点击 */ export function ButtonLock(lockTime: number = 0.3, callBackFun?: Function) { return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) { let oldFun: Function = descriptor.value; let isLock: boolean = false; descriptor.value = function (...args: any[]) { if (isLock) { callBackFun?.() return } isLock = true; setTimeout(() => { isLock = false }, lockTime * 1000); oldFun.apply(this, args); } return descriptor } } @ccclass export default class Sofa extends cc.Component { start() { } //沙发ID SofaID: number = 0 //沙发Prefab 类型 SofaPrefabID: number = 0 //沙发颜色 SofaColor: SofaColor = SofaColor.无 //沙发方向 Sofadir: SofaDir = SofaDir.正 //沙发还可以做人的位置 SofaSitDown: number[] = [] //是否是上锁的 isLock: boolean = false NodeBarrierDragComp: BarrierDrag = null // update (dt) {} //鸭子目标是这个沙发的时候 沙发不能被玩家移动 isTarget: boolean[] = [] //初始化 init(BarrierDragtype: number, SofaPrefabID: number, islock) { this.SofaID = BarrierDragtype this.SofaPrefabID = SofaPrefabID this.isLock = islock //障碍物 61 // 蓝 灰 粉 红 黄 蓝2 灰2 粉2 红2 黄2 // 1 3 5 7 9 11 13 15 17 19 // 2 4 6 8 10 12 14 16 18 20 let Type = [{ Has: [1, 2, 11, 12], is: SofaColor.蓝, }, { Has: [3, 4, 13, 14], is: SofaColor.灰 }, { Has: [5, 6, 15, 16], is: SofaColor.粉 }, { Has: [7, 8, 17, 18], is: SofaColor.红 }, { Has: [9, 10, 19, 20], is: SofaColor.黄 }] Type.forEach(e => { if (e.Has.includes(this.SofaPrefabID)) { this.SofaColor = e.is } if (this.SofaPrefabID % 2 === 0) { this.Sofadir = SofaDir.反 } else { this.Sofadir = SofaDir.正 } if (this.SofaPrefabID > 10 && this.SofaPrefabID < 21) { this.SofaSitDown = [0, 0] this.isTarget = [false, false] } else { this.SofaSitDown = [0] this.isTarget = [false] } }) this.NodeBarrierDragComp = this.node.getComponent(BarrierDrag) } NowSitDownisok() { if (this.NodeBarrierDragComp.MyisDouble()) { if (this.SofaSitDown[0] == 0 || this.SofaSitDown[1] == 0) { return true } } else { if (this.SofaSitDown[0] == 0) { return true } } return false } getSitDownMapIndex() { let pathPos = [] // if (this.NodeBarrierDragComp.isLock) { // return pathPos // } let NowMapPos = this.NodeBarrierDragComp.getNodeMapPos(this.node.x, this.node.y) let CheckPos = (x, y, isleft) => { var slefNode = this.node //上下左右 if (x >= 0 && x < tableData[0].length && y >= 0 && y < tableData.length ) { if (tableData[y][x] > 0) { return null } else { return { x, y, isleft, slefNode } } } } //2个位置的 if (this.NodeBarrierDragComp.MyisDouble()) { //满人了 if (this.SofaSitDown[0] == 1 && this.SofaSitDown[1] == 1) { return pathPos } if (this.Sofadir === SofaDir.正) { //右边 if (CheckPos(NowMapPos.x + 2, NowMapPos.y, 2) && this.SofaSitDown[1] == 0) { pathPos.push(CheckPos(NowMapPos.x + 2, NowMapPos.y, 2)) } //左边 if (CheckPos(NowMapPos.x - 1, NowMapPos.y, 1) && this.SofaSitDown[0] == 0) { pathPos.push(CheckPos(NowMapPos.x - 1, NowMapPos.y, 1)) } //左上边 if (CheckPos(NowMapPos.x, NowMapPos.y + 1, 1) && this.SofaSitDown[0] == 0) { pathPos.push(CheckPos(NowMapPos.x, NowMapPos.y + 1, 1)) } //右上边 if (CheckPos(NowMapPos.x + 1, NowMapPos.y + 1, 2) && this.SofaSitDown[1] == 0) { pathPos.push(CheckPos(NowMapPos.x + 1, NowMapPos.y + 1, 2)) } } else { //右边 if (CheckPos(NowMapPos.x + 2, NowMapPos.y, 2) && this.SofaSitDown[1] == 0) { pathPos.push(CheckPos(NowMapPos.x + 2, NowMapPos.y, 2)) } //左边 if (CheckPos(NowMapPos.x - 1, NowMapPos.y, 1) && this.SofaSitDown[0] == 0) { pathPos.push(CheckPos(NowMapPos.x - 1, NowMapPos.y, 1)) } //左上边 if (CheckPos(NowMapPos.x, NowMapPos.y - 1, 1) && this.SofaSitDown[0] == 0) { pathPos.push(CheckPos(NowMapPos.x, NowMapPos.y - 1, 1)) } //右上边 if (CheckPos(NowMapPos.x + 1, NowMapPos.y - 1, 2) && this.SofaSitDown[1] == 0) { pathPos.push(CheckPos(NowMapPos.x + 1, NowMapPos.y - 1, 2)) } } } else { //1个位置的 //满人了 if (this.SofaSitDown[0] == 1) { return pathPos } //右边 if (CheckPos(NowMapPos.x + 1, NowMapPos.y, 0)) { pathPos.push(CheckPos(NowMapPos.x + 1, NowMapPos.y, 0)) } //左边 if (CheckPos(NowMapPos.x - 1, NowMapPos.y, 0)) { pathPos.push(CheckPos(NowMapPos.x - 1, NowMapPos.y, 0)) } //上边 if (CheckPos(NowMapPos.x, NowMapPos.y + 1, 0) && this.Sofadir == SofaDir.正) { pathPos.push(CheckPos(NowMapPos.x, NowMapPos.y + 1, 0)) } //下 if (CheckPos(NowMapPos.x, NowMapPos.y - 1, 0) && this.Sofadir == SofaDir.反) { pathPos.push(CheckPos(NowMapPos.x, NowMapPos.y - 1, 0)) } } return pathPos } @ButtonLock(1, null) NoMoveAnim() { if (this.NodeBarrierDragComp.MyisDouble()) { cc.tween(this.node) .by(0.03, { x: 15 }) .by(0.03, { x: -15 }) .by(0.02, { x: 8 }) .by(0.02, { x: -8 }) .by(0.01, { x: 4 }) .by(0.01, { x: -4 }) .start() } else { cc.tween(this.node) .to(0.03, { angle: 15 }) .to(0.03, { angle: -15 }) .to(0.03, { angle: 8 }) .to(0.01, { angle: -4 }) .to(0.01, { angle: 0 }) .start() } Sdk.getInstance().Vibrate(VibrateType.Short, null) } }