import Animator = Laya.Animator;
import Sprite3D = Laya.Sprite3D;
import Vector3 = Laya.Vector3;
import { GameManager, GameState } from "./GameManager";
import { ScenceManager } from "./ScenceManager";
import { GameUtils } from "../../GameUtils/GameUtils";
import { EventManager, EventType } from "./EventManager";
import PoolManager from "./PoolManager";
import { LevelManager } from "./LevelManager";
import { InGameWindow } from "../../Window/InGameWindow";
import { ResourcesManager, effecttype } from "./ResourcesManager";
import { Rigbody } from "./Rigbody";
import { Stage, Level } from "./LevelData";
import PlaySoundManager, { SoundType } from "./PlaySoundManager";
import WxApi from "../../WXSDK/wxsdk";
import { ThingsEffect } from "../Network/PlayerAccount";
import { Player } from "./Player";
import { Queue } from "./Queue";
import CameraManager from "./CameraManager";

export enum ColliderLay
{
    Play = Laya.Physics3DUtils.COLLISIONFILTERGROUP_CUSTOMFILTER1,Npc = Laya.Physics3DUtils.COLLISIONFILTERGROUP_CUSTOMFILTER2,Plane = Laya.Physics3DUtils.COLLISIONFILTERGROUP_CUSTOMFILTER10,
    things = Laya.Physics3DUtils.COLLISIONFILTERGROUP_CUSTOMFILTER3,dieplane = Laya.Physics3DUtils.COLLISIONFILTERGROUP_CUSTOMFILTER5
}

export class Runner extends Laya.Script3D
{
    public Ai = false;
    public aioffestx = 0;
    public aimiss = 0;
    public Id:number;
    public name:string;
    m_sprite:Sprite3D;
    m_model:Sprite3D;
    m_animator:Animator;
    m_speed:number = 1;
    m_flySprite:Sprite3D;
    m_effect:Sprite3D;
    m_head:Sprite3D;
    m_hat:Sprite3D;
    m_cb:Sprite3D;
    m_crown:Sprite3D;
    m_curstages:Stage[] = [];
    offest:Vector3 = new Vector3();
    //m_huo:Sprite3D;
    onAwake()
    {
        this.m_sprite = this.owner as Sprite3D;
        this.m_model = this.m_sprite.getChildAt(0) as Sprite3D;;
        this.m_animator = this.m_sprite.getChildAt(0).getComponent(Animator);
        this.m_flySprite = PoolManager.Instance.CreatMeshSprite3D(ResourcesManager.Instance.FalconMesh,"");
        this.m_sprite.addChild(this.m_flySprite);
        this.m_flySprite.transform.localPosition = new Vector3(0,0.95,0);
        this.m_flySprite.transform.localScale = new Vector3(0.36,0.36,0.36);
        this.m_flySprite.active = false;

        this.m_hat = this.m_model.getChildAt(0).getChildAt(0) as Sprite3D;
        this.m_cb = this.m_model.getChildAt(1).getChildAt(0) as Sprite3D;

        this.m_head = this.m_model.getChildAt(0) as Sprite3D;
        this.m_crown = this.m_crown?this.m_crown:PoolManager.Instance.CreatMeshSprite3D(ResourcesManager.Instance.crown,"");
        this.m_head.addChild(this.m_crown);
        this.m_crown.transform.localScale = new Vector3(13,13,13);
        this.m_crown.transform.localPosition = new Vector3(0,0,0);
        this.m_crown.transform.rotationEuler = new Vector3(0,0,0);
        this.m_crown.active = false;

        this.m_effect = this.m_sprite.getChildByName("effect") as Sprite3D;

        this.GetStages();

        EventManager.GameStateEventOn(GameState.Playing,this,this.OnEnterPlaying);
        EventManager.EventOn(EventType.crownchange,this,this.OnCrownChange);
    }

    onStart()
    {
        // var offesty = this.Ai?0.15:0;
        // this.m_crown.transform.localPosition = new Vector3(offesty,0,0);

        //this.GetCrown();
    }

    GetStages()
    {
        var curstages = LevelManager.Instance.curLevel.stages;
        var stages:Stage[] = [];
        for(var i = 0; i < curstages.length;i++)
        {
            var st = curstages[i];
            var news = new Stage(st.id.toString(),st.roadsdata);
            news.sprite = st.sprite;
            news.startz = st.startz;
            news.endz = st.endz;
            news.starty = st.starty;
            news.endy = st.endy;
            news.roadpointxs = st.roadpointxs;
            stages.push(news);
        }
        this.m_curstages = stages;
    }

    onDestroy()
    {
        EventManager.GameStateEventOff(GameState.Playing,this,this.OnEnterPlaying);
        EventManager.EventOff(EventType.crownchange,this,this.OnCrownChange);
    }

    OnEnterPlaying()
    {
        this.SetMove();
    }

    OnCrownChange(_id:number)
    {
        if(_id != this.Id)
        {
            this.CloseCrown();
        }
        else
        {
            this.GetCrown();
        }
    }

    SetMove()
    {
        this.Start();
        this.enteroutplane = false;
        this.fly = false;
        this.die = false;
        this.endroad = false;
        this.outlandtime = 0;
        //this.m_huo.active = true;
        Laya.timer.clearAll(this);
        Laya.timer.frameLoop(1,this,this.OnFrameLoop);
    }
    
    outlandtime = 0;
    OnLand = true;
    hit:Laya.HitResult = new Laya.HitResult();
    JS = false;
    curcollider:Laya.PhysicsComponent;
    OnFrameLoop()
    {
        if(this.canmove)
        {
            var startpoint = new Vector3(this.m_sprite.transform.position.x,this.m_sprite.transform.position.y + 1,this.m_sprite.transform.position.z);
            var dir = new Vector3(0,-1,0);
            var ray:Laya.Ray = new Laya.Ray(startpoint,dir);
            if(ScenceManager.Instance.GameScence.physicsSimulation.rayCast(ray,this.hit,20))
            {
                //this.CheckCurStage(this.hit.collider);

                if(this.hit.collider.collisionGroup != ColliderLay.Plane && this.hit.collider.collisionGroup != ColliderLay.things)return;
                if(this.OnLand)
                {
                    var dis = this.m_sprite.transform.position.y - this.hit.point.y;
                    if(this.hit.collider.owner.name == "JS")
                    {
                        this.JS = true;
                    }
                    if (dis > 0.01)
                    {
                        this.OnLand = false;
                        this.OnEnterOutPlane();
                        return;
                    }
                    else
                    {
                        if(this.hit.collider.owner.name == "Coin")
                        {
                            this.OnEnterPlane();
                            return;
                        }
                        this.m_sprite.transform.position = new Vector3(this.m_sprite.transform.position.x,this.hit.point.y,this.m_sprite.transform.position.z);
                    }
                }
                else
                {
                    var dis = this.m_sprite.transform.position.y - this.hit.point.y;
                    if (dis < 0.0001)
                    {
                        this.OnEnterPlane();
                    }
                }
            }
            else
            {
                this.OnLand = false;
                this.OnEnterOutPlane();
            }

            this.RayCastUpdate();
            
        }
    }

    delayMove = false;
    OnGetRoadPoint(_ps:number[])
    {
        this.nextpoint = _ps;

        this.TriggerAuto();

        this.oldpoint = _ps;
    }

    nextpoint = [];
    oldpoint = [];
    TriggerAuto(now = false)
    {
        if(this.nextpoint.length == 1)
        {
            if(this.oldpoint.length == 0 || this.oldpoint.length > 1)
            {
                if(this.aimiss <= Math.random())
                {
                    this.AutoMoveX(this.nextpoint[0]);
                }
            }
            if(this.oldpoint.length == 1)
            {
                this.delayMove = true;
            }
        }
        if(this.delayMove)
        {
            this.AutoMoveX(this.oldpoint[0]);
        }
    }

    LandTime = 0;
    enteroutplane = false;
    OnEnterOutPlane()
    {
        if(this.enteroutplane)return;

        //this.TriggerAuto(true);

        //this.m_tuowei.active = false;
        this.enteroutplane = true;
        if(!this.Ai)EventManager.EventTrigger(EventType.OutLand);
        //console.log("outplane");
        if(this.JS)
        {
            this.SetExtraSpeed(ThingsEffect.JSEffect,ThingsEffect.Jstime);
            this.JS = false;
        }
        if(!this.fly)
        {
            this.m_animator.play("land1");
        }
    }

    OnEnterPlane()
    {
        if(this.OnLand)return;
        if(this.hit.collider.collisionGroup == ColliderLay.things)
        {
            this.TriggerThings(this.hit.collider);
        }

        if(!this.Ai)
        {
            PlaySoundManager.Play(SoundType.land);
            WxApi.Shake();
        }

        //this.m_tuowei.active = true;

        if(!this.Ai)EventManager.EventTrigger(EventType.OnLand);
        this.enteroutplane = false;
        this.m_flySprite.active = false;
        //console.log("flyfalse");
        this.fly = false;
        this.flytime = 0;
        this.flycounttime = 0;
        this.m_animator.play("run");
        this.outlandtime = 0;
        this.LandTime = 0;
        //if(!this.Ai)EventManager.EventTrigger(EventType.Hover,this.LandTime);
        if(!this.Ai)EventManager.EventTrigger(EventType.flytime,this.flytime);
        this.OnLand = true;
        this.jump = false;
        var cp = new Vector3(this.m_sprite.transform.position.x,this.hit.point.y,this.m_sprite.transform.position.z);
        Vector3.lerp(this.m_sprite.transform.position,cp,0.3,this.curpos);
        this.m_sprite.transform.position = this.curpos;
    }

    Start()
    {
        this.JS = false;
        this.canmove = true;
        this.m_animator.play("run");
    }
    Pause()
    {
        this.canmove = false;
    }
    Stop()
    {
        this.Pause();
        Laya.timer.clearAll(this);
    }

    LandDown()
    {
        this.curspeed = 0;
    }

    fly = false;
    flytime = 0;
    flyspeed = 2;
    flycutrate = 0.01;
    flycounttime = 0;
    SetFly()
    {
        this.extraspeed = 0;
        this.extraspeedtime = 0;
        this.flytime = ThingsEffect.flytime;
        this.flyspeed = ThingsEffect.flyeffect;
        this.m_flySprite.active = true;
        this.fly = true;
        this.m_animator.play("fly");
        this.BackRote();
    }

    extracutrate = 0.01;
    extraspeed = 0;
    extraspeedtime = 0;
    SetExtraSpeed(_speed:number,time = 0)
    {
        if(!this.Ai)
        {
            PlaySoundManager.Play(SoundType.extraspeed);
            WxApi.Shake();
        }
        this.extraspeed = _speed;
        this.extraspeedtime += time;
        //this.m_huo.active = true;
    }

    JumpDuration = 0;
    jump = false;
    csp = 0;
    //跳跃
    public Jump(_csp:number)
    {
        this.csp = _csp*0.1;
        this.csp = Math.pow(this.csp,0.5);

        this.JumpHigth = 1;
        this.JumpDuration = 0;
        this.jump = true;
        this.OnLand = false;
    }
    //跳跃高度差
    public JumpHigth = 0;
    CurHight(csp = 1,dd:number,a = 3.5)
    {
        var hight = csp*dd - (dd*dd*a);
        if(hight < 0)
        {
            this.jump = false;
        }
        return hight;
    }

    bacoZ = 2;
    HitObstacle(_node:Laya.Node)
    {
        var target = _node as Sprite3D;
        this.curspeed *= 0.5;
        var curpos = this.m_sprite.transform.position;
        var offesxx = curpos.x - target.transform.position.x>=0?0.4:-0.4;
        this.m_sprite.transform.position = new Vector3(curpos.x+offesxx,curpos.y,curpos.z - this.bacoZ);
    }

    RayCastUpdate()
    {
        this.CheckForward();
        this.Gravity();
    }


    currotV = new Vector3();
    currot = new Laya.Quaternion;
    NormalizeSpeed:Vector3 = new Vector3();
    turndir:Vector3 = new Vector3(1,0,0);
    TurnDir(_dir:Vector3)
    {
        if(_dir.x != 0)
        {
            Vector3.lerp(this.currotV,_dir,0.35,_dir);
            this.currotV = _dir;
            Laya.Quaternion.rotationLookAt(_dir,new Vector3(0,1,0),this.currot);
            Laya.Quaternion.slerp(this.m_sprite.transform.rotation,this.currot,0.025,this.currot);
            this.currot = new Laya.Quaternion(0,this.currot.y,0);
            this.m_sprite.transform.rotation = this.currot;
        }
        else
        {
            Laya.Quaternion.slerp(this.m_sprite.transform.rotation,new Laya.Quaternion(0,0,0,0),0.1,this.currot);
            this.currot = new Laya.Quaternion(0,this.currot.y,0);
            this.m_sprite.transform.rotation = this.currot;
        }
    }

    backrot = false;
    BackRote()
    {
        this.backrot = true;
    }

    Cheat()
    {
        var stage = Player.Instance.m_runner.completeStage;
        if(0.65 < Math.random())return;
        if(!stage || Player.Instance.m_runner.m_curstages[Player.Instance.m_runner.m_curstages.length - 1].enter || Player.Instance.m_runner.die)return;
        stage.complete = false;
        this.curstage = stage;
        this.die = false;
        this.m_crown.active = false;
        var pp = Player.Instance.m_runner;
        this.m_sprite.transform.position = new Vector3(pp.m_sprite.transform.position.x,stage.endy,stage.endz - 5);
        this.SetMove();
        this.SetExtraSpeed(0.01,2);
        this.curspeed = this.m_speed;
    }

    completeStage:Stage;
    curstage:Stage;
    canmove = false;
    die = false;
    curpos:Vector3 = new Vector3();
    onUpdate()
    {
        if(this.canmove)
        {
            this.Move(new Vector3(0,0,1),this.m_speed);
        }
        if(this.backrot)
        {
            Laya.Quaternion.slerp(this.m_sprite.transform.rotation,new Laya.Quaternion(0,0,0,0),0.25,this.currot);
            this.currot = new Laya.Quaternion(0,this.currot.y,0);
            this.m_sprite.transform.rotation = this.currot;
            if(Math.abs(this.m_sprite.transform.rotation.y) < 0.0001)
            {
                this.backrot = false;
            }
        }

        if(this.m_curstages[this.m_curstages.length - 1].complete == false)
        {
            for(var i = 0;i < this.m_curstages.length;i++)
            {
                var stage = this.m_curstages[i];
                if(stage.complete == false && this.m_sprite.transform.position.z > stage.endz)
                {
                    this.completeStage = stage;
                    stage.complete = true;
                    EventManager.EventTrigger(EventType.CompleteStage,stage);
                }
                if(stage.enter == false && this.m_sprite.transform.position.z > stage.startz - 3)
                {
                    this.curstage = stage;
                    stage.enter = true;
                    
                    if(!this.Ai)
                    {
                        //console.log(stage.id);
                        if(i == this.m_curstages.length - 1)LevelManager.Instance.curLevel.endroadsprite.active = true;
                        this.SetRoad(i);
                        EventManager.EventTrigger(EventType.GetRoadPoint,i + 1);
                    }
                    else
                    {
                        //this.SetRoad(i);
                        this.OnEnterStage(stage);
                    }
                }
            }
        }
        else
        {
            this.curstage.endy = LevelManager.Instance.curLevel.endroadsprite.transform.position.y - 5;
        }
        
        
        // if(!this.Ai && this.curstage)
        // {
            
        //     var me = Math.floor(this.m_sprite.transform.position.y) + ":" + (this.curstage.endy);
        //     EventManager.EventTrigger(EventType.Hover,me.toString());

        //     var me = Math.floor(this.m_sprite.transform.position.z) + ":" + (this.curstage.endz);
        //     EventManager.EventTrigger(EventType.speed,me);
        // }
        if(this.curstage && this.flytime == 0 && !this.die && this.m_sprite.transform.position.y < this.curstage.endy - 8)
        {
            this.die = true;
            this.Stop();
            if(this.m_flySprite.active)
            {
                this.m_flySprite.active = false;
            }
            if(!this.Ai)
            {
                //console.log(this.m_sprite.transform.position.y + "..." + (this.curstage.endy - 5));
                PlaySoundManager.Play(SoundType.die);
                WxApi.Shake(false);
                GameManager.Instance.ChangeState(GameState.Die);
            }
        }
    }

    SetRoad(_stageid:number)
    {
        if(_stageid + 2 < LevelManager.Instance.curLevel.stages.length)
        {
            var nextroads = LevelManager.Instance.curLevel.stages[_stageid + 2].roads;
            for(var i = 0;i < nextroads.length;i++)
            {
                var road = nextroads[i];
                road.SetActive(true);
            }

            var builds = LevelManager.Instance.curLevel.stages[_stageid + 2].builds;
            for(var i = 0;i < builds.length;i++)
            {
                var build = builds[i];
                build.active = true;
            }
        }
        if(_stageid - 1 >= 0)
        {
            var oldroads = LevelManager.Instance.curLevel.stages[_stageid - 1].roads;
            for(var i = 0;i < oldroads.length;i++)
            {
                var road = oldroads[i];
                Laya.timer.once(1000,this,()=>
                {
                    road.SetActive(false);
                })
            }
            var oldbuilds = LevelManager.Instance.curLevel.stages[_stageid - 1].builds;
            for(var i = 0;i < oldbuilds.length;i++)
            {
                var build = oldbuilds[i];
                build.active = false;
            }
        }
    }

    OnEnterStage(_stage:Stage)
    {
        this.OnGetRoadPoint(_stage.roadpointxs);
    }

    public GetCrown()
    {
        this.m_crown.active = true;
        Laya.timer.clear(this,this.RotCrown);
        Laya.timer.frameLoop(1,this,this.RotCrown);
    }

    CloseCrown()
    {
        this.m_crown.active = false;
        Laya.timer.clear(this,this.RotCrown);
    }

    RotCrown()
    {
        this.m_crown.transform.rotate(new Vector3(0,2,0));
    }

    endroad = false;
    curspeed:number = 0;
    Move(_dir:Vector3,_speed:number)
    {
        if(this.flytime > 0)
        {
            this.flytime -= this.flycutrate;
            this.flycounttime += (this.flycutrate*0.6);
            //if(!this.Ai)console.log(this.flytime);
            if(!this.Ai)EventManager.EventTrigger(EventType.flytime,this.flytime);
            _speed = this.flyspeed + this.extraspeed;
            if(this.flytime < 0)
            {
                //if(!this.OnLand)this.m_animator.play("land2");
                //this.m_flySprite.active = false;
                this.flytime = 0;
                //this.fly = false;
                this.flycounttime = 0;
            }
        }
        else
        {
            if(this.fly)
            {
                _speed = (this.flyspeed*0.8) + this.extraspeed;
            }
        }

        if(this.extraspeedtime > 0 || this.extraspeed > 0)
        {
            this.extraspeedtime -= this.extracutrate;
            if(!this.Ai)EventManager.EventTrigger(EventType.extraspeedtime,this.extraspeedtime);
            var esp = _speed + this.extraspeed;
            _speed = GameUtils.lerp(_speed,esp,0.9);
            if(this.extraspeedtime < 0)
            {
                //this.m_huo.active = false;
                this.extraspeed = 0;
                this.extraspeedtime = 0;
            }
        }

        var sp = GameUtils.lerp(this.curspeed,_speed,0.1);

        this.curspeed = sp;

        Vector3.scale(_dir,this.curspeed,this.offest);
        Vector3.add(this.m_sprite.transform.position,this.offest,this.curpos);
        Vector3.lerp(this.m_sprite.transform.position,this.curpos,0.15,this.curpos);

        //jump
        if(this.JumpHigth > 0)
        {
            //if(!this.Ai)console.log(this.JumpHigth);
            
            this.JumpDuration += ThingsEffect.jumptime;
            this.JumpHigth = this.CurHight(this.csp,this.JumpDuration);
            this.curpos = new Vector3(this.curpos.x,this.curpos.y + this.JumpHigth,this.curpos.z);
        }

        this.curpos = new Vector3(this.curpos.x + this.aioffestx,this.curpos.y,this.curpos.z);

        this.m_sprite.transform.position = this.curpos;

        if(this.endroad == false && (this.m_sprite.transform.position.z > LevelManager.Instance.curLevel.endroadsprite.transform.position.z))
        {
            this.endroad = true;
            if(this.fly)
            {
                this.fly = false;
                this.flytime = 0;
                this.flycounttime = 0;
                this.m_flySprite.active = false;
                if(this.OnLand)
                {
                    this.m_animator.play("land1");
                }
            }
        }
    }

    CheckForward()
    {
        var startpoint = new Vector3(this.m_sprite.transform.position.x,this.m_sprite.transform.position.y + 0.5,this.m_sprite.transform.position.z);
        var dir = new Vector3(0,0,1);
        var ray:Laya.Ray = new Laya.Ray(startpoint,dir);
        if(ScenceManager.Instance.GameScence.physicsSimulation.rayCast(ray,this.hit,0.5))
        {
            if(this.hit.collider.collisionGroup != ColliderLay.things)return;
            //console.log(this.hit.collider.owner.name);
            this.TriggerThings(this.hit.collider);
        }
    }

    TriggerThings(_collider:Laya.PhysicsComponent)
    {
        if(this.curcollider == _collider && _collider.owner.name != "obstacle")return;
        this.curcollider = _collider;
        //console.log(_collider.owner.name);
        //_collider.collisionGroup = 0;

        if(_collider.owner.name == "Box")
        {
            if(!this.Ai)
            {
                PlaySoundManager.Play(SoundType.crash);
                WxApi.Shake();
            }
            _collider.enabled = false;
            this.HitBox(_collider.owner);
            //this.Stop();
        }
        else if(_collider.owner.name == "obstacle")
        {
            if(!this.Ai)
            {
                PlaySoundManager.Play(SoundType.crash);
                WxApi.Shake();
            }
            
            this.fly = false;
            this.flytime = 0;
            this.flycounttime = 0;
            this.extraspeed = 0;
            this.HitObstacle(_collider.owner);
        }
        else if(_collider.owner.name == "fly")
        {
            if(!this.Ai)
            {
                PlaySoundManager.Play(SoundType.fly);
                WxApi.Shake();
            }
            this.SetFly();
        }
        else if(_collider.owner.name == "rocket")
        {
            if(!this.Ai)
            {
                PlaySoundManager.Play(SoundType.xp);
                WxApi.Shake();
            }
            this.SetExtraSpeed(ThingsEffect.rocketeffect,ThingsEffect.rockettime);
        }
        else if(_collider.owner.name == "Jump")
        {
            if(!this.Ai)
            {
                PlaySoundManager.Play(SoundType.jump);
                WxApi.Shake();
            }
            this.outlandtime = 0;
            this.fly = false;
            this.flytime = 0;
            this.flycounttime = 0;
            this.Jump(ThingsEffect.jumpeffect);
            this.SetExtraSpeed(0.3,0.2);
        }
        else if(_collider.owner.name == "Coin")
        {
            if(!this.Ai)
            {
                PlaySoundManager.Play(SoundType.gold);
                WxApi.Shake();
                EventManager.EventTrigger(EventType.GetMoney);
            }
            var sprite = _collider.owner.parent as Sprite3D;
            sprite.active = false;
        }
        else if(_collider.owner.name == "Win")
        {
            this.m_animator.play("win");
            if(this.m_flySprite.active)
            {
                this.m_flySprite.active = false;
            }
            
            if(!this.Ai)
            {
                if(Queue.Instance.GetPlayerScore == 1)
                {
                    GameManager.Instance.ChangeState(GameState.Win);
                }
                else
                {
                    Player.CanInput = false;
                    this.Stop();
                    this.m_animator.play("idle2");
                    GameManager.Instance.ChangeState(GameState.End,false);
                }
            }
            else
            {
                this.Stop();
                this.m_animator.play("fly");
            }
        }
    }

    Gravity()
    {
        if(this.OnLand == false)
        {
            if(this.extraspeed > 0 && this.flytime <= 0)
            {
                if(this.jump)
                {
                    this.outlandtime += 0.007;
                }
                else
                {
                    this.outlandtime += 0.007;
                }
            }
            else if(this.flytime > 0)
            {
                //this.outlandtime += 0.0013;
                if(this.flycounttime > 0 && this.flycounttime < 0.5)
                {
                    this.outlandtime += 0.004;
                }
                else if(this.flycounttime > 0.5 && this.flycounttime < 1.3 && this.outlandtime > 0)
                {
                    this.outlandtime -= 0.004;
                }
                else
                {
                    this.outlandtime += 0.0035;
                }
            }
            else
            {
                if(this.fly)
                {
                    this.outlandtime += 0.003;
                }
                else
                {
                    this.outlandtime += 0.011;
                }
                //this.LandTime += 0.1;

                //if(this.Ai && this.LandTime > 10)PoolManager.Instance.RecoverMeshSprite3D(this.m_sprite);
                //if(!this.Ai)EventManager.EventTrigger(EventType.Hover,this.LandTime);
            }
            
            var offesty = this.outlandtime*this.outlandtime;
            this.m_sprite.transform.position = new Vector3(this.m_sprite.transform.position.x,this.m_sprite.transform.position.y - offesty,this.m_sprite.transform.position.z);
        }
    }

    delayDestory(_sprite:Sprite3D)
    {
        _sprite.destroy();
        //console.log(_sprite.name);
    }
    HitBox(_box:Laya.Node)
    {
        //this.Stop();
        var bp = _box as Sprite3D;

        var boxspite = _box.parent.getChildAt(0) as Laya.Sprite3D;
        var pps = _box.parent as Sprite3D;
        Laya.timer.once(1000,this,this.delayDestory,[_box.parent]);
        this.TriggerHit(boxspite,bp.transform.position.clone());
    }
    TriggerHit(_node:Laya.Node,_center:Vector3)
    {
        var childNumber = _node.numChildren;

        for(var i = 0;i < childNumber;i ++)
        {
            var box = _node.getChildAt(i) as Sprite3D;
            var rig = box.addComponent(Rigbody) as Rigbody;
            var dir = new Vector3();
            Vector3.subtract(box.transform.position,_center,dir);
            Vector3.normalize(dir,dir);
            rig.AddVelocity(dir,GameUtils.getRandoms(0.1,0.5));
        }
    }

    MoveX(_speed:number)
    {

        _speed*=0.07;

        //var dir = new Vector3(_speed,0,0);
        //Vector3.add(this.m_sprite.transform.position,dir,this.m_sprite.transform.position);
        this.m_sprite.transform.translate(new Vector3(_speed,0,0));

        this.m_sprite.transform.position = this.m_sprite.transform.position.x < -4.5?new Vector3(-4.5,this.m_sprite.transform.position.y,this.m_sprite.transform.position.z):this.m_sprite.transform.position;
        this.m_sprite.transform.position = this.m_sprite.transform.position.x > 4.5?new Vector3(4.5,this.m_sprite.transform.position.y,this.m_sprite.transform.position.z):this.m_sprite.transform.position;
        //this.m_sprite.transform.position = this.curpos;
    }

    AutoMoveSpeed = 0.5;
    curautospeed = 0;
    AutoMoveX(_targetx:number)
    {
        this.curautospeed = 0;
        Laya.timer.clear(this,this.AutoMoveLoop);
        Laya.timer.frameLoop(1,this,this.AutoMoveLoop,[_targetx]);
    }

    AutoMoveLoop(_targetx:number)
    {
        if(Math.abs(_targetx - this.m_sprite.transform.position.x) < 0.01)
        {
            Laya.timer.clear(this,this.AutoMoveLoop);
        }
        var speed = GameUtils.lerp(this.curautospeed,this.AutoMoveSpeed,0.25);
        if(_targetx > this.m_sprite.transform.position.x)
        {
            this.MoveX(speed);
        }
        else
        {
            this.MoveX(-speed);
        }
    }
}