import EventType from "./EventType"; import Fruit from "./Fruit"; const {ccclass, property} = cc._decorator; @ccclass export default class Bird extends cc.Component { //sprite frames @property({type:cc.SpriteFrame}) spFlyingFrames1 = []; @property({type:cc.SpriteFrame}) spFlyingFrames2 = []; @property({type:cc.SpriteFrame}) arrowBlue = null; @property({type:cc.SpriteFrame}) arrowPink = null; //audios @property({type:cc.AudioClip}) wing = null; body:cc.Node; rigidBody:cc.RigidBody; scream:cc.Node; index:number; ai:boolean; arrow:cc.Node; spFlyingFrames:cc.SpriteFrame[]; spBird:cc.Sprite; currentFlyingFrame: number = 0; currentAcc: number = 0; running:boolean = false; onGroundCount:number = 0; bouncing:boolean = false; withAttack:boolean = false; speed:number = 240; basicSpeed:number = 240; quickenSpeed:number = 1500; //quicken flag quickTimeCount:number = 0; quicking:boolean = false; quickenArmature:dragonBones.ArmatureDisplay; upDownSpeedScale: number = 1; aiAccuracy: number = 1; init(index:number) { this.index = index; if (this.index == 0) { this.spFlyingFrames = this.spFlyingFrames1; this.node.setPosition(0,80); } else { this.spFlyingFrames = this.spFlyingFrames2; this.node.setPosition(0,-160); } } isSelf():boolean { return this.index == window.myPlayerInfo.index; } onLoad() { this.body = this.node.getChildByName('Body'); this.rigidBody = this.node.getComponent(cc.RigidBody); this.scream = this.body.getChildByName('Scream'); this.spBird = this.body.getComponent(cc.Sprite); this.spBird.spriteFrame = this.spFlyingFrames[0]; this.quickenArmature = this.body.getChildByName('Quicken').getComponent(dragonBones.ArmatureDisplay); this.quickenArmature.node.opacity = 0; this.upDownSpeedScale = window.appGame.device ? 0.5 : 1; this.aiAccuracy = window.appGame.device ? 0.7 : 1; if(this.isSelf()){ let arrow = new cc.Node(); if(window.myPlayerInfo.gender==2){ arrow.addComponent(cc.Sprite).spriteFrame = this.arrowPink; }else{ arrow.addComponent(cc.Sprite).spriteFrame = this.arrowBlue; } arrow.setAnchorPoint(0,0.5); arrow.scale = this.node.scale; arrow.angle = 90; this.node.parent.addChild(arrow); this.arrow = arrow; } this.schedule(this.updateFlyingFrame, 0.1); } update(dt:number) { if (this.arrow) { this.arrow.setPosition(this.node.x + 40 * this.node.scaleX, this.node.y + 125 * this.node.scaleY); } if (this.running) { this.rigidBody.gravityScale = 4 * this.upDownSpeedScale; if (this.onGroundCount == 0) { this.currentAcc -= dt * 300 * this.upDownSpeedScale; let nextAngle = this.currentAcc/3; if (nextAngle > -45) { this.body.angle = nextAngle; } } if (this.body.angle < 25) { this.withAttack = false; } if (this.quicking) { this.quickTimeCount += dt; if (this.quickTimeCount > 0.5) { this.quitQuicken(); } } } else { this.withAttack = false; this.rigidBody.gravityScale = 0; this.rigidBody.linearVelocity = cc.v2(0,0); } if (this.onGroundCount > 0) { this.scream.active = true; } else { this.scream.active = false; } if (this.ai) { if (this.node.x < -(window.cameraNode.x + cc.winSize.width / 2 + 120)) { this.node.y = 0; } else if (this.node.x > (window.cameraNode.x + cc.winSize.width / 2 + 120)) { this.node.y = 0; this.rigidBody.linearVelocity = cc.v2(this.aiAccuracy * this.basicSpeed, 0); } } } flyUp(hitPower: number = 30) { if (hitPower > 45) { hitPower = 45; } if(!window.gm.isGameOver){ this.running = true; } if(!this.running || this.bouncing) { return; } this.withAttack = true; this.currentAcc = 130; this.rigidBody.linearVelocity = cc.v2(this.speed, 300 + 300 * this.upDownSpeedScale * hitPower / 30); if (this.isSelf()) { cc.audioEngine.playEffect(this.wing, false); } } updateFlyingFrame() { if (!this.running || this.onGroundCount > 0) { return; } this.currentFlyingFrame++; if (this.currentFlyingFrame == this.spFlyingFrames.length) { this.currentFlyingFrame = 0; } this.spBird.spriteFrame = this.spFlyingFrames[this.currentFlyingFrame]; } run() { this.running = true; } stop() { this.running = false; } quicken(strength:number) { this.quicking = true; this.quickTimeCount = 0; this.speed = this.basicSpeed + this.quickenSpeed * strength / (6 - strength); this.rigidBody.linearVelocity = cc.v2(this.speed, this.rigidBody.linearVelocity.y); this.quickenArmature.node.opacity = 255; this.quickenArmature.playAnimation('jiasu' + strength, 1); } quitQuicken() { this.quicking = false; this.quickTimeCount = 0; this.speed = this.basicSpeed; this.rigidBody.linearVelocity = cc.v2(this.speed, this.rigidBody.linearVelocity.y); this.quickenArmature.node.opacity = 0; } onCollisionEnter(otherCollider:cc.Collider){ if(otherCollider.node.group==EventType.GROUP_BLOCK){ if(otherCollider.tag==0||otherCollider.tag==1){ this.onGroundCount++; this.currentAcc = 0; this.body.stopAllActions(); this.body.runAction(cc.rotateTo(0.1,0).easing(cc.easeIn(1.5))); } else if(otherCollider.tag==2){ if (this.isSelf()) { window.player.call('reachDestination',[this.index]); } else if (this.ai) { window.player.call('reachDestination',[this.index]); } } }else if(otherCollider.node.group==EventType.GROUP_BIRD){ let otherBird = otherCollider.node.getComponent(Bird); if(!this.bouncing&&!otherBird.bouncing){ //1.if under of other and with attack, can bounce other //2.if above of other and other with none attack, can bounce other if(this.withAttack&&this.node.yotherBird.node.y){ otherBird.bouncing = true; otherBird.rigidBody.linearVelocity = cc.v2(otherBird.rigidBody.linearVelocity.x, otherBird.rigidBody.linearVelocity.y + this.node.y{ otherBird.bouncing = false; },0.3); } } }else if(otherCollider.node.group==EventType.GROUP_FRUIT){ if(this.isSelf()){ window.player.call('eatFruit',[otherCollider.node.getComponent(Fruit).index]); }else if(this.ai){ window.player.call('aiEatFruit',[otherCollider.node.getComponent(Fruit).index]); } } } onCollisionExit(otherCollider:cc.Collider){ if(otherCollider.node.group==EventType.GROUP_BLOCK){ if(otherCollider.tag==0||otherCollider.tag==1){ this.onGroundCount--; this.body.stopAllActions(); } } } setAI() { this.ai = true; this.flyUp(); this.schedule(()=>{ if(!window.gm.isGameOver){ if (window.appGame.device) { if (window.gc.getBirdRelativeNextPipeStatus(this.node) < 0) { if (Math.random() < this.aiAccuracy) { this.flyUp(); } else { // no do } } else { if (Math.random() < this.aiAccuracy) { // no do } else { this.flyUp(); } } } else { if(window.gc.getBirdRelativeNextPipeStatus(this.node)<0){ this.flyUp(); } } } },0.3); } }