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 NpcCFG from "../CFG/NpcCFG";

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

    private _nowAction:string = "";

    private _animator:Laya.Animator;

    resouseName:string = "";

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

    /**是否发生碰撞 */
    private haveCollision:boolean = false;
    private _TargetCollision:string = "";
    private _normalizeVec:Laya.Vector3 = new Laya.Vector3;
    
    private _nowselectVec:Laya.Vector3;

    private staybuild:string = "";
    
    private stayscene:string = "";
    /**
     * 0:未锁，1：锁左边，2：锁右边
     */
    private _lockxstate:number = 0;

    constructor()
    {
        super();
    }

    public onAwake()
    {
        this._animator = (this.owner as Laya.Sprite3D).getComponent( Laya.Animator );
        PropManager.setAnimator( this._animator );
    }

    public onStart()
    {
        this._speedMoveV3 = new Laya.Vector3( 0,0,0 );
        this._transform = (this.owner as Laya.Sprite3D).transform;
        var sp:Laya.Sprite3D = this.owner as Laya.Sprite3D;
        (sp.getChildByName("lefttrail") as Laya.TrailSprite3D).active = false;
        (sp.getChildByName("righttrail") as Laya.TrailSprite3D ).active = false;
    }

    private _initPosVec:Laya.Vector3;
    /**
     * 状态，0：等待状态，1：开始状态，2：完成状态
     */
    private _nowRoleState:number = 0;

    public startRun():void
    {
        if( this._nowNpcinfo.gravity )
        {
            this.Gravity_g = this._nowNpcinfo.gravity;
        }
        else
        {
            this.Gravity_g = 10;
        }
        this._checktime = 500;
        this._nowRoleState = 1;
        this._ending = false;
        this._changeMoveX = 0;
        this._initPosVec = this._transform.position.clone();
        this.staybuild = "";
        this.stayscene = "";
        this._nowAction = CharacterState.stand;
        this.nowAction = CharacterState.run;
        this._speedMoveV3.x = 0;
        this._speedMoveV3.y = 0;
        this._speedMoveV3.z = -this.movespeed;
        this._iscanMove = true;
        TimerService.Instance.addUpdateTarget( this );
    }

    private _nowNpcinfo:NpcCFG;

    movespeed:number = 0;
    frontjumpspeed:Laya.Vector3 = new Laya.Vector3;
    leftjumpspeed:Laya.Vector3 = new Laya.Vector3;
    rightjumpspeed:Laya.Vector3 = new Laya.Vector3;
    climbspeed:number = 0;
    sideclimbspeed:Laya.Vector3 = new Laya.Vector3;

    public npcinfo( cfg:NpcCFG ):void
    {
        this._nowNpcinfo = cfg;
        this.movespeed = this._nowNpcinfo.speed;
        this.climbspeed = Number( this._nowNpcinfo.upclimb );
        this.frontjumpspeed = new Laya.Vector3(Number(this._nowNpcinfo.frontjump.x),Number( this._nowNpcinfo.frontjump.y),Number( this._nowNpcinfo.frontjump.z))
        this.leftjumpspeed = new Laya.Vector3(Number(this._nowNpcinfo.leftjump.x),Number( this._nowNpcinfo.leftjump.y),Number( this._nowNpcinfo.leftjump.z))
        this.rightjumpspeed = new Laya.Vector3(Number(this._nowNpcinfo.rightjump.x),Number( this._nowNpcinfo.rightjump.y),Number( this._nowNpcinfo.rightjump.z))
        this.sideclimbspeed = new Laya.Vector3(Number(this._nowNpcinfo.slideclimb.x),Number( this._nowNpcinfo.slideclimb.y),Number( this._nowNpcinfo.slideclimb.z))
        
        // this.frontjump.x = Number( this._nowNpcinfo.frontjump.x );
        // this.frontjump.y = Number( this._nowNpcinfo.frontjump.y );
        // this.frontjump.z = Number( this._nowNpcinfo.frontjump.z );

        // this.leftjump.x = Number( this._nowNpcinfo.leftjump.x );
        // this.leftjump.y = Number( this._nowNpcinfo.leftjump.y );
        // this.leftjump.z = Number( this._nowNpcinfo.leftjump.z );
        
        // this.rightjump.x = Number( this._nowNpcinfo.rightjump.x );
        // this.rightjump.y = Number( this._nowNpcinfo.rightjump.y );
        // this.rightjump.z = Number( this._nowNpcinfo.rightjump.z );

        // this.upclimb = Number( this._nowNpcinfo.upclimb );

        // this.slideclimb.x = Number( this._nowNpcinfo.slideclimb.x );
        // this.slideclimb.y = Number( this._nowNpcinfo.slideclimb.y );
        // this.slideclimb.z = Number( this._nowNpcinfo.slideclimb.z );
    }
    
    /**
     * 开始碰撞时执行
     */
    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);
        }
    }

    

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

    private _lockbyBuildname:string = "";
    /**
     * 0:未被锁，1：锁定左边，2：锁定右边，
     */
    private _lockstate:number = 0;
    private Calculate( normal:Laya.Vector3,target:Laya.Node ):void
    {
        if( target.name == "SliderBoost" || target.name.indexOf( "npc" ) != -1 || target.name.indexOf( "role" ) != -1 )
        {
            return;
        }
        this._TargetCollision = target.name;
        var n:Laya.Vector3 = new Laya.Vector3;
        Laya.Vector3.normalize(normal,this._normalizeVec);
        if( this._TargetCollision.indexOf( "build" ) != -1 || this._TargetCollision.indexOf( "Scaffolding" ) != -1 ||
        this._TargetCollision.indexOf( "irongauze" ) != -1 ) //碰到建筑，判断是上爬，左爬还是右爬
        {
            if( this._TargetCollision.indexOf( "build" ) != -1 )
            {
                this.staybuild = this._TargetCollision;
                this.stayscene = target.parent.parent.name;
            }
            if( this._TargetCollision.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._TargetCollision );
            } 
            if( this._normalizeVec.z == -1 ) //上爬
            {
                if( this._nowselectVec && this._transform.position.y > (this._nowselectVec.y - 0.1)  )
                {
                    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._TargetCollision.indexOf( "panel_jump" ) != -1 ) //加速板跳跃
        {
            this.nowAction = CharacterState.speedupjump;
            // this.setJump( this.nowAction );
        }
        else if( this._TargetCollision.indexOf("panel_downslip") != -1 )
        {
            this.nowAction = CharacterState.downmove;
            // this.setJump( this.nowAction );
        }
        else if( this._TargetCollision.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._TargetCollision.indexOf( "box_sidejump" ) != -1 )
        {
            this.nowAction = CharacterState.sidespeedup;
            // this.setJump( this.nowAction );
        }
        else if( this._TargetCollision.indexOf( "box_Surmount" ) != -1 )
        {
            this.nowAction = CharacterState.cross;
            // this.setJump( this.nowAction );
        }
        else if( this._TargetCollision.indexOf( "irongauze" ) != -1 )
        {
            this.nowAction = CharacterState.climbup;
            this._shangpaVec = this._nowselectVec.clone();
        }
    }
    /**
     * 持续碰撞时执行
     */
    public  onCollisionStay(collision:Laya.Collision):void 
    {
        // Debuger.log( "碰撞中" );
        if( collision && collision.contacts && collision.contacts.length > 0 && collision.other )
        {
            this.haveCollision = 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 && targetname.indexOf( "role" ) == -1 )
            {
                this._TargetCollision = targetname;
            }
            if( this._TargetCollision.indexOf( "build" ) != -1 || this._TargetCollision.indexOf( "Scaffolding" ) != -1 ||
                this._TargetCollision.indexOf( "irongauze" ) != -1 ) //碰到建筑，判断是上爬，左爬还是右爬
            {
                if( this._TargetCollision.indexOf( "build" ) != -1 )
                {
                    this.staybuild = this._TargetCollision;
                    this.stayscene = target.parent.parent.name;
                }

                if( this._TargetCollision.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._TargetCollision );
                } 
            }
        }
    }
    /**
     * 结束碰撞时执行
     */
    public onCollisionExit(collision:Laya.Collision):void 
    {
        this.haveCollision = 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 || target.name.indexOf( "role" ) != -1 )
        {
            return;
        }
        if( this.HaveForceJump )
        {
            this.HaveForceJump = false;
            return;
        }
        if( this._lockxstate )
        {
            this._lockxstate = 0;
            return;
        }
        if( this._TargetCollision != target.name )
        {
            return;
        }
        if( target.name.indexOf( "panel_jump" ) != -1 )
        {
            return;
        }
        switch( this.nowAction )
        {
            case CharacterState.climbup:
            case CharacterState.climbupblock:
                if( this._TargetCollision.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;
        }
    }

    private JumpAction( value:number,isquanzhong:boolean = false ):void
    {
        if( value > 0 )
        {
            this.nowAction = CharacterState.jumpright;
        }
        else if( value < 0 )
        {
            this.nowAction = CharacterState.jumpleft;
        }
        else
        {
            if( isquanzhong )
            {
                if( this._TargetCollision.indexOf( "build" ) != -1 ) 
                {
                    var sort:number = GameUtils.randRangeint( 0,100 );
                    var allvalue:number = 0;
                    var sort:number;
                    var selectdir:string = "";
                    if( this._TargetCollision.indexOf( "leftright" ) != -1 )
                    {
                        allvalue = this._nowNpcinfo.jump_left + this._nowNpcinfo.jump_right;
                        sort = GameUtils.randRangeint( 0,allvalue );
                        if( sort <= this._nowNpcinfo.jump_left )
                        {
                            selectdir = "left";
                        }
                        else
                        {
                            selectdir = "right";
                        }
                    }
                    else if( this._TargetCollision.indexOf( "frontright" ) != -1 )
                    {
                        allvalue = this._nowNpcinfo.start_ahead + this._nowNpcinfo.jump_right;
                        sort = GameUtils.randRangeint( 0,allvalue );
                        if( sort <= this._nowNpcinfo.start_ahead )
                        {
                            selectdir = "front";
                        }
                        else
                        {
                            selectdir = "right";
                        }
                    }
                    else if( this._TargetCollision.indexOf( "frontleft" ) != -1 )
                    {
                        allvalue = this._nowNpcinfo.start_ahead + this._nowNpcinfo.jump_left;
                        sort = GameUtils.randRangeint( 0,allvalue );
                        if( sort <= this._nowNpcinfo.start_ahead )
                        {
                            selectdir = "front";
                        }
                        else
                        {
                            selectdir = "left";
                        }
                    }
                    else if( this._TargetCollision.indexOf( "right" ) != -1 )
                    {
                        selectdir = "right";
                    }
                    else if( this._TargetCollision.indexOf( "left" ) != -1 )
                    {
                        selectdir = "left";
                    }
                    else if( this._TargetCollision.indexOf( "front" ) != -1 )
                    {
                        selectdir = "front";
                    }
                    else
                    {
                        allvalue = this._nowNpcinfo.start_ahead + this._nowNpcinfo.jump_left + this._nowNpcinfo.jump_right;
                        sort = GameUtils.randRangeint( 0,allvalue );
                        if( sort <= this._nowNpcinfo.start_ahead )
                        {
                            selectdir = "front";
                        }
                        else if( sort <= this._nowNpcinfo.start_ahead + this._nowNpcinfo.jump_left )
                        {
                            selectdir = "left";
                        }
                        else
                        {
                            selectdir = "right";
                        }
                    }
                    switch( selectdir )
                    {
                        case "left":
                            this.nowAction = CharacterState.jumpleft;
                        break;
                        case "right":
                            this.nowAction = CharacterState.jumpright;
                        break;
                        case "front":
                            this.nowAction = CharacterState.jump;
                        break;
                        default:
                            this.nowAction = CharacterState.jump;
                        break;
                    }
                }
                else
                {
                    this.nowAction = CharacterState.jump;
                }
            }
            else
            {
                this.nowAction = CharacterState.jump;
            }
        }
    }


    private LastAction:string = "";
    private _moveTime:number = 0;
    private _chixuTime:number = 0;

    private _isActionNums:boolean = false;
    private _isneedactionnum:number = 0;
    private _needactiontime:number = 0;

    public set nowAction( value:string )
    {
        if( this._nowRoleState == 1 )
        {
            if( this._nowAction != value || this.LastAction == CharacterState.speedupjump||this.LastAction == CharacterState.jumpright )
            {
                this.LastAction = this._nowAction;
                this._nowAction = value;
                this._moveTime = 0;
                switch( this._nowAction )
                {
                    case CharacterState.stand:
                        this._isActionNums = false;
                        this.PlayAnimator( CharacterState.stand );
                    break;
                    case CharacterState.run:
                        if( this.LastAction == CharacterState.jump || this.LastAction == CharacterState.jumpleft ||this.LastAction == 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 );
                        this._isActionNums = true;
                        this._isneedactionnum = 1;
                        this.jumpSpeed.x = this.frontjumpspeed.x;
                        this.jumpSpeed.y = this.frontjumpspeed.y;
                        this.jumpSpeed.z = this.frontjumpspeed.z;
                    break;
                    case CharacterState.jumpleft:
                        this.jumpSpeed = this.leftjumpspeed;
                        // this.jumpSpeed.x = this.leftjumpspeed.x;
                        // this.jumpSpeed.y = this.leftjumpspeed.y;
                        // this.jumpSpeed.z = this.leftjumpspeed.z;
                        this._isActionNums = true;
                        this._isneedactionnum = 1;
                        this.PlayAnimator( CharacterState.leftjump );
                    break;
                    case CharacterState.jumpright:
                        this.jumpSpeed = this.rightjumpspeed;
                        // this.jumpSpeed.x = this.rightjumpspeed.x;
                        // this.jumpSpeed.y = this.rightjumpspeed.y;
                        // this.jumpSpeed.z = this.rightjumpspeed.z;
                        this._isActionNums = true;
                        this._isneedactionnum = 1;
                        this.PlayAnimator( CharacterState.rightjump );
                    break;
                    case CharacterState.speedupjump:
                        var buildcfg:BuildCFG = BuildCFG.dict[ "panel_jump" ];
                        this.jumpSpeed.x = buildcfg.buffx;
                        this.jumpSpeed.y = buildcfg.buffy;
                        if( this.LastAction == CharacterState.speedupjump )
                        {
                            this.jumpSpeed.z = this.jumpSpeed.z*buildcfg.buffz;
                        }
                        else
                        {
                            this.jumpSpeed.z = this._speedMoveV3.z*buildcfg.buffz;
                        }
                        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 );
                        }
                    break;
                    case CharacterState.downmove:
                        var buildcfg:BuildCFG = BuildCFG.dict[ "panel_downslip" ];
                        this.jumpSpeed.x = buildcfg.buffx;
                        this.jumpSpeed.y = buildcfg.buffy;
                        this.jumpSpeed.z = this._speedMoveV3.z*buildcfg.buffz;
                        this._chixuTime = buildcfg.time;
                        this._isActionNums = true;
                        this._isneedactionnum = 1;
                        this.PlayAnimator( CharacterState.huachan );
                    break;
                    case CharacterState.cross:
                        var buildcfg:BuildCFG = BuildCFG.dict[ "box_Surmount" ];
                        this.jumpSpeed.x = buildcfg.buffx;
                        this.jumpSpeed.y = buildcfg.buffy+0.5;
                        this.jumpSpeed.z = this._speedMoveV3.z*buildcfg.buffz;
                        this._chixuTime = buildcfg.time;
                        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.climbspeed;
                        this.jumpSpeed.z = 0;
                        this._isActionNums = false;
                        this.PlayAnimator( CharacterState.climbwall );
                    break;
                    case CharacterState.climbleft:
                           this.jumpSpeed = new Laya.Vector3(0,this.sideclimbspeed.y,this.sideclimbspeed.z)
                        // this.jumpSpeed.x = 0;
                        // this.jumpSpeed.y = this.sideclimbspeed.y;
                        // this.jumpSpeed.z = this.sideclimbspeed.z;
                        this._isActionNums = false;
                        this.PlayAnimator( CharacterState.runwallleft );
                    break;
                    case CharacterState.climbright:
                        this.jumpSpeed = new Laya.Vector3(0,this.sideclimbspeed.y,this.sideclimbspeed.z)
                        // this.jumpSpeed.x = 0;
                        // this.jumpSpeed.y = this.sideclimbspeed.y;
                        // this.jumpSpeed.z = this.sideclimbspeed.z;
                        this._isActionNums = false;
                        this.PlayAnimator( CharacterState.runwallright );
                    break;
                    case CharacterState.ClimbMotion:
                        this._isActionNums = true;
                        this._isneedactionnum = 0;
                        this._needactiontime = 0.3;
                        this._moveTime = 0;
                        this.PlayAnimator( CharacterState.climbend );
                    break;
                }
            }
        }
    }

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

    private _moveVec:Laya.Vector3 = new Laya.Vector3;
    private jumpSpeed:Laya.Vector3 = new Laya.Vector3;
    private jumpHight:number = 0;
    private _disy:number = 0;
    public Gravity_g:number=10;//重力加速度
    private _checktime:number = 0;
    public update( dt:number ):void
    {
        if( this._nowRoleState == 1 )
        {
            dt = 16;
            this._checktime += dt;
            if( PropManager.iscarmanpc )
            {

            }
            else
            {
                if( this._checktime >= 500 )
                {
                    this._checktime = 0;
                    if( this._nowNpcinfo && PropManager.mainRole )
                    {
                        if( this._transform.position.z + this._nowNpcinfo.distance < PropManager.mainRole.transform.position.z )
                        {
                            this._iscanMove = false;
                        }
                        else
                        {
                            this._iscanMove = true;
                        }
                    }
                }
            }
            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;
                        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.Gravity_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.Gravity_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);
                        if( this._moveTime > this._chixuTime )
                        {
                            this.nowAction = CharacterState.run;
                        }
                    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._moveTime > this._chixuTime && this.LastAction==CharacterState.climbleft || this.LastAction == CharacterState.climbright )
                        {
                            this.nowAction = this.LastAction;
                        }
                        else
                        {
                            this.nowAction = CharacterState.jump;
                            // this.setJump( this.nowAction );
                        }
                    break;
                    case CharacterState.cross:
                        this._disy = this.jumpSpeed.y - this.Gravity_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.haveCollision )
                {
                    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 )//角色死亡
                {
                    if( this.stayscene == "" || this.staybuild == "" )
                    {
                        this._transform.position = this._initPosVec.clone();
                    }
                    else
                    {
                        this._transform.position = PropManager.getPosVecbyScene( this.stayscene,this.staybuild ).clone();
                    }
                    this.nowAction = CharacterState.run;
                }
                if( this._transform.position.z < PropManager.gamewinPosZ )
                {
                    this.Win();
                }
                if( this._isActionNums )
                {
                    //如果当前播放state已经播放完了一次
                    if( this._isneedactionnum > 0 )
                    {
                        if(this._animator.getCurrentAnimatorPlayState().normalizedTime >= this._isneedactionnum)
                        {
                            switch( this._nowActionName )
                            {
                                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:
                                case CharacterState.land2:
                                    this._isActionNums = false;
                                    this.PlayAnimator( CharacterState.run );
                                break;
                            }
                        } 
                    }
                    else if( this._needactiontime != 0 && this._moveTime > this._needactiontime )
                    {
                        switch( this._nowActionName )
                            {
                                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;
                            }
                    }
                }
            }
            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 GameOver():void
    {
        this._ending = false;
        TimerService.Instance.removeUpdateTarget( this );
        DispatcherManager.dispatchEvent( GameState.end );
    }

    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
    {
        TimerService.Instance.removeUpdateTarget( this );
        this._nowRoleState = 2;
        this.PlayAnimator( CharacterState.sky );
        DispatcherManager.dispatchEvent( GameState.npcend,this.owner.name );
    }
    public stop():void
    {
        TimerService.Instance.removeUpdateTarget( this );
    }
    public destroy():void
    {
        TimerService.Instance.removeUpdateTarget( this );
        super.destroy();
    }

    private _changeMoveX:number = 0;

    private HaveForceJump:boolean = false;

    private quaternion:Laya.Quaternion = new Laya.Quaternion; 
    public Joy( value:number,angle:number ):void
    {
        if( this.nowAction == CharacterState.run || this.nowAction == CharacterState.climbup || CharacterState.climbleft || CharacterState.climbright )
        {
            this._changeMoveX = value;
        }
        if( this.nowAction == CharacterState.climbleft && this._changeMoveX > 0 ) //强制跳跃
        {
            this.HaveForceJump = true;
            this.nowAction = CharacterState.jumpright;
        }
        else if( this.nowAction == CharacterState.climbright && this._changeMoveX < 0 )
        {
            this.HaveForceJump = 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 );
            Laya.Vector3.transformQuat( this.jumpSpeed,this.quaternion,this.jumpSpeed );
    }

    private _nowActionName:string;
    public PlayAnimator( actionName:string ):void
    {
        // this._nowActionName = actionName;
        var _actionname = this.TransitionActionName(actionName);
        this._nowActionName = actionName;
        if( this._animator )
        {
            if( this._nowActionName == CharacterState.climbend )
            {
                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 TestJump():void
    {
        this.nowAction = CharacterState.jump;
    }


    public getPostion():Laya.Vector3
    {
        return this._transform.position;
    }

    public getAction():number
    {
        return this._nowRoleState;
    }

   
}