CenterGridCell.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /** @format */
  2. import {idNum} from '../proto/typedef'
  3. import {ccUtils} from '../utils/ccUtils'
  4. import {Mgr} from '../GameControl'
  5. /** @format */
  6. const {ccclass, property, executeInEditMode, requireComponent} = cc._decorator
  7. @ccclass
  8. @executeInEditMode
  9. @requireComponent(cc.Layout)
  10. export class CenterGridCell extends cc.Component {
  11. @property(cc.Node)
  12. rawLay: cc.Node = null
  13. @property({displayName: '最大行数'})
  14. maxRaw: number = 0
  15. @property({displayName: '最大列数'})
  16. maxCol: number = 0
  17. protected onLoad() {
  18. this.checkNode()
  19. }
  20. checkNode() {
  21. let check = true
  22. if (!this.rawLay) {
  23. this.rawLay = this.node.children[0]
  24. if (!this.rawLay || !this.rawLay.children[0]) {
  25. check = false
  26. console.error('CenterGridCell:layout or children is null')
  27. }
  28. }
  29. return check
  30. }
  31. init(num: number): cc.Node[] {
  32. let nodes: cc.Node[] = []
  33. if (!this.checkNode()) return nodes
  34. let min = Math.min(num, this.maxRaw * this.maxCol)
  35. let realRow = Math.ceil(min / this.maxCol)
  36. let allRawLay = ccUtils.instantChildren(this.rawLay, realRow)
  37. for (let i = 0; i < allRawLay.length; i++) {
  38. let tmp = ccUtils.instantChildren(
  39. allRawLay[i].children[0],
  40. i == allRawLay.length - 1 ? min - this.maxCol * (allRawLay.length - 1) : this.maxCol,
  41. )
  42. nodes = nodes.concat(tmp)
  43. }
  44. return nodes
  45. }
  46. getMaxNum() {
  47. return this.maxRaw * this.maxCol
  48. }
  49. }