Game.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { _decorator, Component, instantiate, Node,Prefab ,Button,AudioClip,AudioSource} from 'cc';
  2. const { ccclass, property } = _decorator;
  3. import { Item } from './Item';
  4. import { GameManager } from './Manager/GameManager';
  5. @ccclass('Game')
  6. export class Game extends Component {
  7. @property({type:Prefab})
  8. preBlock = null
  9. private row:number=13;
  10. private col:number=12;
  11. private lockArr:Item[];
  12. private itemDic={};
  13. @property({type:[AudioClip]})// 晋级 选择颜色 结束
  14. otherAudioArr = []
  15. audioSource: AudioSource;
  16. start() {
  17. this.audioSource = this.node.getComponent(AudioSource)
  18. this.lockArr=[];
  19. this.createItems();
  20. for(let i=1;i<=4;i++){
  21. this.node.getChildByPath("Btns/Button_"+i).on(Button.EventType.CLICK, this.callback, this);
  22. }
  23. }
  24. reset(): void {
  25. this.lockArr=[];
  26. this.itemDic={}
  27. this.node.getChildByName("Con").removeAllChildren()
  28. this.createItems();
  29. }
  30. callback (button: Button) {
  31. let number=Number(button.name.replace("<Button>","").split("_")[1]);
  32. console.log("callback==>>"+button.name+","+number)
  33. this.checkColor(number-1)
  34. this.audioSource.playOneShot(this.otherAudioArr[1],1)
  35. }
  36. createItems(){
  37. let con=this.node.getChildByName("Con")
  38. for(let i=0;i<this.row;i++){
  39. for(let j=0;j<this.col;j++){
  40. let item = instantiate(this.preBlock)
  41. item.parent = con
  42. item.setPosition(i*80+40,-j*80-40,0)
  43. let itemScript = item.getComponent(Item)
  44. let num_type_random = Math.floor(Math.random()*4)
  45. itemScript.init(num_type_random,i,j)
  46. if(i==0 && j==0){
  47. this.lockArr.push(itemScript)
  48. itemScript.isLock=true
  49. }
  50. this.itemDic[i+","+j]=itemScript
  51. }
  52. }
  53. this.checkColor(this.lockArr[0].numType);
  54. console.log("lockArr=============================>"+this.lockArr.length)
  55. }
  56. checkColor(numType){
  57. let count=0;
  58. let checkItemDic={};
  59. let operNum=0;
  60. for(let i=0;i<this.lockArr.length;i++){
  61. let itemRow=this.lockArr[i].row
  62. let itemCol=this.lockArr[i].col
  63. if(checkItemDic[itemRow+","+itemCol])
  64. continue
  65. checkItemDic[itemRow+","+itemCol]=true
  66. for(let m=-1;m<=1;m++){
  67. for(let n=-1;n<=1;n++){
  68. // console.log("otherItem=============================>"+m+","+n)
  69. // operNum++;
  70. // if(operNum>=1000)
  71. // {
  72. // console.log("异常退出")
  73. // return;
  74. // }
  75. //十字型检测
  76. if((!((m==0) && (n==0))) && this.inArea(itemRow+m,itemCol+n) && ((itemRow+m==itemRow) || (itemCol+n==itemCol))){
  77. let otherItem=this.itemDic[(itemRow+m)+","+(itemCol+n)]
  78. if(otherItem)
  79. {
  80. // console.log("otherItem=>"+otherItem.numType+","+numType+","+itemRow+","+itemCol+","+(itemRow+m)+","+(itemCol+n))
  81. // console.log("otherItem=>"+otherItem.row)
  82. // console.log("otherItem=>"+otherItem.col)
  83. // console.log("otherItem=============================>")
  84. if((!otherItem.isLock) && (otherItem.numType==numType)){
  85. console.log("otherItem=============================>颜色相同")
  86. this.lockArr.push(otherItem)
  87. // otherItem.changeType(numType)
  88. otherItem.isLock=true
  89. count++
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. if(count>0){
  97. for(let i=0;i<this.lockArr.length;i++){
  98. this.lockArr[i].changeType(numType)
  99. }
  100. }
  101. console.log("更换颜色==>>"+count+","+this.lockArr.length)
  102. if(this.lockArr.length==12*13){
  103. //胜利
  104. GameManager.getInstance().addScore(100)
  105. this.audioSource.playOneShot(this.otherAudioArr[0],1)
  106. }
  107. }
  108. inArea( x, y){
  109. let isIn= x >= 0 && x < this.row && y >= 0 && y < this.col;
  110. // if(!isIn)
  111. // {
  112. // console.log("inArea==>>"+x+","+y)
  113. // }
  114. return isIn
  115. }
  116. update(deltaTime: number) {
  117. }
  118. }