check.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * @author uu
  3. * @file 检测组件
  4. */
  5. cc.Class({
  6. extends: cc.Component,
  7. properties: {
  8. groups: [],
  9. map: [],
  10. mapLength: 8
  11. },
  12. init(g) {
  13. this._game = g
  14. this.map = g.map
  15. this.mapLength = g.rowNum
  16. for (let i = 0; i < this.mapLength; i++) { //行
  17. this.groups[i] = []
  18. for (let j = 0; j < this.mapLength; j++) { //列
  19. // this.map[i][j].getComponent('cell').growInit() //全部初始化
  20. if (!this.map[i][j]) {
  21. // cc.log('报错x,y:', i, j)
  22. }
  23. this.map[i][j].getComponent('cell').isSingle = false
  24. this.map[i][j].getComponent('cell').warningInit()
  25. this.groups[i][j] = []
  26. }
  27. }
  28. },
  29. check(g) { //该函数主要用于检测一个区块能否形成道具等
  30. let propConfig = g._controller.config.json.propConfig
  31. this._game = g
  32. this.map = g.map
  33. this.mapLength = g.rowNum
  34. let min = 999
  35. for (let i = 0; i < propConfig.length; i++) {
  36. min = propConfig[i].min < min ? propConfig[i].min : min
  37. }
  38. for (let i = 0; i < this.mapLength; i++) { //行
  39. for (let j = 0; j < this.mapLength; j++) { //列
  40. this.pushPop(this.map[i][j], i, j)
  41. let target = this.map[i][j]
  42. let x = target.getComponent('cell').iid
  43. let y = target.getComponent('cell').jid
  44. let isSingle = true
  45. if ((x - 1) >= 0 && this.map[x - 1][y].getComponent('cell').color == target.getComponent('cell').color) {
  46. isSingle = false
  47. }
  48. if ((x + 1) < this.mapLength && this.map[x + 1][y].getComponent('cell').color == target.getComponent('cell').color) {
  49. isSingle = false
  50. }
  51. if ((y - 1) >= 0 && this.map[x][y - 1].getComponent('cell').color == target.getComponent('cell').color) {
  52. isSingle = false
  53. }
  54. if ((y + 1) < this.mapLength && this.map[x][y + 1].getComponent('cell').color == target.getComponent('cell').color) {
  55. isSingle = false
  56. }
  57. this.map[i][j].getComponent('cell').isSingle = isSingle
  58. // console.log(i, j, this.map[i][j].getComponent('cell').isSingle, this.map[i][j].getComponent('cell').color)
  59. if (this.groups[i][j].length >= min) {
  60. for (let z = 0; z < propConfig.length; z++) {
  61. if (this.groups[i][j].length <= propConfig[z].max && this.groups[i][j].length >= propConfig[z].min) {
  62. this.warning(propConfig[z].type, this.groups[i][j])
  63. }
  64. }
  65. }
  66. }
  67. }
  68. },
  69. pushPop(target, i, j) { //用于判断一个方块四个方向上的方块颜色是否一样 如果一样则加入组 如果组长度小于1则返回false?
  70. // if (target.getComponent('cell').isPush==true) {
  71. // return
  72. // }
  73. target.getComponent('cell').isPush = true
  74. this.groups[i][j].push(target)
  75. let x = target.getComponent('cell').iid
  76. let y = target.getComponent('cell').jid
  77. if ((x - 1) >= 0) {
  78. if (!this.map[x - 1][y].getComponent('cell').isPush && this.map[x - 1][y].getComponent('cell').color == target.getComponent('cell').color) {
  79. this.pushPop(this.map[x - 1][y], i, j)
  80. }
  81. }
  82. if ((x + 1) < this.mapLength) {
  83. if (!this.map[x + 1][y].getComponent('cell').isPush && this.map[x + 1][y].getComponent('cell').color == target.getComponent('cell').color) {
  84. this.pushPop(this.map[x + 1][y], i, j)
  85. }
  86. }
  87. if ((y - 1) >= 0) {
  88. if (!this.map[x][y - 1].getComponent('cell').isPush && this.map[x][y - 1].getComponent('cell').color == target.getComponent('cell').color) {
  89. this.pushPop(this.map[x][y - 1], i, j)
  90. }
  91. }
  92. if ((y + 1) < this.mapLength) {
  93. if (!this.map[x][y + 1].getComponent('cell').isPush && this.map[x][y + 1].getComponent('cell').color == target.getComponent('cell').color) {
  94. this.pushPop(this.map[x][y + 1], i, j)
  95. }
  96. }
  97. // 判断方块是否单身
  98. },
  99. warning(type, group) {
  100. group.map(item => {
  101. item.getComponent('cell').onWarning(type)
  102. })
  103. }
  104. });