elementCheck.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. //zh:打印消除的方块信息
  61. //console.log(i, j, this.map[i][j].getComponent('element').isSingle, this.map[i][j].getComponent('element').color)
  62. if (this.groups[i][j].length >= min) {
  63. for (let z = 0; z < propConfig.length; z++) {
  64. if (this.groups[i][j].length <= propConfig[z].max && this.groups[i][j].length >= propConfig[z].min) {
  65. this.warning(propConfig[z].type, this.groups[i][j])
  66. }
  67. }
  68. }
  69. }
  70. }
  71. },
  72. pushPop(target, i, j) { //用于判断一个方块四个方向上的方块颜色是否一样 如果一样则加入组 如果组长度小于1则返回false?
  73. // if (target.getComponent('element').isPush==true) {
  74. // return
  75. // }
  76. target.getComponent('element').isPush = true
  77. this.groups[i][j].push(target)
  78. let x = target.getComponent('element').iid
  79. let y = target.getComponent('element').jid
  80. if ((x - 1) >= 0) {
  81. if (!this.map[x - 1][y].getComponent('element').isPush && this.map[x - 1][y].getComponent('element').color == target.getComponent('element').color) {
  82. this.pushPop(this.map[x - 1][y], i, j)
  83. }
  84. }
  85. if ((x + 1) < this.mapLength) {
  86. if (!this.map[x + 1][y].getComponent('element').isPush && this.map[x + 1][y].getComponent('element').color == target.getComponent('element').color) {
  87. this.pushPop(this.map[x + 1][y], i, j)
  88. }
  89. }
  90. if ((y - 1) >= 0) {
  91. if (!this.map[x][y - 1].getComponent('element').isPush && this.map[x][y - 1].getComponent('element').color == target.getComponent('element').color) {
  92. this.pushPop(this.map[x][y - 1], i, j)
  93. }
  94. }
  95. if ((y + 1) < this.mapLength) {
  96. if (!this.map[x][y + 1].getComponent('element').isPush && this.map[x][y + 1].getComponent('element').color == target.getComponent('element').color) {
  97. this.pushPop(this.map[x][y + 1], i, j)
  98. }
  99. }
  100. // 判断方块是否单身
  101. },
  102. warning(type, group) {
  103. group.map(item => {
  104. item.getComponent('element').onWarning(type)
  105. })
  106. }
  107. });