import { LayoutControl } from "./LayoutControl";
import { AssetPath } from "../Config/AssetPath";
import {CameraManager} from "../Game/CameraManager";
import {PropManager} from "../PrefabControl/PropManager";
import {Player} from "../Game/Player";
import { DispatcherManager } from "../utils/DispatcherManager";
import {GameState} from "../utils/GameState";
import LevelCFG from "../CFG/LevelCFG";
import {AgencyUserMessage} from "../Agency/AgencyUserMessage";
import {PanelResDic} from "../PrefabControl/PanelResDic";
import {RoleData} from "../Data/RoleData";
import { GameUtils } from "../utils/GameUtils";
import NpcCFG from "../CFG/NpcCFG";
import {NpcScript} from "../Game/NpcScript";
import {ResourceControl} from "./ResourceControl";
import GameCFG from "../CFG/GameCFG";
import {CharacterState} from "../PrefabControl/CharacterState";
import {UserInfoEvent} from "../Data/UserInfoEvent";
import { Global } from "../Global";
import {CharacterPool} from "../PrefabControl/CharacterPool";
import RoleCFG from "../CFG/RoleCFG";
import { ViewManager } from "../View/ViewManager";
import { LayOUT } from "../utils/LayOUT";


import Sprite = Laya.Sprite;
import Stage = Laya.Stage;
import Event = Laya.Event;
import Browser = Laya.Browser;
import WebGL = Laya.WebGL;
export class SceneControl
{
    private static ins:SceneControl;

    private _initPosY:number = 1;

    public cameraScript:CameraManager;
    public PlayerScript:Player;

    constructor(){
        SceneControl.ins = this;
    }

    /*使用界面显示，0：砸蛋,1:宝箱*/
    static EggGGroup:number=0;

    /**签到是否正在显示 */
    static IsShowSign:boolean = false;

    public static get Instance():SceneControl
    {
        if( SceneControl.ins == null )
        {
            return new SceneControl();
        }
        return SceneControl.ins;
    }

    //创建主角
    public CreatePlayer( rolecfg:RoleCFG ):void
    {
        var iscreate:boolean = false;
        if( !PropManager.mainRole )
        {
            iscreate = true;
        }
        else if( PropManager.mainRole.name != "Character" + rolecfg.name )
        {
            iscreate = true;
            this.ClearMomentCharacter();
        }
        if( iscreate )
        {
            // var sp:Laya.Sprite3D = Laya.Loader.getRes(AssetPath.get3DRoleModel(RoleData.roleCfg.name)) as Laya.Sprite3D;
            // PropManager.mainRole = Laya.Sprite3D.instantiate(sp, PropManager.scene3D, false,new Laya.Vector3( 0,0,0 )) as Laya.Sprite3D;
            PropManager.mainRole = CharacterPool.AddCharacter( rolecfg.name,Player,PropManager.scene3D );
            PropManager.mainRole.name = "Character" + rolecfg.name;
            this.PlayerScript = PropManager.mainRole.getComponent( Player );
            
            // this.mainRoleScript.resouseName = RoleData.roleCfg.name;
            // if(  )
            // this.mainRoleScript = PropManager.mainRole.addComponent( MainRAoleScript );
            
        }
        var agencyusermessage:AgencyUserMessage = PanelResDic.Instance.getSingleTon( AgencyUserMessage );
        var RoleData:RoleData = agencyusermessage.RoleData;
        var info:any = RoleData.levelcfg.roleinitpos;
        var chidarr:Array<string> = GameUtils.cutstrtoArr(info.offsetPos);
        var offsetPos:Laya.Vector3 = new Laya.Vector3( Number( chidarr[0]),Number( chidarr[1]),Number( chidarr[2]) );
        var posVec:Laya.Vector3 = new Laya.Vector3;
        var selectVec:Laya.Vector3;
        if( info.initbuild == "startpanel" )
        {
            posVec.x = PropManager.startPlane.transform.position.x + offsetPos.x;
            posVec.y = PropManager.startPlane.transform.position.y + offsetPos.y;
            posVec.z = PropManager.startPlane.transform.position.z + offsetPos.z;
            selectVec = ( PropManager.startPlane.getChildByName( "child_pos" ) as Laya.Sprite3D ).transform.position;
        }
        else
        {
            var childpos:Laya.Vector3 = PropManager.getPosVecbyScene( RoleData.levelmapArr[0],info.initbuild );
            posVec.x = childpos.x + offsetPos.x;
            posVec.y = childpos.y + offsetPos.y;
            posVec.z = childpos.z + offsetPos.z;
            selectVec = childpos;
        }
        PropManager.mainRole.transform.position = posVec;
        this.cameraScript.SetCameraPosition(PropManager.mainRole.transform.position);
        this.PlayerScript.setnowSelectVec( selectVec );
        this.PlayerScript.nowAction = CharacterState.stand;
        this.PlayerScript.PlayAnimator( CharacterState.stand );

        // var UIPos = PropManager.mainRole.transform.position;
        // var NAMEUI = new Laya.Text();

        // NAMEUI.text = "自己";
        // NAMEUI.fontSize = 60;
        // NAMEUI.color = "#ffffff";
        // NAMEUI.scaleX = 0.5;
        // NAMEUI.scaleY = 0.5;
        // NAMEUI.align = "center";
        // NAMEUI.valign = "middle";
        // Laya.stage.addChild(NAMEUI);
        // Laya.timer.frameLoop(1, this, () => {
        //     let ScreenPos = new Laya.Vector3();
        //     PropManager.carma.worldToViewportPoint(UIPos, ScreenPos);
        //     NAMEUI.pos(ScreenPos.x, ScreenPos.y);
        // })
    }
    //创建npc
    private createNpc( ):void
    {
        console.log("CreatNpc---");
        this.ClearNpc();
        var agencyusermessage:AgencyUserMessage = PanelResDic.Instance.getSingleTon( AgencyUserMessage );
        var RoleData:RoleData = agencyusermessage.RoleData;
        var arr:Array<any> = RoleData.levelcfg.npc;
        var childdict:any;
        var index:number = 0;
        PropManager.npcnum = arr.length;
        for( var s in arr )
        {
            childdict = arr[s];
            var selectVec:Laya.Vector3 = null;
            var npccfg:NpcCFG = NpcCFG.dict[ childdict.id ];
            var sp:Laya.Sprite3D = Laya.Loader.getRes(AssetPath.get3DRoleModel(npccfg.name)) as Laya.Sprite3D;
            var chidarr:Array<string> = GameUtils.cutstrtoArr(childdict.offsetPos);
            var offsetPos:Laya.Vector3 = new Laya.Vector3( Number( chidarr[0]),Number( chidarr[1]),Number( chidarr[2]) );
            var posVec:Laya.Vector3 = new Laya.Vector3;
            if( childdict.scene == "startpanel" )
            {
                posVec.x = PropManager.startPlane.transform.position.x + offsetPos.x;
                posVec.y = PropManager.startPlane.transform.position.y + 1 + offsetPos.y;
                posVec.z = PropManager.startPlane.transform.position.z + offsetPos.z;
                selectVec = ( PropManager.startPlane.getChildByName( "child_pos" ) as Laya.Sprite3D ).transform.position;
            }
            else
            {
                var childpos:Laya.Vector3 = PropManager.getPosVecbyScene(childdict.scene ,childdict.initbuild );
                posVec.x = childpos.x + offsetPos.x;
                posVec.y = childpos.y + offsetPos.y;
                posVec.z = childpos.z + offsetPos.z;
                selectVec = childpos;
            }

            var npcsp:Laya.Sprite3D = CharacterPool.AddCharacter( npccfg.name,NpcScript,PropManager.scene3D );
            npcsp.name = "npc_" + index;
            npcsp.transform.position = posVec;
            var npcscript:NpcScript = npcsp.getComponent( NpcScript );
            npcscript.setnowSelectVec( selectVec );
            npcscript.npcinfo( npccfg );
            PropManager.npcDict[ npcsp.name ] = npcsp;
            PropManager.npcscriptDict[npcsp.name] = npcscript;
            npcscript.PlayAnimator( CharacterState.stand );
            index++;
            // this.ShowUI(npcsp);
            
        }
        console.log("CreatNpc2---");
        
    }

    index = 0;
    ShowUI(_sprite:Laya.Sprite3D){
        var UIPos = _sprite.transform.position;
        var NAMEUI = new Laya.Text();
        let random = GameUtils.randRangeint(1,30);
       
        NAMEUI.text = "npc_"+ this.index;
        NAMEUI.fontSize = 60;
        NAMEUI.color = "#ffffff";
        NAMEUI.scaleX = 0.5;
        NAMEUI.scaleY = 0.5;
        NAMEUI.align = "center";
        NAMEUI.valign = "middle";
        Laya.stage.addChild(NAMEUI);
        Laya.timer.frameLoop(1, this, () => {
            let ScreenPos = new Laya.Vector3();
            PropManager.carma.worldToViewportPoint(UIPos, ScreenPos);
            NAMEUI.pos(ScreenPos.x, ScreenPos.y+20);
        })
        this.index++;
    }
    

    public ShowTheGameScene():void
    {
        var pathArr:Array<any> = [];
        var urlArr:Array<string> = [];
        this.cameraScript.iscanfollow = true;
        var roleData:RoleData = ( PanelResDic.Instance.getSingleTon( AgencyUserMessage ) as AgencyUserMessage ).RoleData;
        var arr:Array<string> = roleData.levelmapArr;
        var i:number = 0;
        for( i = 0;i < arr.length;i++ )
        {
            if( urlArr.indexOf( AssetPath.get3DModel( arr[i] ) ) == -1 )
            {
                urlArr.push( AssetPath.get3DModel( arr[i] ));
            }
        }
        var npcArr:Array<any> = roleData.levelcfg.npc;
        for( i = 0;i < npcArr.length;i++ )
        {
            if( urlArr.indexOf( AssetPath.get3DRoleModel( NpcCFG.dict[ npcArr[i].id ].name ) ) == -1 )
            {
                urlArr.push( AssetPath.get3DRoleModel( NpcCFG.dict[ npcArr[i].id ].name ) );
            }
        }
        for( i = 0;i < urlArr.length;i++ )
        {
            pathArr.push( { url:urlArr[i] } );
        }
        ResourceControl.Instance.loader3DArr( pathArr,this,this.SceneCreatComplete,null,true );
    }

    private SceneCreatComplete():void
    {
        this.RefreshTerrain();
        var agencyusermessage:AgencyUserMessage = PanelResDic.Instance.getSingleTon( AgencyUserMessage );
        var RoleData:RoleData = agencyusermessage.RoleData;
        this.CreatePlayer( RoleData.roleCfg );
        this.createNpc();
    }

    private ListenOnEvent():void
    {
        DispatcherManager.addEventListener( GameState.start,this,this.EnterGame ); 
        DispatcherManager.addEventListener( GameState.jump,this,this.Jump );
        DispatcherManager.addEventListener( UserInfoEvent.userinfoEvent_updateroleid,this,this.RefreshCharacterModel );
    }

   

    private CharacterLoadEnd( url:string ):void
    {
        var agencyusermessage:AgencyUserMessage = PanelResDic.Instance.getSingleTon( AgencyUserMessage );
        var RoleData:RoleData = agencyusermessage.RoleData;
        if( AssetPath.get3DModel( RoleData.roleCfg.name ) == url )
        {
            this.CreatePlayer( RoleData.roleCfg );
        }
    }

    private Jump():void
    {
        this.PlayerScript.TestJump();
    }

    /************************************************************** */
    private setup(): void {
        this.createSprite();

        Laya.stage.on(Event.MOUSE_UP, this, this.onMouseUp);
        Laya.stage.on(Event.MOUSE_OUT, this, this.onMouseUp);
    }

    private sp: Sprite;
    private preRadian: number = 0;
    private createSprite(): void {
        this.sp = new Sprite();
        var w: number = 200, h: number = 300;
        this.sp.graphics.drawRect(0, 0, w, h, "#FF7F50");
        this.sp.size(w, h);
        this.sp.pivot(w / 2, h / 2);
        this.sp.pos(Laya.stage.width / 2, Laya.stage.height / 2);
        Laya.stage.addChild(this.sp);

        this.sp.on(Event.MOUSE_DOWN, this, this.onMouseDown);
    }

    private onMouseDown(e: Event): void {
        var touches: Array<any> = e.touches;

        if (touches && touches.length == 2) {
            this.preRadian = Math.atan2(
                touches[0].stageY - touches[1].stageY,
                touches[0].stageX - touches[1].stageX);

            Laya.stage.on(Event.MOUSE_MOVE, this, this.onMouseMove);
        }
    }

    private onMouseMove(e: Event): void {
        var touches: Array<any> = e.touches;
        if (touches && touches.length == 2)
        {
            var nowRadian: number = Math.atan2(
                touches[0].stageY - touches[1].stageY,
                touches[0].stageX - touches[1].stageX);

            this.sp.rotation += 180 / Math.PI * (nowRadian - this.preRadian);

            this.preRadian = nowRadian;
        }
    }

    private onMouseUp(e: Event): void {
        Laya.stage.off(Event.MOUSE_MOVE, this, this.onMouseMove);
    }
    /********************************** */


    private EnterGame(jiasu?):void
    {
        this.PlayerScript.startRun(jiasu);
        var npcdict:any = PropManager.npcscriptDict;
        for( var s in npcdict )
        {
            var npcscript:NpcScript = npcdict[s];
            npcscript.startRun();
        }
    }

    private ClearMomentCharacter():void
    {
        if( PropManager.mainRole && this.PlayerScript )
        {
            CharacterPool.ClearCharacter( PropManager.mainRole,this.PlayerScript.resouseName,this.PlayerScript );
            this.PlayerScript = null;
            PropManager.mainRole = null;
        }
    }

    public SetScene():void
    {
        var backgroundParent:Laya.Sprite = LayoutControl.Instance.background.displayObject;
        var scenebg:fairygui.GComponent = fairygui.UIPackage.createObject( "main","gambg" ) as fairygui.GComponent;
        scenebg.setSize(fairygui.GRoot.inst.width, fairygui.GRoot.inst.height);
        LayoutControl.Instance.background.addChild( scenebg );
        // UiManager.instance.add( GameBgView,null,LayerNotify.LAYER_BACKGROUND );

        //添加3D场景
        PropManager.scene3D = backgroundParent.addChild(new Laya.Scene3D()) as Laya.Scene3D;
        PropManager.scene3D.physicsSimulation.continuousCollisionDetection = true;

        //添加照相机
        PropManager.carma = (PropManager.scene3D.addChild(new Laya.Camera(0, 0.1, 70))) as Laya.Camera;
        this.cameraScript = PropManager.carma.addComponent( CameraManager );

        PropManager.carma.clearFlag = Laya.BaseCamera.CLEARFLAG_DEPTHONLY;
        
        //添加方向光
        var directionLight: Laya.DirectionLight = Laya.Loader.getRes(AssetPath.get3DModel("DirectionalLight")) as Laya.DirectionLight;//PropManager.scene3D.addChild(new Laya.DirectionLight()) as Laya.DirectionLight;
        PropManager.scene3D.addChild( directionLight );
        // directionLight.transform.worldMatrix.setForward(new Laya.Vector3(1, -1, 0));

        // var directionLight: Laya.DirectionLight = PropManager.scene3D.addChild(new Laya.DirectionLight()) as Laya.DirectionLight;
        // directionLight.color = new Laya.Vector3(1,1,1);
        // directionLight.intensity = 0.5;
        // directionLight.transform.worldMatrix.setForward(new Laya.Vector3(1, -1, 0));

        PropManager.startPlane = Laya.Loader.getRes(AssetPath.get3DModel("startpanel"));
        PropManager.scene3D.addChild(PropManager.startPlane);

        PropManager.endPlane = Laya.Loader.getRes(AssetPath.get3DModel("endpanel"));
        PropManager.scene3D.addChild(PropManager.endPlane);

        // PropManager.bgsprite3d = Laya.Loader.getRes(AssetPath.get3DModel("distantview"));
        // PropManager.scene3D.addChild(PropManager.bgsprite3d);

        //雾化代码
        PropManager.scene3D.enableFog = true;
        //设置雾化的颜色
        var fogArr:Array<string> = GameUtils.cutstrtoArr( GameCFG.dict[2].value );
        if( GameCFG.dict[8] )
        {
            var wucolor:Array<string> = GameUtils.cutstrtoArr( GameCFG.dict[8].value );
            PropManager.scene3D.fogColor = new Laya.Vector3(Number(wucolor[0]),Number(wucolor[1]),Number(wucolor[2]));
        }
        else
        {
            PropManager.scene3D.fogColor = new Laya.Vector3(0,0,0.6);
        }
        //设置雾化的起始位置，相对于相机的距离
        var valueArr:Array<string> = GameUtils.cutstrtoArr( GameCFG.dict[3].value );
        PropManager.scene3D.fogStart = Number( valueArr[0] );
        //设置雾化最浓处的距离。
        PropManager.scene3D.fogRange = Number( valueArr[1] );
        this.ShowTheGameScene();
        this.ListenOnEvent();
        
    }


    /**清除所有NPC */
    private ClearNpc():void
    {
        var npcdict:any = PropManager.npcDict;
        var s;
        for( s in npcdict )
        {
            var sp:Laya.MeshSprite3D = npcdict[s];
            var npcscript:NpcScript = PropManager.npcscriptDict[s];
            var resousename:string = npcscript.resouseName;
            CharacterPool.ClearCharacter( sp,resousename,npcscript);
            delete PropManager.npcscriptDict[s];
            delete npcdict[s];
        }
    }

    private Pattern( mapSprite3D:Laya.Sprite3D ):void
    {
        console.log("Pattern1----");
        if( PropManager.patternDict[ mapSprite3D.name ] )
        {
            delete PropManager.patternDict[ mapSprite3D.name ];
        }
        var BlocksSprite:Laya.Sprite3D = mapSprite3D.getChildByName( "Blocks" ) as Laya.Sprite3D;
        
        var chiddict:any = {};
        var chidsp:Laya.Sprite3D;
        var buildSp:Laya.MeshSprite3D;
        var index:number = 0;
        while( BlocksSprite.getChildByName( "build_all_" + index ) )
        {
            buildSp = BlocksSprite.getChildByName( "build_all_" + index ) as Laya.MeshSprite3D;
            chidsp = buildSp.getChildByName("child_pos") as Laya.Sprite3D;
            chiddict[ "build_all_" + index ] = chidsp.transform.position;
            if( PropManager.diePosY > buildSp.transform.position.y )
            {
                PropManager.diePosY = buildSp.transform.position.y;
            }
            index++;
        }
        index = 0;
        while( BlocksSprite.getChildByName( "build_front_" + index ) )
        {
            buildSp = BlocksSprite.getChildByName( "build_front_" + index ) as Laya.MeshSprite3D;
            chidsp = buildSp.getChildByName("child_pos") as Laya.Sprite3D;
            chiddict[ "build_front_" + index ] = chidsp.transform.position;
            if( PropManager.diePosY > buildSp.transform.position.y )
            {
                PropManager.diePosY = buildSp.transform.position.y;
            }
            index++;
        }
        index = 0;
        while( BlocksSprite.getChildByName( "build_left_" + index ) )
        {
            buildSp = BlocksSprite.getChildByName( "build_left_" + index ) as Laya.MeshSprite3D;
            chidsp = buildSp.getChildByName("child_pos") as Laya.Sprite3D;
            chiddict[ "build_left_" + index ] = chidsp.transform.position;
            if( PropManager.diePosY > buildSp.transform.position.y )
            {
                PropManager.diePosY = buildSp.transform.position.y;
            }
            index++;
        }
        index = 0;
        while( BlocksSprite.getChildByName( "build_right_" + index ) )
        {
            buildSp = BlocksSprite.getChildByName( "build_right_" + index ) as Laya.MeshSprite3D;
            chidsp = buildSp.getChildByName("child_pos") as Laya.Sprite3D;
            chiddict[ "build_right_" + index ] = chidsp.transform.position;
            if( PropManager.diePosY > buildSp.transform.position.y )
            {
                PropManager.diePosY = buildSp.transform.position.y;
            }
            index++;
        }
        index = 0;
        while( BlocksSprite.getChildByName( "build_frontleft_" + index ) )
        {
            buildSp = BlocksSprite.getChildByName( "build_frontleft_" + index ) as Laya.MeshSprite3D;
            chidsp = buildSp.getChildByName("child_pos") as Laya.Sprite3D;
            chiddict[ "build_frontleft_" + index ] = chidsp.transform.position;
            if( PropManager.diePosY > buildSp.transform.position.y )
            {
                PropManager.diePosY = buildSp.transform.position.y;
            }
            index++;
        }
        index = 0;
        while( BlocksSprite.getChildByName( "build_frontright_" + index ) )
        {
            buildSp = BlocksSprite.getChildByName( "build_frontright_" + index ) as Laya.MeshSprite3D;
            chidsp = buildSp.getChildByName("child_pos") as Laya.Sprite3D;
            chiddict[ "build_frontright_" + index ] = chidsp.transform.position;
            if( PropManager.diePosY > buildSp.transform.position.y )
            {
                PropManager.diePosY = buildSp.transform.position.y;
            }
            index++;
        }
        index = 0;
        while( BlocksSprite.getChildByName( "build_leftright_" + index ) )
        {
            buildSp = BlocksSprite.getChildByName( "build_leftright_" + index ) as Laya.MeshSprite3D;
            chidsp = buildSp.getChildByName("child_pos") as Laya.Sprite3D;
            chiddict[ "build_leftright_" + index ] = chidsp.transform.position;
            if( PropManager.diePosY > buildSp.transform.position.y )
            {
                PropManager.diePosY = buildSp.transform.position.y;
            }
            index++;
        }
        index=0;
        var Props:Laya.Sprite3D = mapSprite3D.getChildByName( "Props" ) as Laya.Sprite3D;
        while( Props.getChildByName( "Scaffolding_" + index ) )
        {
            chidsp = Props.getChildByName( "Scaffolding_" + index ).getChildByName("child_pos") as Laya.Sprite3D;
            chiddict[ "Scaffolding_" + index ] = chidsp.transform.position;
            index++;
        }
        index=0;
        while( Props.getChildByName( "grilling_" + index ) )
        {
            chidsp = Props.getChildByName( "grilling_" + index ).getChildByName("child_pos") as Laya.Sprite3D;
            chiddict[ "grilling_" + index ] = chidsp.transform.position;
            index++;
        }
        index = 0;
        var jumpchildSp:Laya.MeshSprite3D;
        var iscreatejt:boolean = (PanelResDic.Instance.getSingleTon( AgencyUserMessage ) as AgencyUserMessage).RoleData.LevelID < 3 ? true:false;
        while( Props.getChildByName( "JumpBoost_" + index ) )
        {
            jumpchildSp = Props.getChildByName( "JumpBoost_" + index ) as Laya.MeshSprite3D;
            if( iscreatejt )
            {
                var pos:Laya.Vector3 = jumpchildSp.transform.position.clone();
                pos.y += 3;
                var jiantou:Laya.MeshSprite3D = Laya.Sprite3D.instantiate( Laya.loader.getRes( AssetPath.get3DRoleModel( "Arrow" ) ),PropManager.scene3D,false,
                pos  ) as Laya.MeshSprite3D;
                PropManager.jiantouArr.push( jiantou );
            }
            index++;
        }
        PropManager.patternDict[ mapSprite3D.name ] = chiddict;

        console.log("Pattern2----");
    }
    
    /***************************************************** */
    private onSpriteClick(e: Event): void {
        var randomAngle: number = Math.random() * 180;
        //发送自定义事件
        // this.sp.event(Interaction_CustomEvent.ROTATE, [randomAngle]);
    }

    // 触发自定义的rotate事件
    private onRotate(newAngle: number): void {
       Laya.Tween.to(this.sp, { "rotation": newAngle }, 1000,Laya.Ease.elasticOut);
    }

    /***************************************************** */ 

    private RefreshCharacterModel():void
    {
        var agencyusermessage:AgencyUserMessage = PanelResDic.Instance.getSingleTon( AgencyUserMessage );
        var RoleData:RoleData = agencyusermessage.RoleData;
        var pathArr:Array<any> = [];
        pathArr.push( {url:AssetPath.get3DRoleModel( RoleData.roleCfg.name ) } );
        ResourceControl.Instance.loader3DArr( pathArr,this,this.CharacterLoadEnd,null,true,AssetPath.get3DModel( RoleData.roleCfg.name ) );
    }

    /*********更新地形 */
    public RefreshTerrain(  ):void
    {
        var agencyusermessage:AgencyUserMessage = PanelResDic.Instance.getSingleTon( AgencyUserMessage );
        var RoleData:RoleData = agencyusermessage.RoleData;
        var nowlevelcfg:LevelCFG = RoleData.levelcfg;
        var mapArr:Array<string> = RoleData.levelmapArr;
        var mapSprite3D:Laya.Sprite3D;
        var mapname:string;
        var _lastmapSprite3D:Laya.Sprite3D;

        for( var s in PropManager.nowmapDict )
        {
            (PropManager.nowmapDict[s] as Laya.MeshSprite3D).active = false;
        }

        for( var i:number = 0;i < mapArr.length;i++ )
        {
            mapname = mapArr[i];
            console.log("mapname1----",mapname);
            if( PropManager.nowmapDict[ mapname ] )
            {
                mapSprite3D = PropManager.nowmapDict[ mapname ];
                mapSprite3D.active = true;
            }
            else
            {
                console.log("mapname2----",mapname);
                var sp:Laya.Sprite3D = Laya.Loader.getRes(AssetPath.get3DModel(mapname)) as Laya.Sprite3D;
                console.log("mapname3----",mapname);
                mapSprite3D = Laya.Sprite3D.instantiate(sp, PropManager.scene3D, false,null) as Laya.Sprite3D;
                console.log("mapname4----",mapname);
                PropManager.nowmapDict[ mapname ] = mapSprite3D;
                console.log("mapname5----",mapname);
            }
            if( i == 0 )
            {
                var arr:Array<string> = GameUtils.cutstrtoArr( nowlevelcfg.firstScenepos );
                mapSprite3D.transform.position = new Laya.Vector3( Number( arr[0]),Number( arr[1]),Number( arr[2] ) );
            }
            else
            {
                var endPos:Laya.Vector3 = (_lastmapSprite3D.getChildByName( "END" ) as Laya.Sprite3D).transform.position.clone();
                endPos.x = 0;
                endPos.y = 0;
                mapSprite3D.transform.position = endPos;
            }
            this.Pattern( mapSprite3D );
            _lastmapSprite3D = mapSprite3D;
            var endPos1:Laya.Vector3 = (mapSprite3D.getChildByName( "END" ) as Laya.Sprite3D).transform.position.clone();
            endPos1.z += Number(GameCFG.dict[6].value);
            PropManager.endPlane.transform.position = endPos1;
            PropManager.gamewinPosZ = endPos1.z;
            PropManager.gamewinPosZ -= 2;
            // PropManager.bgsprite3d.transform.position = new Laya.Vector3( 0,-20,-40 );
        }
    }

   


}