123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- // 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)
- }
- }
|