import {TimerService} from "../Time/TimerService";
import {PropManager} from "../PrefabControl/PropManager";
import {CharacterState} from "../PrefabControl/CharacterState";
import {RoleData} from "../Data/RoleData";
import {PanelResDic} from "../PrefabControl/PanelResDic";
import {AgencyUserMessage} from "../Agency/AgencyUserMessage";
import BuildCFG from "../CFG/BuildCFG";
import { GlobalTip } from "../utils/GlobalTip";
import {CameraManager} from "./CameraManager";
import { DispatcherManager } from "../utils/DispatcherManager";
import {GameState} from "../utils/GameState";
import { GameUtils } from "../utils/GameUtils";
import {GameStateControl} from "../Control/GameStateControl";
import {AudioControl} from "../Control/AudioControl";
import {WXGamePlatform} from "../WXSDK/WXGamePlatform";
import { PlayerAccount } from "../WXSDK/PlayerAccount";

export class Player extends Laya.Script3D implements TimerControl
{
    public _transform:Laya.Transform3D;
    public _iscanMove:boolean;
    public _speedMoveV3:Laya.Vector3;

    private _isActionNums:boolean = false;
    private _isneedactionnum:number = 0;
    private _needactiontime:number = 0;
    
    private _nowActionState:string = "";

    public _RoleData:RoleData;

    private _animator:Laya.Animator;
    private _multiple:Laya.Vector3 = new Laya.Vector3( 1,1,1 );

    private LeftTrial:Laya.TrailSprite3D;
    private RightTrial:Laya.TrailSprite3D;

    private LastActionState:string = "";
    private _moveTime:number = 0;

    /**
     * 加速buffvec
     */
    private _lastjiasuState:string = "";
    private buff_speeduptime:number = 0;
    private canshake:boolean = false;
    private shaketime:number = 0;
    public doublespeedup:boolean = false;

    resouseName:string = "";

    private static ins:Player;

    constructor()
    {
        super();
        Player.ins = this;
    }
    public static get Instance():Player{
        if(Player.ins == null){
            return new Player();
        }
        return Player.ins;
    }

    public onAwake()
    {
        this._animator = (this.owner as Laya.Sprite3D).getComponent( Laya.Animator );
        PropManager.setAnimator( this._animator );
        this._speedMoveV3 = new Laya.Vector3( 0,0,0 );
        var sp:Laya.Sprite3D = this.owner as Laya.Sprite3D;
        this._transform = sp.transform;
        this._RoleData = (PanelResDic.Instance.getSingleTon( AgencyUserMessage ) as AgencyUserMessage).RoleData;
        ( sp.getComponent( Laya.Rigidbody3D ) as Laya.Rigidbody3D ).ccdMotionThreshold = 0.001;
        this.LeftTrial = sp.getChildByName("lefttrail") as Laya.TrailSprite3D;
        this.RightTrial = sp.getChildByName("righttrail") as Laya.TrailSprite3D;
    }

    public _initPosVec:Laya.Vector3;
    public startRun(jiasu?):void
    {
        if( this._RoleData.roleCfg.gravity )
        {
            this.g = this._RoleData.roleCfg.gravity;
        }
        else
        {
            this.g = 10;
        }
        this._multiple.x = 1;
        this._multiple.y = 1;
        this._multiple.z = 1;
        this.buff_speeduptime = 0;
        this.doublespeedup = false;
        this._lastjiasuState = "";
        this._ending = false;
        this._changeMoveX = 0;
        this._initPosVec = this._transform.position.clone();
        console.log("玩家的初始位置----",this._initPosVec);
        this._nowbuildname = "";
        this._nowscenename = "";
        this.nowAction = CharacterState.run;
        this._speedMoveV3.x = 0;
        this._speedMoveV3.y = 0;
        if(jiasu){
            this._speedMoveV3.z = -this._RoleData.movespeed*2;
            Laya.timer.once(3000,this,()=>{
                this._speedMoveV3.z = -this._RoleData.movespeed;
            })
        }
        else{
            this._speedMoveV3.z = -this._RoleData.movespeed;
        }
        this._iscanMove = true;
        TimerService.Instance.addUpdateTarget( this );
    }
    

    private _isColling:boolean = false;
    private _nowCollTargetName:string = "";
    private _normalizeVec:Laya.Vector3 = new Laya.Vector3;
    private _nowselectVec:Laya.Vector3;
    public _nowbuildname:string = "";
    public _nowscenename:string = "";

    // private pzdict:any = {};
    // private pzarr:Array<any> = [];
    /**
     * 开始碰撞时执行
     */
    public onCollisionEnter(collision:Laya.Collision):void {
        if( collision && collision.contacts && collision.contacts.length > 0 && collision.other )
        {
            var contactPoint:Laya.ContactPoint = collision.contacts[0];
            this.Calculate( contactPoint.normal,collision.other.owner,contactPoint.positionOnB);
        }
    }

    /**
     * 持续碰撞时执行
     */
    public  onCollisionStay(collision:Laya.Collision):void 
    {
        // Debuger.log( "碰撞中" );
        if( collision && collision.contacts && collision.contacts.length > 0 && collision.other )
        {
            this._isColling = true;
            var contactPoint:Laya.ContactPoint = collision.contacts[0];
            var target = collision.other.owner;
            var targetname:string = target.name;
            if( targetname != "SliderBoost" && targetname.indexOf( "npc" ) == -1  )
            {
                this._nowCollTargetName = targetname;
            }
            if( this._nowCollTargetName.indexOf( "build" ) != -1 || this._nowCollTargetName.indexOf( "Scaffolding" ) != -1 ||
                this._nowCollTargetName.indexOf( "irongauze" ) != -1 ) //碰到建筑，判断是上爬，左爬还是右爬
            {
                if( this._nowCollTargetName.indexOf( "build" ) != -1 )
                {
                    this._nowbuildname = this._nowCollTargetName;
                    this._nowscenename = target.parent.parent.name;
                }

                if( this._nowCollTargetName.indexOf( "irongauze" ) != -1 )
                {
                    this._nowselectVec = PropManager.getPosVecbyScene( target.parent.parent.parent.name,target.parent.name );
                }
                else
                {
                    this._nowselectVec = PropManager.getPosVecbyScene( target.parent.parent.name,this._nowCollTargetName );
                } 
            }
        }
            
    }
    /**
     * 结束碰撞时执行
     */
    public onCollisionExit(collision:Laya.Collision):void 
    {
        this._isColling = false;
        if( collision && collision.contacts && collision.contacts.length > 0 && collision.other )
        {
            var contactPoint:Laya.ContactPoint = collision.contacts[0];
            this.CheckEnd( contactPoint.normal,collision.other.owner );
        }
    }


    /**检测结束碰撞 */
    private CheckEnd( normal:Laya.Vector3,target:Laya.Node ):void
    {
        if( target.name == "SliderBoost" || target.name.indexOf( "npc" ) != -1 )
        {
            return;
        }
        if( this.isforceJump )
        {
            this.isforceJump = false;
            return;
        }
        if( this._lockxstate )
        {
            this._lockxstate = 0;
            return;
        }
        if( this._nowCollTargetName != target.name )
        {
            return;
        }
        if( target.name.indexOf( "panel_jump" ) != -1 )
        {
            return;
        }
        switch( this.nowAction )
        {
            case CharacterState.climbup:
            case CharacterState.climbupblock:
                if( this._nowCollTargetName.indexOf( "box_Board" ) != -1 )
                {
                    this.JumpAction( this._changeMoveX );
                }
                else
                {
                    if( this._changeMoveX > 0 )
                    {
                        this.nowAction = CharacterState.jumpright;
                    }
                    else if( this._changeMoveX < 0 )
                    {
                        this.nowAction = CharacterState.jumpleft;
                    }
                }
            break;
            case CharacterState.climbright:
            case CharacterState.climbleft:
                this.JumpAction( this._changeMoveX );
            break;
            case CharacterState.run:
                this.JumpAction( this._changeMoveX );
            break;
            case CharacterState.MoveX:
            this.JumpAction( this._speedMoveV3.x );
            break;
        }
    }
    /**
     * 0:未锁，1：锁左边，2：锁右边
     */
    private _lockxstate:number = 0;

    public setnowSelectVec( pos:Laya.Vector3 ):void
    {
        this._nowselectVec = pos;
    }

    private _lockbyBuildname:string = "";
    /**
     * 0:未被锁，1：锁定左边，2：锁定右边，
     */
    private _lockstate:number = 0;

    private _shangpaVec:Laya.Vector3 = new Laya.Vector3;

    /**计算碰撞 */
    private Calculate( normal:Laya.Vector3,target:Laya.Node,collpoint:Laya.Vector3 ):void
    {
        if( target.name == "SliderBoost" || target.name.indexOf( "npc" ) != -1 )
        {
            return;
        }
        this._nowCollTargetName = target.name;
        var n:Laya.Vector3 = new Laya.Vector3;
        Laya.Vector3.normalize(normal,this._normalizeVec);
        if( this._nowCollTargetName.indexOf( "build" ) != -1 || this._nowCollTargetName.indexOf( "Scaffolding" ) != -1 || 
            this._nowCollTargetName.indexOf( "irongauze" ) != -1) //碰到建筑，判断是上爬，左爬还是右爬
        {
            if( this._nowCollTargetName.indexOf( "build" ) != -1 )
            {
                this._nowbuildname = this._nowCollTargetName;
                this._nowscenename = target.parent.parent.name;
            }
            if( this._nowCollTargetName.indexOf( "irongauze" ) != -1 )
            {
                this._nowselectVec = PropManager.getPosVecbyScene( target.parent.parent.parent.name,target.parent.name );
            }
            else
            {
                this._nowselectVec = PropManager.getPosVecbyScene( target.parent.parent.name,this._nowCollTargetName );
            }   
            
            if( this._normalizeVec.z == -1 ) //上爬
            {
                if( this._nowselectVec && this._transform.position.y > (this._nowselectVec.y - 0.2) )
                {
                    this._transform.position = new Laya.Vector3( this._transform.position.x,this._nowselectVec.y,this._transform.position.z );
                    this.nowAction = CharacterState.run;
                }
                else
                {
                    this.nowAction = CharacterState.climbup;
                    this._shangpaVec = this._nowselectVec.clone();
                }
                
            }
            else if( this._normalizeVec.y == 1 ||this._normalizeVec.y == -1 )
            {
                this.nowAction = CharacterState.run;
            }
            else if( this._normalizeVec.x == -1 )
            {
                if( this.nowAction == CharacterState.run )
                {
                    this._lockxstate = 1;
                }
                else if( this.nowAction == CharacterState.climbup )
                {
                    this._lockxstate = 1;
                }
                else
                {
                    this.nowAction = CharacterState.climbleft;    
                }
            }
            else if( this._normalizeVec.x == 1 )
            {
                if( this.nowAction == CharacterState.run )
                {
                    this._lockxstate = 2;
                }
                else if( this.nowAction == CharacterState.climbup )
                {
                    this._lockxstate = 2;
                }
                else
                {
                    this.nowAction = CharacterState.climbright;  
                }
            }
            else 
            {
                if( this._normalizeVec.z != 1 )
                {
                    this.nowAction = CharacterState.climbupblock;
                    this._shangpaVec = this._nowselectVec.clone();
                }
            }
        }
        else if( this._nowCollTargetName.indexOf( "panel_jump" ) != -1 ) //加速板跳跃
        {
            this.nowAction = CharacterState.speedupjump;
        }
        else if( this._nowCollTargetName.indexOf("panel_downslip") != -1 )
        {
            this.nowAction = CharacterState.downmove;
        }
        else if( this._nowCollTargetName.indexOf( "box_Board" ) != -1 && this.nowAction != CharacterState.downmove )
        {
            if( this._normalizeVec.z == -1 ) //上爬
            {
                this.nowAction = CharacterState.climbup;
                this._shangpaVec = this._nowselectVec.clone();
            }
        }
        else if( this._nowCollTargetName.indexOf( "box_sidejump" ) != -1 )
        {
            this.nowAction = CharacterState.sidespeedup;
        }
        else if( this._nowCollTargetName.indexOf( "box_Surmount" ) != -1 )
        {
            this.nowAction = CharacterState.cross;
        }
        else if( this._nowCollTargetName.indexOf( "irongauze" ) != -1 )
        {
            this.nowAction = CharacterState.climbup;
            this._shangpaVec = this._nowselectVec.clone();
        }
    }

    

    private JumpAction( value:number ):void
    {
        if( value > 0 )
        {
            this.nowAction = CharacterState.jumpright;
            // this.setJump( this.nowAction );
        }
        else if( value < 0 )
        {
            this.nowAction = CharacterState.jumpleft;
            // this.setJump( this.nowAction );
        }
        else
        {
            this.nowAction = CharacterState.jump;
            // this.setJump( this.nowAction );
        }
    }


    private _StateName:string;
    public PlayAnimator( actionName:string ):void
    {
        var _actionname = this.TransitionActionName(actionName);
        this._StateName = actionName;
        if( this._animator )
        {
            if( this._StateName == CharacterState.climbend || this._StateName == CharacterState.land1 || this._StateName == CharacterState.land2 )
            {
                this._animator.speed = 1.5;
            }
            else
            {
                this._animator.speed = 1;
            }
            this._animator.play(_actionname,0,0);
        }
    }

    /**转换动作名称 */
    TransitionActionName(_actionname:string):string{
        switch (_actionname) {
            case "rollover1":
            return "cefan1"
            case "rollover2":
            return "cefan2"
            case "runwallleft":
            return "paoqiangleft"
            case "rightjump":
            return "rightjump"
            case "runwallright":
            return "paoqiangright"
            case "leftjump":
            return "leftjump"
            case "climbwall":
            return "paqiang"
            case "climbend":
            return "climbend"
            case "stand":
            return "stand"
            case "sky":
            return "kongzhong"
            case "land1":
            return "luodi1"
            case "land2":
            return "luodi2"
            case "highjump1":
            return "datiao1"
            case "highjump2":
            return "datiao2"
            case "run":
            return "run"
            case "jump":
            return "jump"
            case "climbup":
            return "Climb"
            case "falldown":
            return "Freefalling"
            case "climbleft":
            return "left_climb"
            case "climbright":
            return "right_climb"
            case "jumpright":
            return "right_jump"
            case "jumpleft":
            return "left_jump"
            case "ClimbMotion":
            return "PlayPalouAction"
            case "huachan":
            return "huachan"
            case "hurdle":
            return "kualan"
            case "speedupjump":
            return "jiasuJump"
            case "MoveX":
            return "xshunyi"
            case "downmove":
            return "downSlide"
            case "sidespeedup":
            return "slidejiasu"
            case "cross":
            return "surmount"
            case "climbupblock":
            return "lockclimb"
        }

    }


    public set nowAction( value:string )
    {
        if( this._nowActionState != value || this.LastActionState == CharacterState.speedupjump || this._nowActionState == CharacterState.speedupjump )
        {
            this.LastActionState = this._nowActionState;
            this._nowActionState = value;
            this._moveTime = 0;
            this.canshake = false;
            this.ChangeTrial( true );
            switch( this._nowActionState )
            {
                case CharacterState.stand:
                    this._isActionNums = false;
                    this.PlayAnimator( CharacterState.stand );
                break;
                case CharacterState.run:
                    if( this.LastActionState == CharacterState.jump || this.LastActionState == CharacterState.jumpleft ||this.LastActionState == CharacterState.jumpright )
                    {
                        this._isActionNums = true;
                        this._isneedactionnum = 1;
                        var sort:number = GameUtils.randRangeint( 0,100 );
                        if( sort < 50 )
                        {
                            this.PlayAnimator( CharacterState.land1 );
                        }
                        else
                        {
                            this.PlayAnimator( CharacterState.land2 );
                        }
                    }
                    else
                    {
                        this._isActionNums = false;
                        this.PlayAnimator( CharacterState.run );
                    }
                break;
                case CharacterState.jump:
                    this.PlayAnimator( CharacterState.jump );
                    PropManager.Instance.SetEffectColor( "color_white", this._transform.position );
                    this._isActionNums = true;
                    this._isneedactionnum = 1;
                    this.jumpSpeed.x = this._RoleData.frontjump.x;
                    this.jumpSpeed.y = this._RoleData.frontjump.y;
                    this.jumpSpeed.z = this._RoleData.frontjump.z;
                    AudioControl.Instance.playGameSound("tiao");
                    WXGamePlatform.Instance.vibrateShort();
                break;
                case CharacterState.jumpleft:
                    PropManager.Instance.SetEffectColor( "color_white", this._transform.position );
                    this.jumpSpeed.x = this._RoleData.leftjump.x;
                    this.jumpSpeed.y = this._RoleData.leftjump.y;
                    this.jumpSpeed.z = this._RoleData.leftjump.z;
                    this._isActionNums = true;
                    this._isneedactionnum = 1;
                    this.PlayAnimator( CharacterState.leftjump );
                    AudioControl.Instance.playGameSound("tiao");
                    WXGamePlatform.Instance.vibrateShort();
                break;
                case CharacterState.jumpright:
                    PropManager.Instance.SetEffectColor( "color_white", this._transform.position );
                    this.jumpSpeed.x = this._RoleData.rightjump.x;
                    this.jumpSpeed.y = this._RoleData.rightjump.y;
                    this.jumpSpeed.z = this._RoleData.rightjump.z;
                    this._isActionNums = true;
                    this._isneedactionnum = 1;
                    this.PlayAnimator( CharacterState.rightjump );
                    AudioControl.Instance.playGameSound("tiao");
                    WXGamePlatform.Instance.vibrateShort();
                break;
                case CharacterState.speedupjump:
                    var buildcfg:BuildCFG = BuildCFG.dict[ "panel_jump" ];
                    if( this.buff_speeduptime > 0 )
                    {
                        this._multiple.x = 1;
                        this._multiple.y = 1;
                        this._multiple.z = buildcfg.buffz * buildcfg.buffz;
                        this.doublespeedup = true;
                    }
                    else
                    {
                        this._multiple.x = 1;
                        this._multiple.y = 1;
                        this._multiple.z = buildcfg.buffz;
                    }
                    this.buff_speeduptime = buildcfg.time;
                    this.jumpSpeed.x = buildcfg.buffx * this._multiple.x;
                    this.jumpSpeed.y = buildcfg.buffy;
                    this.jumpSpeed.z = this._speedMoveV3.z*buildcfg.buffz * this._multiple.z;
                    this._isActionNums = true;
                    this._isneedactionnum = 1;
                    var sort:number = GameUtils.randRangeint( 0,100 );
                    if( sort < 50 )
                    {
                        this.PlayAnimator( CharacterState.highjump1 );
                    }
                    else
                    {
                        this.PlayAnimator( CharacterState.highjump2 );
                    }
                    AudioControl.Instance.playGameSound("tiao");
                    Laya.timer.clear( this,this.playspeedSound );
                    Laya.timer.once( 200,this,this.playspeedSound );
                    WXGamePlatform.Instance.vibrateShort();
                break;
                case CharacterState.downmove:
                    var buildcfg:BuildCFG = BuildCFG.dict[ "panel_downslip" ];
                    if( this.buff_speeduptime > 0 )
                    {
                        this._multiple.x = 1;
                        this._multiple.y = 1;
                        this._multiple.z = buildcfg.buffz * buildcfg.buffz;
                        this.doublespeedup = true;
                    }
                    else
                    {
                        this._multiple.x = 1;
                        this._multiple.y = 1;
                        this._multiple.z = buildcfg.buffz;
                    }
                    this.buff_speeduptime = buildcfg.time;
                    this.jumpSpeed.x = buildcfg.buffx * this._multiple.x;
                    this.jumpSpeed.y = buildcfg.buffy * this._multiple.y;
                    this.jumpSpeed.z = this._speedMoveV3.z*buildcfg.buffz * this._multiple.z;
                    this._isActionNums = true;
                    this._isneedactionnum = 1;
                    this.PlayAnimator( CharacterState.huachan );
                    this.playspeedSound();
                    WXGamePlatform.Instance.vibrateShort();
                break;
                case CharacterState.sidespeedup:
                    var buildcfg:BuildCFG = BuildCFG.dict[ "box_sidejump" ];
                    if( this.buff_speeduptime > 0 )
                    {
                        this._multiple.x = 1;
                        this._multiple.y = 1;
                        this._multiple.z = buildcfg.buffz * buildcfg.buffz;
                        this.doublespeedup = true;
                    }
                    else
                    {
                        this._multiple.x = 1;
                        this._multiple.y = 1;
                        this._multiple.z = buildcfg.buffz;
                    }
                    this.buff_speeduptime = buildcfg.time;
                    this.jumpSpeed.x = buildcfg.buffx * this._multiple.x;
                    this.jumpSpeed.y = buildcfg.buffy * this._multiple.y;
                    this.jumpSpeed.z = this._speedMoveV3.z*buildcfg.buffz * this._multiple.z;
                    this._isActionNums = false;
                    this.playspeedSound();
                    WXGamePlatform.Instance.vibrateShort();
                    // this.PlayAnimator( CharacterState. );
                break;
                case CharacterState.cross:
                    var buildcfg:BuildCFG = BuildCFG.dict[ "box_Surmount" ];
                    if( this.buff_speeduptime > 0 )
                    {
                        this._multiple.x = 1;
                        this._multiple.y = 1;
                        this._multiple.z = buildcfg.buffz * buildcfg.buffz;
                        this.doublespeedup = true;
                    }
                    else 
                    {
                        this._multiple.x = 1;
                        this._multiple.y = 1;
                        this._multiple.z = buildcfg.buffz;
                    }
                    this.buff_speeduptime = buildcfg.time;
                    this.jumpSpeed.x = buildcfg.buffx * this._multiple.x;
                    this.jumpSpeed.y = (buildcfg.buffy+0.5) * this._multiple.y;
                    this.jumpSpeed.z = this._speedMoveV3.z*buildcfg.buffz * this._multiple.z;
                    this._isActionNums = true;
                    this._isneedactionnum = 1;
                    this.PlayAnimator( CharacterState.hurdle );
                break;
                case CharacterState.climbup:
                case CharacterState.climbupblock:
                    this.jumpSpeed.x = 0;
                    this.jumpSpeed.y = this._RoleData.upclimb;
                    this.jumpSpeed.z = 0;
                    this._isActionNums = false;
                    this.PlayAnimator( CharacterState.climbwall );
                    WXGamePlatform.Instance.vibrateShort();
                    this.canshake = true;
                    this.shaketime = 0;
                    this.ChangeTrial( false );
                break;
                case CharacterState.climbleft:
                    this.jumpSpeed.x = 0;
                    this.jumpSpeed.y = this._RoleData.slideclimb.y;
                    this.jumpSpeed.z = this._RoleData.slideclimb.z;
                    this._isActionNums = false;
                    this.PlayAnimator( CharacterState.runwallleft );
                    this.ChangeTrial( false );
                break;
                case CharacterState.climbright:
                    this.jumpSpeed.x = 0;
                    this.jumpSpeed.y = this._RoleData.slideclimb.y;
                    this.jumpSpeed.z = this._RoleData.slideclimb.z;
                    this._isActionNums = false;
                    this.PlayAnimator( CharacterState.runwallright );
                    this.ChangeTrial( false );
                break;
                case CharacterState.ClimbMotion:
                    this._isActionNums = true;
                    this._isneedactionnum = 0;
                    this._needactiontime = 0.3;
                    this._moveTime = 0;
                    this.PlayAnimator( CharacterState.climbend );
                break;
            }
        }
    }

    private playspeedSound():void
    {
        AudioControl.Instance.playGameSound("speedup");
    }

    public get nowAction():string
    {
        return this._nowActionState;
    }

    private ChangeTrial( bo:boolean ):void
    {
        if( this.LeftTrial && this.RightTrial && this.LeftTrial.active != bo )
        {
            this.LeftTrial.active = bo;
            this.RightTrial.active = bo;
        }
    }

    public Joy( value:number,value1:number,angle:number ):void
    {
        if( this.nowAction == CharacterState.run )
        {
            this._changeMoveX = value;
        }
        else if( this.nowAction == CharacterState.climbup || CharacterState.climbleft || CharacterState.climbright )
        {
            this._changeMoveX = value1;
        }
        if( this.nowAction == CharacterState.climbleft && this._changeMoveX > 0 ) //强制跳跃
        {
            this.isforceJump = true;
            this.nowAction = CharacterState.jumpright;
        }
        else if( this.nowAction == CharacterState.climbright && this._changeMoveX < 0 )
        {
            this.isforceJump = true;
            this.nowAction = CharacterState.jumpleft;
        }
        if( this.nowAction == CharacterState.jump ||
            this.nowAction == CharacterState.jumpleft ||
            this.nowAction == CharacterState.jumpright ||
            this.nowAction == CharacterState.speedupjump )
        {
            angle = -angle * Math.PI/180;
            Laya.Quaternion.createFromAxisAngle( new Laya.Vector3( 0,1,0 ),angle,this.quaternion );
            var jumpz:number = this.jumpSpeed.z;
            Laya.Vector3.transformQuat( this.jumpSpeed,this.quaternion,this.jumpSpeed );
            this.jumpSpeed.z = jumpz;
        }
    }

    public _moveVec:Laya.Vector3 = new Laya.Vector3;
    private jumpSpeed:Laya.Vector3 = new Laya.Vector3;
    private jumpHight:number = 0;
    private _disy:number = 0;
    public g:number=10;//重力加速度

    public update( dt:number ):void
    {
        dt = 16;
        if( this._iscanMove )
        {
            var distime:number = dt/1000;
            this._moveTime += distime;
            switch( this.nowAction )
            {
                case CharacterState.run://跑动
                    this._moveVec.y = 0;
                    this._moveVec.z = distime * (this._speedMoveV3.z * this._multiple.z);
                    if( this._lockxstate == 0 )
                    {
                        this._moveVec.x = this._changeMoveX * distime;
                    }
                    else if( this._lockxstate == 1 )
                    {
                        if( this._changeMoveX < 0 )
                        {
                            this._moveVec.x = 0;
                        }
                        else
                        {
                            this._moveVec.x = this._changeMoveX * distime;
                        }
                    }
                    else if( this._lockxstate == 2 )
                    {
                        if( this._changeMoveX > 0 )
                        {
                            this._moveVec.x = 0;
                        }
                        else
                        {
                            this._moveVec.x = this._changeMoveX * distime;
                        }
                    }
                    this._transform.translate(this._moveVec, false);
                    break;
                case CharacterState.climbup://爬升
                    this._moveVec.z = 0;
                    this._moveVec.y = distime * this.jumpSpeed.y;
                    if( this._lockxstate == 1 )
                    {
                        if( this._changeMoveX < 0 )
                        {
                            this._moveVec.x = 0;
                        }
                        else
                        {
                            this._moveVec.x = this._changeMoveX * distime;
                        }
                    }
                    else if( this._lockstate == 2 )
                    {
                        if( this._changeMoveX > 0 )
                        {
                            this._moveVec.x = 0;
                        }
                        else
                        {
                            this._moveVec.x = this._changeMoveX * distime;
                        }
                    }
                    else
                    {
                        this._moveVec.x = this._changeMoveX * distime;
                    }
                    this._transform.translate(this._moveVec, false);
                    if( this._shangpaVec )
                    {
                        if( this._transform.position.y+1 > this._shangpaVec.y )
                        {
                            this.nowAction = CharacterState.ClimbMotion;
                        }
                    }
                    else if( this._nowselectVec )
                    {
                        if( this._transform.position.y+1 > this._nowselectVec.y )
                        {
                            this.nowAction = CharacterState.ClimbMotion;
                        }
                    }
                    break;
                case CharacterState.climbupblock:
                    this._moveVec.z = 0;
                    this._moveVec.y = distime * this.jumpSpeed.y;
                    this._moveVec.x = 0;
                    this._transform.translate(this._moveVec, false);
                    if( this._shangpaVec )
                    {
                        if( this._transform.position.y+1 > this._shangpaVec.y )
                        {
                            this.nowAction = CharacterState.ClimbMotion;
                        }
                    }
                    else if( this._transform.position.y+1 > this._nowselectVec.y )
                    {
                        this.nowAction = CharacterState.ClimbMotion;
                    }
                break;
                case CharacterState.jump://跳跃
                case CharacterState.jumpleft:
                case CharacterState.jumpright:
                    this._disy = this.jumpSpeed.y - this.g*distime;
                    this.jumpSpeed.y = this._disy;
                    this._moveVec.x = this.jumpSpeed.x * distime;
                    this._moveVec.y = this._disy*distime;
                    this._moveVec.z = this.jumpSpeed.z *distime;
                    this._transform.translate(this._moveVec, false);
                    break;
                case CharacterState.speedupjump:
                    this._disy = this.jumpSpeed.y - this.g*distime;
                    this.jumpSpeed.y = this._disy;
                    this._moveVec.x = this.jumpSpeed.x * distime;
                    this._moveVec.y = this._disy*distime;
                    this._moveVec.z = this.jumpSpeed.z *distime;
                    this._transform.translate(this._moveVec, false);
                    break;
                case CharacterState.climbleft:
                case CharacterState.climbright:
                    this._moveVec.z = distime * this.jumpSpeed.y;
                    this._moveVec.y = this.jumpSpeed.y*distime;
                    this._moveVec.x = 0;
                    this._transform.translate(this._moveVec, false);
                    break;
                case CharacterState.MoveX:
                    this._moveVec.z = distime * this._speedMoveV3.z;
                    this._moveVec.x = this._speedMoveV3.x*distime;
                    this._transform.translate(this._moveVec, false);
                    if( this._moveTime > 0.1 )
                    {
                        this.nowAction = CharacterState.run;
                    }
                break;
                case CharacterState.downmove:
                    this._moveVec.y = this.jumpSpeed.x;
                    this._moveVec.x = this.jumpSpeed.y;
                    this._moveVec.z = this.jumpSpeed.z *distime;
                    this._transform.translate(this._moveVec, false);
                break;
                case CharacterState.sidespeedup:
                    this._moveVec.y = 0;
                    this._moveVec.x = 0;
                    this._moveVec.z = this.jumpSpeed.z *distime;
                    this._transform.translate(this._moveVec, false);
                    if( this.buff_speeduptime <= 0 && this.LastActionState==CharacterState.climbleft || this.LastActionState == CharacterState.climbright )
                    {
                        this.nowAction = this.LastActionState;
                    }
                    else
                    {
                        this.nowAction = CharacterState.jump;
                    }
                break;
                case CharacterState.cross:
                    this._disy = this.jumpSpeed.y - this.g*distime;
                    this.jumpSpeed.y = this._disy;
                    this._moveVec.x = this.jumpSpeed.x * distime;
                    this._moveVec.y = this._disy*distime;
                    this._moveVec.z = this.jumpSpeed.z *distime;
                    this._transform.translate(this._moveVec, false);
                break;
                case CharacterState.ClimbMotion:
                    if(this._nowselectVec)
                    {
                        if( this._transform.position.y < this._nowselectVec.y )
                        {
                            this._moveVec.x = 0;
                            this._moveVec.y = (1/0.3)*distime;
                            this._moveVec.z = 0;
                            this._transform.translate(this._moveVec, false);
                        }
                    }
                break;
            }
            if( this._nowselectVec && (this.nowAction == CharacterState.run || this.nowAction == CharacterState.downmove ) && !this._isColling )
            {
                if( this._transform.position.z <= this._nowselectVec.z )
                {
                    if( this._changeMoveX > 0 )
                    {
                        this.nowAction = CharacterState.jumpright;
                    }
                    else if( this._changeMoveX < 0 )
                    {
                        this.nowAction = CharacterState.jumpleft;
                    }
                    else
                    {
                        this.nowAction = CharacterState.jump;
                    }
                }
            }
            if( this._transform.position.y < PropManager.diePosY - PlayerAccount.CurrentAccount.CurAccountData.DieDistance )//角色死亡
            {
                if( this._nowscenename == "" || this._nowbuildname == "" )
                {
                    if(GameState.fuhuocishu>0){
                        this.Revive();
                    }
                    else{
                        this.Lose();
                    }
                }
                else
                {
                    if(GameState.fuhuocishu>0){
                        this.Revive();
                    }
                    else{
                        this.Lose();
                    }
                }
                this.nowAction = CharacterState.stand;
            }
            if( this._transform.position.z < PropManager.gamewinPosZ )//到达终点
            {
                this.Win();
            }
            else
            {
                PropManager.pro_jindu.value =  this._transform.position.z / PropManager.gamewinPosZ * 100;
            }
            if( this._isActionNums )
            {
                if( this._isneedactionnum > 0 )
                {
                    if(this._animator.getCurrentAnimatorPlayState().normalizedTime >= this._isneedactionnum)
                    {
                        switch( this._StateName )
                        {
                            case CharacterState.jump:
                            case CharacterState.leftjump:
                            case CharacterState.rightjump:
                            case CharacterState.highjump1:
                            case CharacterState.highjump2:
                            case CharacterState.hurdle:
                                this._isActionNums = false;
                                this.PlayAnimator( CharacterState.sky);
                            break;
                            case CharacterState.huachan:
                            case CharacterState.climbend:
                                this._isActionNums = false;
                                this.PlayAnimator( CharacterState.run);
                                this.nowAction = CharacterState.run;
                                this._transform.position = new Laya.Vector3( this._transform.position.x,this._nowselectVec.y,this._transform.position.z );
                            break;
                            case CharacterState.land1:
                                // this._isActionNums = false;
                                // this.PlayAnimator( CharacterState.run );
                            case CharacterState.land2:
                                this._isActionNums = false;
                                this.PlayAnimator( CharacterState.run );
                            break;
                            case CharacterState.huachan:
                                this.nowAction = CharacterState.run;
                            break;
                        }
                    } 
                }
                else if( this._needactiontime != 0 && this._moveTime > this._needactiontime )
                {
                    switch( this._StateName )
                    {
                        case CharacterState.jump:
                        case CharacterState.leftjump:
                        case CharacterState.rightjump:
                        case CharacterState.highjump1:
                        case CharacterState.highjump2:
                            this._isActionNums = false;
                            this.PlayAnimator( CharacterState.sky );
                            this._changeMoveX = 0;
                        break;
                        case CharacterState.hurdle:
                            this._isActionNums = false;
                            this.PlayAnimator( CharacterState.sky );
                        break;
                        case CharacterState.huachan:
                        case CharacterState.climbend:
                            this._isActionNums = false;
                            this.PlayAnimator( CharacterState.run );
                            this.nowAction = CharacterState.run;
                            this._transform.position = new Laya.Vector3( this._transform.position.x,this._nowselectVec.y,this._transform.position.z );
                        break;
                    }
                }
            }
            if( this.buff_speeduptime > 0 )
            {
                this.buff_speeduptime -= distime;
                PropManager.Instance.SpeedUpEffect( "jiasulizi",this._transform.position );
                if( this.doublespeedup )
                {
                    PropManager.Instance.SpeedUpEffect1( "jiasulizi",this._transform.position );
                }
            }
            else
            {
                this._multiple.x = 1;
                this._multiple.y = 1;
                this._multiple.z = 1;
                this.doublespeedup = false;
                PropManager.Instance.CloseSpeedUpEffect( );
                PropManager.Instance.CloseSpeedUpEffect1( );
                
            }
            if( this.canshake )
            {
                this.shaketime+= distime;
                if( this.shaketime >= 0.5 )
                {
                    this.shaketime = 0;
                    WXGamePlatform.Instance.vibrateShort();
                }
            }
        }
        else 
        {
            if( this._ending )
            {
                var distime:number = dt/1000;
                this._moveTime += distime;
                if( this._moveTime >= this._endanmotime )
                {
                    this.GameOver();
                }
                else
                {
                    this._endAnmoSpeed1.x = this._endAnmoSpeed.x * distime;
                    this._endAnmoSpeed1.y = this._endAnmoSpeed.y * distime;
                    this._endAnmoSpeed1.z = this._endAnmoSpeed.z * distime;
                    ( PropManager.carma.getComponent( CameraManager ) as CameraManager ).GameEndRotate( this._transform.position, this._endAnmoSpeed1 );
                }
            }
        }
    }

   
    private _ending:boolean = false;
    private _endAnmoSpeed:Laya.Vector3 = new Laya.Vector3();
    private _endanmotime:number = 1;
    private _endAnmoSpeed1:Laya.Vector3 = new Laya.Vector3;
    private Win():void
    {
        AudioControl.Instance.playGameSound("over");
        DispatcherManager.dispatchEvent( GameState.end );
        var carmaTrans:Laya.Transform3D = PropManager.carma.transform;
        this._endAnmoSpeed.z = (this._transform.position.z - 2 - carmaTrans.position.z)/this._endanmotime;
        this._endAnmoSpeed.y = (this._transform.position.y - carmaTrans.position.y)/this._endanmotime;
        this._endAnmoSpeed.x = ( this._transform.position.x + 3 - carmaTrans.position.x ) / this._endanmotime;
        
        this._moveTime = 0;
        this._iscanMove = false;
        this._ending = true;
        this.PlayAnimator( CharacterState.sky );
        PropManager.Instance.GameOverEffecf( "yanhua",this._transform.position,GameStateControl.Instance.endGamePlayerArr.length == 0?true:false );
    }

    /**出现复活界面 */
    private Revive():void{
        AudioControl.Instance.playGameSound("over");
        GameState.isdie = true;
        DispatcherManager.dispatchEvent( GameState.revive );
        this._iscanMove = false;
        this.PlayAnimator( CharacterState.stand );
        // this._ending = true;
    }

    private Lose():void{
        AudioControl.Instance.playGameSound("over");
        GameState.isdie = true;
        DispatcherManager.dispatchEvent( GameState.lose );
        this._iscanMove = false;
        this.PlayAnimator( CharacterState.stand );
        this._ending = true;
    }

    private GameOver():void
    {
        this._ending = false;
        TimerService.Instance.removeUpdateTarget( this );
    }

    private _changeMoveX:number = 0;

    private isforceJump:boolean = false;

    private quaternion:Laya.Quaternion = new Laya.Quaternion; 
    

    public TestJump():void
    {
        this.nowAction = CharacterState.jump;
    }

    public destroy():void
    {
        TimerService.Instance.removeUpdateTarget( this );
        super.destroy();
    }

    public XMove( state:number ):void
    {
        if( this.nowAction == CharacterState.run )
        {
            this._moveTime = 0;
            this._speedMoveV3.x = state * 20;
            this.nowAction = CharacterState.MoveX;
        }
    }

    public dispose():void
    {
        TimerService.Instance.removeUpdateTarget( this );
    }

    

    
}