GroupController.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import Fruit from "./Fruit";
  2. const {ccclass, property} = cc._decorator;
  3. @ccclass
  4. export default class GroupController extends cc.Component {
  5. @property({type:cc.Node})
  6. buildingGroup:cc.Node = null;
  7. @property({type:cc.Node})
  8. destinationGroup:cc.Node = null;
  9. @property({type:cc.Node})
  10. pipeGroup:cc.Node = null;
  11. @property({type:cc.Node})
  12. fruitGroup:cc.Node = null;
  13. @property({type:cc.Node})
  14. groundGroup:cc.Node = null;
  15. @property({type:cc.Node})
  16. cloudGroup:cc.Node = null;
  17. @property({type:cc.Prefab})
  18. pipePrefab:cc.Prefab = null;
  19. @property({type:cc.Prefab})
  20. fruitPrefab:cc.Prefab = null;
  21. modeInfo:ModeInfo;
  22. pipeInfos:PipeInfo[] = [];
  23. fruitInfos:FruitInfo[] = [];
  24. fruitNodes:cc.Node[] = [];
  25. fruitEaten:boolean[] = [];
  26. onLoad():void{
  27. window.gc = this;
  28. this.buildingGroup.setPosition(-cc.winSize.width/2,-cc.winSize.height/2+158);
  29. this.pipeGroup.setPosition(0,-cc.winSize.height/2+158);
  30. this.fruitGroup.setPosition(0,-cc.winSize.height/2+158);
  31. this.groundGroup.setPosition(-cc.winSize.width/2,-cc.winSize.height/2);
  32. this.cloudGroup.setPosition(-cc.winSize.width/2,cc.winSize.height/2);
  33. }
  34. startMode(){
  35. for(let i in this.modeInfo.pipeInfos){
  36. this.pipeInfos[i] = this.modeInfo.pipeInfos[i];
  37. }
  38. for(let i in this.modeInfo.fruitInfos){
  39. let fruitInfo = this.modeInfo.fruitInfos[i];
  40. this.fruitInfos[i] = fruitInfo;
  41. let fruitNode = cc.instantiate(this.fruitPrefab);
  42. fruitNode.getComponent(Fruit).init(fruitInfo.index,fruitInfo.type);
  43. fruitNode.setPosition(fruitInfo.x,fruitInfo.y);
  44. this.fruitNodes[i] = fruitNode;
  45. }
  46. this.destinationGroup.setPosition(this.modeInfo.destination,-cc.winSize.height/2+158);
  47. }
  48. update(){
  49. this.fillAndDestroyPipe();
  50. this.fillAndDestroyFruit();
  51. this.fillAndDestroy(this.buildingGroup);
  52. this.fillAndDestroy(this.groundGroup);
  53. this.fillAndDestroy(this.cloudGroup);
  54. }
  55. fillAndDestroyPipe(){
  56. if (this.pipeInfos.length > 0) {
  57. if (cc.winSize.width/2 + this.pipeInfos[0].gapX < this.getBorderRightX() + 300) {
  58. let pipeInfo = this.pipeInfos.shift();
  59. let newChild = cc.instantiate(this.pipePrefab);
  60. newChild.setPosition(pipeInfo.gapX,pipeInfo.gapY);
  61. newChild.getChildByName('PipeTop').y = pipeInfo.gapSize;
  62. this.pipeGroup.addChild(newChild);
  63. }
  64. }
  65. if (this.pipeGroup.children.length > 0) {
  66. if (this.getFirstNodeRightX(this.pipeGroup) < this.getBorderLeftX()) {
  67. this.pipeGroup.children[0].destroy();
  68. }
  69. }
  70. }
  71. fillAndDestroyFruit(){
  72. if (this.fruitInfos.length > 0) {
  73. if (cc.winSize.width/2 + this.fruitInfos[0].x < this.getBorderRightX() + 300) {
  74. let fruitInfo = this.fruitInfos.shift();
  75. let fruitNode = this.fruitNodes[fruitInfo.index];
  76. if (!this.fruitEaten[fruitInfo.index]) {
  77. this.fruitGroup.addChild(fruitNode);
  78. }
  79. }
  80. }
  81. if (this.fruitGroup.children.length > 0) {
  82. if (this.getFirstNodeRightX(this.fruitGroup) < this.getBorderLeftX()) {
  83. this.fruitGroup.children[0].destroy();
  84. }
  85. }
  86. }
  87. fillAndDestroy(group:cc.Node){
  88. if (this.getEndNodeLeftX(group) < this.getBorderRightX()) {
  89. let endChild = group.children[group.childrenCount-1];
  90. let newChild = cc.instantiate(endChild);
  91. let newOffsetX = endChild.width;
  92. newChild.setPosition(endChild.x+newOffsetX,endChild.y);
  93. group.addChild(newChild);
  94. }
  95. if (this.getFirstNodeRightX(group) < this.getBorderLeftX()) {
  96. group.children[0].destroy();
  97. }
  98. }
  99. getBorderLeftX(){
  100. return window.cameraNode.parent.convertToWorldSpaceAR(
  101. window.cameraNode.position.add(
  102. cc.v2(-cc.winSize.width/2, 0)
  103. )
  104. ).x;
  105. }
  106. getBorderRightX(){
  107. return window.cameraNode.parent.convertToWorldSpaceAR(
  108. window.cameraNode.position.add(
  109. cc.v2(cc.winSize.width/2,0)
  110. )
  111. ).x;
  112. }
  113. getFirstNodeRightX(group:cc.Node){
  114. return group.convertToWorldSpaceAR(
  115. group.children[0].position.add(cc.v2(group.children[0].width,0))
  116. ).x;
  117. }
  118. getEndNodeLeftX(group:cc.Node){
  119. return group.convertToWorldSpaceAR(
  120. group.children[group.childrenCount-1].position
  121. ).x;
  122. }
  123. /**
  124. * return
  125. * 1:bird above of the center of pipe's center
  126. * -1:bird below of the center of pipe's center
  127. * 0:bird horizontal of the center of pipe's center
  128. */
  129. getBirdRelativeNextPipeStatus(birdNode:cc.Node):number{
  130. let birdLocation = birdNode.convertToWorldSpaceAR(cc.v2(0,0));
  131. for(let i in this.pipeGroup.children){
  132. let pipe = this.pipeGroup.children[i];
  133. let pipeLocation = pipe.convertToWorldSpaceAR(cc.v2(0,0));
  134. let birdLeftX = birdLocation.x - birdNode.width/2;
  135. let pipeRightX = pipeLocation.x + pipe.width/2;
  136. if(pipeRightX>birdLeftX){
  137. let centerGapLocationY = pipeLocation.y + pipe.getChildByName('PipeTop').y/2;
  138. if(birdLocation.y>centerGapLocationY){
  139. return 1;
  140. }
  141. if(birdLocation.y<centerGapLocationY){
  142. return -1;
  143. }
  144. return 0;
  145. }
  146. }
  147. //when forward without pipe,compare to the center point's y of parent
  148. if(birdNode.y>0){
  149. return 1;
  150. }
  151. if(birdNode.y<0){
  152. return -1;
  153. }
  154. return 0;
  155. }
  156. }