Sofa.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html
  7. import { SofaColor, SofaDir } from "../EventName/EventName";
  8. import Sdk, { VibrateType } from "../SDK/SDK";
  9. import { tableData } from "./DataConfig";
  10. import BarrierDrag from "./barrierDrag";
  11. const { ccclass, property } = cc._decorator;
  12. /**
  13. 按钮点击限制节流 防抖 节流
  14. @param lockTime 阻塞时间
  15. @param callBackFun 节流回调 多次点击的时候给一个回调函数提示用户不要多次点击
  16. */
  17. export function ButtonLock(lockTime: number = 0.3, callBackFun?: Function) {
  18. return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
  19. let oldFun: Function = descriptor.value;
  20. let isLock: boolean = false;
  21. descriptor.value = function (...args: any[]) {
  22. if (isLock) {
  23. callBackFun?.()
  24. return
  25. }
  26. isLock = true;
  27. setTimeout(() => {
  28. isLock = false
  29. }, lockTime * 1000);
  30. oldFun.apply(this, args);
  31. }
  32. return descriptor
  33. }
  34. }
  35. @ccclass
  36. export default class Sofa extends cc.Component {
  37. start() {
  38. }
  39. //沙发ID
  40. SofaID: number = 0
  41. //沙发Prefab 类型
  42. SofaPrefabID: number = 0
  43. //沙发颜色
  44. SofaColor: SofaColor = SofaColor.无
  45. //沙发方向
  46. Sofadir: SofaDir = SofaDir.正
  47. //沙发还可以做人的位置
  48. SofaSitDown: number[] = []
  49. //是否是上锁的
  50. isLock: boolean = false
  51. NodeBarrierDragComp: BarrierDrag = null
  52. // update (dt) {}
  53. //鸭子目标是这个沙发的时候 沙发不能被玩家移动
  54. isTarget: boolean[] = []
  55. //初始化
  56. init(BarrierDragtype: number, SofaPrefabID: number, islock) {
  57. this.SofaID = BarrierDragtype
  58. this.SofaPrefabID = SofaPrefabID
  59. this.isLock = islock
  60. //障碍物 61
  61. // 蓝 灰 粉 红 黄 蓝2 灰2 粉2 红2 黄2
  62. // 1 3 5 7 9 11 13 15 17 19
  63. // 2 4 6 8 10 12 14 16 18 20
  64. let Type = [{
  65. Has: [1, 2, 11, 12],
  66. is: SofaColor.蓝,
  67. }, {
  68. Has: [3, 4, 13, 14],
  69. is: SofaColor.灰
  70. }, {
  71. Has: [5, 6, 15, 16],
  72. is: SofaColor.粉
  73. }, {
  74. Has: [7, 8, 17, 18],
  75. is: SofaColor.红
  76. }, {
  77. Has: [9, 10, 19, 20],
  78. is: SofaColor.黄
  79. }]
  80. Type.forEach(e => {
  81. if (e.Has.includes(this.SofaPrefabID)) {
  82. this.SofaColor = e.is
  83. }
  84. if (this.SofaPrefabID % 2 === 0) {
  85. this.Sofadir = SofaDir.反
  86. } else {
  87. this.Sofadir = SofaDir.正
  88. }
  89. if (this.SofaPrefabID > 10 && this.SofaPrefabID < 21) {
  90. this.SofaSitDown = [0, 0]
  91. this.isTarget = [false, false]
  92. } else {
  93. this.SofaSitDown = [0]
  94. this.isTarget = [false]
  95. }
  96. })
  97. this.NodeBarrierDragComp = this.node.getComponent(BarrierDrag)
  98. }
  99. NowSitDownisok() {
  100. if (this.NodeBarrierDragComp.MyisDouble()) {
  101. if (this.SofaSitDown[0] == 0 || this.SofaSitDown[1] == 0) {
  102. return true
  103. }
  104. } else {
  105. if (this.SofaSitDown[0] == 0) {
  106. return true
  107. }
  108. }
  109. return false
  110. }
  111. getSitDownMapIndex() {
  112. let pathPos = []
  113. // if (this.NodeBarrierDragComp.isLock) {
  114. // return pathPos
  115. // }
  116. let NowMapPos = this.NodeBarrierDragComp.getNodeMapPos(this.node.x, this.node.y)
  117. let CheckPos = (x, y, isleft) => {
  118. var slefNode = this.node
  119. //上下左右
  120. if (x >= 0 && x < tableData[0].length &&
  121. y >= 0 && y < tableData.length
  122. ) {
  123. if (tableData[y][x] > 0) {
  124. return null
  125. } else {
  126. return { x, y, isleft, slefNode }
  127. }
  128. }
  129. }
  130. //2个位置的
  131. if (this.NodeBarrierDragComp.MyisDouble()) {
  132. //满人了
  133. if (this.SofaSitDown[0] == 1 && this.SofaSitDown[1] == 1) {
  134. return pathPos
  135. }
  136. if (this.Sofadir === SofaDir.正) {
  137. //右边
  138. if (CheckPos(NowMapPos.x + 2, NowMapPos.y, 2) && this.SofaSitDown[1] == 0) {
  139. pathPos.push(CheckPos(NowMapPos.x + 2, NowMapPos.y, 2))
  140. }
  141. //左边
  142. if (CheckPos(NowMapPos.x - 1, NowMapPos.y, 1) && this.SofaSitDown[0] == 0) {
  143. pathPos.push(CheckPos(NowMapPos.x - 1, NowMapPos.y, 1))
  144. }
  145. //左上边
  146. if (CheckPos(NowMapPos.x, NowMapPos.y + 1, 1) && this.SofaSitDown[0] == 0) {
  147. pathPos.push(CheckPos(NowMapPos.x, NowMapPos.y + 1, 1))
  148. }
  149. //右上边
  150. if (CheckPos(NowMapPos.x + 1, NowMapPos.y + 1, 2) && this.SofaSitDown[1] == 0) {
  151. pathPos.push(CheckPos(NowMapPos.x + 1, NowMapPos.y + 1, 2))
  152. }
  153. } else {
  154. //右边
  155. if (CheckPos(NowMapPos.x + 2, NowMapPos.y, 2) && this.SofaSitDown[1] == 0) {
  156. pathPos.push(CheckPos(NowMapPos.x + 2, NowMapPos.y, 2))
  157. }
  158. //左边
  159. if (CheckPos(NowMapPos.x - 1, NowMapPos.y, 1) && this.SofaSitDown[0] == 0) {
  160. pathPos.push(CheckPos(NowMapPos.x - 1, NowMapPos.y, 1))
  161. }
  162. //左上边
  163. if (CheckPos(NowMapPos.x, NowMapPos.y - 1, 1) && this.SofaSitDown[0] == 0) {
  164. pathPos.push(CheckPos(NowMapPos.x, NowMapPos.y - 1, 1))
  165. }
  166. //右上边
  167. if (CheckPos(NowMapPos.x + 1, NowMapPos.y - 1, 2) && this.SofaSitDown[1] == 0) {
  168. pathPos.push(CheckPos(NowMapPos.x + 1, NowMapPos.y - 1, 2))
  169. }
  170. }
  171. } else {
  172. //1个位置的
  173. //满人了
  174. if (this.SofaSitDown[0] == 1) {
  175. return pathPos
  176. }
  177. //右边
  178. if (CheckPos(NowMapPos.x + 1, NowMapPos.y, 0)) {
  179. pathPos.push(CheckPos(NowMapPos.x + 1, NowMapPos.y, 0))
  180. }
  181. //左边
  182. if (CheckPos(NowMapPos.x - 1, NowMapPos.y, 0)) {
  183. pathPos.push(CheckPos(NowMapPos.x - 1, NowMapPos.y, 0))
  184. }
  185. //上边
  186. if (CheckPos(NowMapPos.x, NowMapPos.y + 1, 0) && this.Sofadir == SofaDir.正) {
  187. pathPos.push(CheckPos(NowMapPos.x, NowMapPos.y + 1, 0))
  188. }
  189. //下
  190. if (CheckPos(NowMapPos.x, NowMapPos.y - 1, 0) && this.Sofadir == SofaDir.反) {
  191. pathPos.push(CheckPos(NowMapPos.x, NowMapPos.y - 1, 0))
  192. }
  193. }
  194. return pathPos
  195. }
  196. @ButtonLock(1, null)
  197. NoMoveAnim() {
  198. if (this.NodeBarrierDragComp.MyisDouble()) {
  199. cc.tween(this.node)
  200. .by(0.03, { x: 15 })
  201. .by(0.03, { x: -15 })
  202. .by(0.02, { x: 8 })
  203. .by(0.02, { x: -8 })
  204. .by(0.01, { x: 4 })
  205. .by(0.01, { x: -4 })
  206. .start()
  207. } else {
  208. cc.tween(this.node)
  209. .to(0.03, { angle: 15 })
  210. .to(0.03, { angle: -15 })
  211. .to(0.03, { angle: 8 })
  212. .to(0.01, { angle: -4 })
  213. .to(0.01, { angle: 0 })
  214. .start()
  215. }
  216. Sdk.getInstance().Vibrate(VibrateType.Short, null)
  217. }
  218. }