| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- import Fruit from "./Fruit";
- const {ccclass, property} = cc._decorator;
- @ccclass
- export default class GroupController extends cc.Component {
- @property({type:cc.Node})
- buildingGroup:cc.Node = null;
- @property({type:cc.Node})
- destinationGroup:cc.Node = null;
- @property({type:cc.Node})
- pipeGroup:cc.Node = null;
- @property({type:cc.Node})
- fruitGroup:cc.Node = null;
- @property({type:cc.Node})
- groundGroup:cc.Node = null;
- @property({type:cc.Node})
- cloudGroup:cc.Node = null;
- @property({type:cc.Prefab})
- pipePrefab:cc.Prefab = null;
- @property({type:cc.Prefab})
- fruitPrefab:cc.Prefab = null;
- modeInfo:ModeInfo;
- pipeInfos:PipeInfo[] = [];
- fruitInfos:FruitInfo[] = [];
- fruitNodes:cc.Node[] = [];
- fruitEaten:boolean[] = [];
- onLoad():void{
- window.gc = this;
- this.buildingGroup.setPosition(-cc.winSize.width/2,-cc.winSize.height/2+158);
- this.pipeGroup.setPosition(0,-cc.winSize.height/2+158);
- this.fruitGroup.setPosition(0,-cc.winSize.height/2+158);
- this.groundGroup.setPosition(-cc.winSize.width/2,-cc.winSize.height/2);
- this.cloudGroup.setPosition(-cc.winSize.width/2,cc.winSize.height/2);
- }
- startMode(){
- for(let i in this.modeInfo.pipeInfos){
- this.pipeInfos[i] = this.modeInfo.pipeInfos[i];
- }
- for(let i in this.modeInfo.fruitInfos){
- let fruitInfo = this.modeInfo.fruitInfos[i];
- this.fruitInfos[i] = fruitInfo;
- let fruitNode = cc.instantiate(this.fruitPrefab);
- fruitNode.getComponent(Fruit).init(fruitInfo.index,fruitInfo.type);
- fruitNode.setPosition(fruitInfo.x,fruitInfo.y);
- this.fruitNodes[i] = fruitNode;
- }
- this.destinationGroup.setPosition(this.modeInfo.destination,-cc.winSize.height/2+158);
- }
- update(){
- this.fillAndDestroyPipe();
- this.fillAndDestroyFruit();
- this.fillAndDestroy(this.buildingGroup);
- this.fillAndDestroy(this.groundGroup);
- this.fillAndDestroy(this.cloudGroup);
- }
- fillAndDestroyPipe(){
- if (this.pipeInfos.length > 0) {
- if (cc.winSize.width/2 + this.pipeInfos[0].gapX < this.getBorderRightX() + 300) {
- let pipeInfo = this.pipeInfos.shift();
- let newChild = cc.instantiate(this.pipePrefab);
- newChild.setPosition(pipeInfo.gapX,pipeInfo.gapY);
- newChild.getChildByName('PipeTop').y = pipeInfo.gapSize;
- this.pipeGroup.addChild(newChild);
- }
- }
- if (this.pipeGroup.children.length > 0) {
- if (this.getFirstNodeRightX(this.pipeGroup) < this.getBorderLeftX()) {
- this.pipeGroup.children[0].destroy();
- }
- }
- }
- fillAndDestroyFruit(){
- if (this.fruitInfos.length > 0) {
- if (cc.winSize.width/2 + this.fruitInfos[0].x < this.getBorderRightX() + 300) {
- let fruitInfo = this.fruitInfos.shift();
- let fruitNode = this.fruitNodes[fruitInfo.index];
- if (!this.fruitEaten[fruitInfo.index]) {
- this.fruitGroup.addChild(fruitNode);
- }
- }
- }
- if (this.fruitGroup.children.length > 0) {
- if (this.getFirstNodeRightX(this.fruitGroup) < this.getBorderLeftX()) {
- this.fruitGroup.children[0].destroy();
- }
- }
- }
- fillAndDestroy(group:cc.Node){
- if (this.getEndNodeLeftX(group) < this.getBorderRightX()) {
- let endChild = group.children[group.childrenCount-1];
- let newChild = cc.instantiate(endChild);
- let newOffsetX = endChild.width;
- newChild.setPosition(endChild.x+newOffsetX,endChild.y);
- group.addChild(newChild);
- }
-
- if (this.getFirstNodeRightX(group) < this.getBorderLeftX()) {
- group.children[0].destroy();
- }
- }
- getBorderLeftX(){
- return window.cameraNode.parent.convertToWorldSpaceAR(
- window.cameraNode.position.add(
- cc.v2(-cc.winSize.width/2, 0)
- )
- ).x;
- }
- getBorderRightX(){
- return window.cameraNode.parent.convertToWorldSpaceAR(
- window.cameraNode.position.add(
- cc.v2(cc.winSize.width/2,0)
- )
- ).x;
- }
- getFirstNodeRightX(group:cc.Node){
- return group.convertToWorldSpaceAR(
- group.children[0].position.add(cc.v2(group.children[0].width,0))
- ).x;
- }
- getEndNodeLeftX(group:cc.Node){
- return group.convertToWorldSpaceAR(
- group.children[group.childrenCount-1].position
- ).x;
- }
- /**
- * return
- * 1:bird above of the center of pipe's center
- * -1:bird below of the center of pipe's center
- * 0:bird horizontal of the center of pipe's center
- */
- getBirdRelativeNextPipeStatus(birdNode:cc.Node):number{
- let birdLocation = birdNode.convertToWorldSpaceAR(cc.v2(0,0));
- for(let i in this.pipeGroup.children){
- let pipe = this.pipeGroup.children[i];
- let pipeLocation = pipe.convertToWorldSpaceAR(cc.v2(0,0));
- let birdLeftX = birdLocation.x - birdNode.width/2;
- let pipeRightX = pipeLocation.x + pipe.width/2;
- if(pipeRightX>birdLeftX){
- let centerGapLocationY = pipeLocation.y + pipe.getChildByName('PipeTop').y/2;
- if(birdLocation.y>centerGapLocationY){
- return 1;
- }
- if(birdLocation.y<centerGapLocationY){
- return -1;
- }
- return 0;
- }
- }
- //when forward without pipe,compare to the center point's y of parent
- if(birdNode.y>0){
- return 1;
- }
- if(birdNode.y<0){
- return -1;
- }
- return 0;
- }
- }
|