elementCheck.js 4.1 KB

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