// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import GameData from "./GameData";
import GameManager from "./GameManager";
import AudioManager from "./tool/AudioManager";
import { Notification } from './tool/Notification';
const {ccclass, property} = cc._decorator;0

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Node)
    add: cc.Node = null;
    @property(cc.SpriteAtlas)
    plist: cc.SpriteAtlas =null;

    basicHeight:number  //每次跳跃前基础高度
    jumpState:number;   //跳跃状态，控制1段跳2段跳
    jumpHeight:number;  //跳跃高度
    jumpAction: cc.Tween<cc.Node>;//跳跃tween动画，用于停止控制
    up:boolean;         //跳跃上升下降状态判断
    block: cc.Node;
    death:boolean;
    interfere:boolean;
    double:boolean;
    jumpPause:boolean;
    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start () {
        this.init();
    }

    init(){
        this.jumpState = 0;
        this.jumpHeight = this.node.y;
        this.basicHeight = this.node.y;
        this.up = true;
        this.double = false;
        this.death = false;
        this.jumpAction = null;
        this.block = null;
        this.interfere = false;
        this.jumpPause = true;
    }

    //跳动
    jump(){
  
        if(this.jumpState < 2){
            if(this.jumpState == 0){
                if(this.death == false){
                    this.node.getChildByName("sp").getComponent(sp.Skeleton).setAnimation(1,"jump",false);
                }
            }
            else if(this.jumpState == 1 || this.node.y > (this.jumpHeight+GameData._instance.GM_INFO.jumpHeight-5)){
                if(this.death == false){
                    if(GameManager._instance.guide >0 && GameManager._instance.guide <3){
                        return;
                    }
                    this.node.getChildByName("sp").getComponent(sp.Skeleton).setAnimation(1,"jump",false);
                }
              
            }
            setTimeout(() => {
                if(this.jumpState == 0){
                    if(this.jumpPause == true){
                        return;
                    }

                    if(this.block){
                        if(this.block.name != "block13"){
                            this.block.getComponent("Block").hide();
                        }
                        else{this.block.active = false;
                            this.jumpPause = true;
                        } 
                        this.block = null;
                    }
                    if(GameManager._instance.guide == 1){
                        GameManager._instance.guide = 2;
                        GameManager._instance.startGuide(2,false);
                    }

                    GameData._instance.CLICK_DATA.jumps = 1;
                    AudioManager._instance.playJump();
                    // this.basicHeight = this.node.y;
                    this.jumpState += 1;
                    this.up = true;
                    Notification.emit("showGround",this.node.y);
                    this.jumpAction = cc.tween(this.node)
                    .to(GameData._instance.GM_INFO.jumpUpTime,{position:cc.v3(0,this.jumpHeight+GameData._instance.GM_INFO.jumpHeight,0)},{easing:'quadOut'})
                    .call(() =>{
                        this.up = false;
                        this.jumpState = -1;
                        var temp = this.node.y - this.jumpHeight;
                    })
                    .to(GameData._instance.GM_INFO.jumpDownTime,{position:cc.v3(0,this.jumpHeight,0)},{easing:'cubicIn'})
                    .call(() =>{
                        this.jumpHeight = this.node.y;
                        this.jumpState = -1;
                        this.up = true;
                    })
                    .start();
                }
                else if(this.jumpState == 1 || this.node.y > (this.jumpHeight+GameData._instance.GM_INFO.jumpHeight-15)){
                    if(GameManager._instance.guide >0 && GameManager._instance.guide <3){
                        return;
                    }
                    if(GameManager._instance.guide == 6){
                        this.jumpPause = true;
                        GameManager._instance.startGuide(6,false);
                    }
                    if(this.jumpAction)this.jumpAction.stop();
                    AudioManager._instance.playJump();
                    GameData._instance.CLICK_DATA.jumps = 2;
                    this.jumpState = 2;
                    this.up = true;
                    this.jumpAction = cc.tween(this.node)
                    .to(GameData._instance.GM_INFO.jumpUpTime,{position:cc.v3(0,this.node.y+GameData._instance.GM_INFO.jumpHeight,0)},{easing:'quadOut'})
                    .call(() =>{
                        this.up = false;
                    })
                    .to(GameData._instance.GM_INFO.jumpDownTime*1.5,{position:cc.v3(0,this.jumpHeight,0)},{easing:'cubicIn'})
                    .call(() =>{
                        this.jumpState = -1;
                        this.jumpHeight = this.node.y;
                        this.up = true;
                    })
                    .start();
                }
            }, 100);

        }
    }

    onCollisionEnter(other: cc.Collider, self: cc.Collider) {
        if(GameManager._instance){
            if(GameManager._instance.over == true){
                return;
            }
        }

        let max = other.name.length;
        let num = 6;
        if(other.node.name != "Ground"){
            if(GameManager._instance.guide == -1){
                if(other.node.getComponent("Block").num > 9) num = 7; 
            }
            else{
                num = 7;
            }
        }
      
        let ColliderName = other.name.substring(num,max);
        //获取碰撞体名字
        if(ColliderName == "<PolygonCollider>"){
            if(this.node.y <= other.node.y && this.up == true && this.death == false){
                if(this.jumpAction)this.jumpAction.stop();
                this.jumpState = -1;
                AudioManager._instance.playGround();
                console.log("死亡1");
                this.gameDeath();
            }
            else if(other.node.name == "Ground" && this.death == false){
                // if(this.jumpAction)this.jumpAction.stop();
                this.jumpState = -1;
                console.log("死亡2");
                this.gameDeath();
            }
            else{
                // console.log("没碰到边缘",this.up,this.node.y,other.node.y,this.jumpState,this.up);
            }
        }
        if(ColliderName == "<BoxCollider>" ){
            if(this.death == true){
                console.log("已经死亡");
                return;
            }
            //
            let portrait = Math.abs(this.node.y-other.node.y)
            let width = other.node.width/2;
            let pos = Math.abs(this.node.x - other.node.x);
            let distance =pos/width;
            // console.log("我的位置:",this.node.x,"块的位置:",other.node.x,"块的宽度:",width,"百分比:",distance);

            if(this.up == true){
                console.log("死亡4");
                AudioManager._instance.playGround();
                this.gameDeath();
            }
            else{
                if(distance*100 < GameData._instance.GM_INFO.distanceMin && this.jumpState != 0){
                    // console.log("成功1");
                    // console.log("百分比:",distance*100+"%");
                    this.success(other,distance*100);
                }
                else{
                    if(portrait >= 20 || distance*100 > GameData._instance.GM_INFO.distanceMax){
                        console.log("死亡3","落差:",portrait,"状态:",this.up);
                        AudioManager._instance.playGround();
                        this.gameDeath();
                    }
                    else{
                        // console.log("成功2");
                        console.log("百分比:",distance*100+"%");
                        this.success(other,distance*100);
                    }
                }
            }
        }
    }

    success(other,distance){
        this.jumpPause = true;
        if(GameManager._instance.guide != -1){
            this.basicHeight = this.node.y;
            other.node.stopAllActions();
            if(GameManager._instance.guide == 3 || GameManager._instance.guide==4){
                GameManager._instance.startGuide(4,false);
                if(GameManager._instance.guide == 4){
                    GameManager._instance.guide = 5;
                }
            }
            else if(GameManager._instance.guide == 5){
                this.jumpPause = true;
                let tip = GameManager._instance.Tip.getChildByName("good");
                cc.tween(tip)
                .to(0.3,{opacity:255,scale:1})
                .delay(0.5)
                .to(0.2,{opacity:0})
                .call(() =>{
                    GameManager._instance.startGuide(5,false);
                })
                .start();
            
            }
            else if(GameManager._instance.guide == 7){
                GameManager._instance.startGuide(8,false);
            }
            else if(GameManager._instance.guide == 8){
                this.jumpPause = true;
                let tip = GameManager._instance.Tip.getChildByName("perfet");
                cc.tween(tip)
                .to(0.3,{opacity:255,scale:1})
                .delay(0.5)
                .to(0.2,{opacity:0})
                .call(() =>{
                    GameManager._instance.startGuide(9,false);
                })
                .start();
           
            }
        }
        let percent = parseInt(distance*10+"")/10;
        GameData._instance.CLICK_DATA.percent = percent;
        if(GameManager._instance)GameManager._instance.destroyBlock(true);
        this.add.active = true;
        this.add.opacity = 0;
        let number = 1;
        if(distance <= 10) number = 3;
        else if(distance <= 20) number = 2;
        GameData._instance.CLICK_DATA.accuracy = number;
    
        this.add.getChildByName("icon").getComponent(cc.Sprite).spriteFrame = this.plist.getSpriteFrames()[number];
        this.add.getChildByName("jia").getComponent(cc.Sprite).spriteFrame = this.plist.getSpriteFrames()[number+3];
        this.addAction();
        this.node.getChildByName("guang").active = true;
        this.node.getChildByName("guang").getComponent(sp.Skeleton).setAnimation(0,"double_1",false);
        if(this.jumpAction)this.jumpAction.stop();
        if(this.death == false){
            this.node.getChildByName("sp").getComponent(sp.Skeleton).setAnimation(1,"jump",false);
        }

        this.basicHeight = this.node.y;
        if(number > 1){
            AudioManager._instance.playPz();
            if(this.double == false){
                this.double = true;
                GameData._instance.LEVEL_INFO.doubleHit = 1.05;
            }
            else{
                GameData._instance.LEVEL_INFO.doubleHit += GameData._instance.LEVEL_INFO.doubleAdd;
            }
            var combo = (GameData._instance.LEVEL_INFO.doubleHit-1)*(1/0.05);
            GameData._instance.CLICK_DATA.combo = parseInt(combo + "");
        }
        else{
            AudioManager._instance.playLuo();
            this.double = false;
            GameData._instance.CLICK_DATA.combo = 0;
        }
        this.block = other.node;
        if(this.block.name != "block13"){
            this.block.getComponent("Block").setScore(number);
            Notification.emit("setData",null);
        }

        setTimeout(() => {
            if(GameManager._instance.guide == -1){
                Notification.emit("createBlock",null);
                if(GameData._instance.LEVEL_INFO.doubleSuccess == 0){
                    setTimeout(() => {
                        Notification.emit("createCrackBlock",null);
                    }, 100);
                }
            }
                
        }, 10);
  
        if(number > 1 && GameData._instance.LEVEL_INFO.doubleSuccess != 0 ){
            GameData._instance.LEVEL_INFO.doubleSuccess -= 1;
        }
        else{
            if(GameData._instance.LEVEL_INFO.doubleSuccess != 0 && GameData._instance.LEVEL_INFO.doubleSuccess<3){
                GameData._instance.LEVEL_INFO.doubleSuccess = 3;
            }
        }
        if(GameData._instance.LEVEL_INFO.doubleSuccess == 0){

        }
        else{
            GameManager._instance.interfere = false;
            GameData._instance.CLICK_DATA.fake = GameManager._instance.interfere;
        }
        this.jumpState = 0;
        this.up = true;
        this.node.y = other.node.y-3+other.node.height/2;
        this.jumpHeight = this.node.y;
    }

    gameDeath(){
        // console.log("条失败");
        this.jumpPause = true;
        if(!GameData._instance.GM_INFO.probation){
            GameData._instance.CLICK_DATA.combo = 0;
            GameData._instance.CLICK_DATA.accuracy = 0;
            GameData._instance.CLICK_DATA.percent = -1;
            Notification.emit("setData",null);
            this.double = false;
            GameData._instance.LEVEL_INFO.doubleSuccess = 3;
            this.death = true;
            if(this.jumpAction)this.jumpAction.stop();
            this.jumpState = -1;
            Notification.emit("death",false);
        }
        else{
            this.death = true;
            if(GameManager._instance.guide == 6){
                GameManager._instance.tipHide("tip4",null);
            }
            if(this.jumpAction)this.jumpAction.stop();
            this.jumpState = -1;
            Notification.emit("death",true);
        }
    }

    xinAction(){
        if(GameData._instance.GM_INFO.life >= 0){
            let xin = this.node.getChildByName("xin");
            xin.y = 120;
            cc.tween(xin)
            .to(0.2,{opacity:255,y:xin.y+50})
            .delay(0.5)
            .to(0.3,{opacity:0,y:xin.y+100})
            .call(() =>{
               
            })
            .start();
        }
    }

    addAction(){
        this.add.y = 120;
        cc.tween(this.add)
        .to(0.2,{opacity:255,y:this.add.y+50})
        .delay(0.5)
        .to(0.3,{opacity:0,y:this.add.y+100})
        .call(() =>{
           this.add.active = false;
        })
        .start();
    }

    update (dt) {
        // if(this.death == true && this.node.y > -500 && GameManager._instance.over == false){
        //     // this.node.y -= 3;
        //     // if(this.node.y <= -500){
        //     //     this.node.y = -500;
        //     // } 
        // }
    }
}
