2D无限地图

做了个小飞机的游戏,为了让游戏一直玩下去,尝试着做了个无限地图,,没想到撸出来了。。
import Vector from "../../Common/DataStructure/Vector";
import Config from "../../Common/Coding/Define";
import PlaneMove from "../Character/Plane/PlaneMove";
import Plane from "../Character/Plane/Plane";
import GameManager from "../Game/GameManager";

 

export default class MapManager {
    public static Instance : MapManager = new MapManager();
    private constructor() {};

    private _mapTopWindow:number;
    private _mapBottomWindow:number;
    private _mapLeftWindow:number;
    private _mapRightWindow:number;

    private _mapNowYWindow:number;
    private _mapNowXWindow:number; 

    private _mapListEight:Map;
    private _mapListEightRemove:Map;

    private _CanvasBG:cc.Node;
    
    public Init():void{ 
        this._CanvasBG= cc.find("BackGroundGroup");
        this._mapNowXWindow=0;
        this._mapNowYWindow=0;  
        this._mapListEight=new Map();
        this._mapListEightRemove=new Map();
        this._mapListEight.set(new Vector(-480,960), cc.find("BackGroundGroup/bg0"));
        this._mapListEight.set(new Vector(480,960), cc.find("BackGroundGroup/bg1"));
        this._mapListEight.set(new Vector(1440,960), cc.find("BackGroundGroup/bg2"));
        this._mapListEight.set(new Vector(-480,320), cc.find("BackGroundGroup/bg3"));
        this._mapListEight.set(new Vector(480,320), cc.find("BackGroundGroup/bg4"));
        this._mapListEight.set(new Vector(1440,320), cc.find("BackGroundGroup/bg5"));
        this._mapListEight.set(new Vector(-480,-320), cc.find("BackGroundGroup/bg6"));
        this._mapListEight.set(new Vector(480,-320), cc.find("BackGroundGroup/bg7"));
        this._mapListEight.set(new Vector(1440,-320), cc.find("BackGroundGroup/bg8"));
    }


    public Cover():void{
        let array:Array= new Array(8);
        for(let i=0 ; i<8 ;i++)
            array[i]=0;

        this._mapListEight.forEach(function(res:cc.Node,pos:Vector,self ){
            //上三行 

            let x=(pos.x/Config.WinSizeWidth/2) ;
            x=Math.floor(x);
            let y=(pos.y/Config.WinSizeHeight/2);
            y=Math.floor(y);

            if(  x==this._mapNowXWindow-1 && y ==this._mapNowYWindow+1  )
            {
                array[0] = 1;
            } 
            else if( x == this._mapNowXWindow && y == this._mapNowYWindow +1 )
            {
                array[1] = 1;
            }
            else if( x == this._mapNowXWindow +1 && y==this._mapNowYWindow +1 )
            {
                array[2] = 1;
            }
            //中三行
            else if( x==this._mapNowXWindow-1  && y==this._mapNowYWindow  )
            {
                array[3]=1;
            }
            else if( x==this._mapNowXWindow  && y==this._mapNowYWindow  )
            {
                array[4]=1;
            }
            else if( x==this._mapNowXWindow +1 && y==this._mapNowYWindow  )
            {
                array[5]=1;
            }   
            //下三行
            else if( x==this._mapNowXWindow-1  && y==this._mapNowYWindow-1  )
            {
                array[6]=1;
            }
            else if( x==this._mapNowXWindow  && y==this._mapNowYWindow-1  )
            {   
                array[7]=1;
            }   
            else if( x==this._mapNowXWindow +1 && y==this._mapNowYWindow -1 )
            {
                array[8]=1;
            }
            else this._mapListEightRemove.set(new Vector(pos.x,pos.y),res);
        }.bind(this));
        cc.log(this._mapListEight.size);
        //清掉Eight
        this._mapListEightRemove.forEach(function(res:cc.Node,pos:Vector,self){  
            //清掉Eight
            this._mapListEight.forEach(function(res2:cc.Node,pos2:Vector,self2){
                if(pos2.x==pos.x&&pos2.y==pos.y)
                {
                    this._mapListEight.delete(pos2);
                    return ;
                }
            }.bind(this))
            //设置位置
            for(let i=0;i<8 ;i++){
                if(array[i]==0){
                    
                    array[i]=1;     
                    if(i==0){
                        res.setPositionX((this._mapNowXWindow-1) * Config.WinSizeWidth * 2 + Config.WinSizeWidth)  ;
                        res.setPositionY((this._mapNowYWindow+1) * Config.WinSizeHeight * 2 + Config.WinSizeHeight) ; 
                    }
                    else if(i==1){
                        res.setPositionX((this._mapNowXWindow) * Config.WinSizeWidth * 2 + Config.WinSizeWidth)  ;
                        res.setPositionY((this._mapNowYWindow+1) * Config.WinSizeHeight * 2 + Config.WinSizeHeight) ; 
                    }
                    else if(i==2){
                        res.setPositionX((this._mapNowXWindow+1) * Config.WinSizeWidth * 2 + Config.WinSizeWidth ) ;
                        res.setPositionY((this._mapNowYWindow+1) * Config.WinSizeHeight * 2 + Config.WinSizeHeight ); 
                    } 
                    else if(i==3){
                        res.setPositionX((this._mapNowXWindow-1) * Config.WinSizeWidth * 2 + Config.WinSizeWidth ) ;
                        res.setPositionY((this._mapNowYWindow) * Config.WinSizeHeight * 2 + Config.WinSizeHeight ); 
                    }
                    else if(i==4){
                        res.setPositionX((this._mapNowXWindow) * Config.WinSizeWidth * 2 + Config.WinSizeWidth ) ;
                        res.setPositionY((this._mapNowYWindow) * Config.WinSizeHeight * 2 + Config.WinSizeHeight ); 
                    }
                    else if(i==5){
                        res.setPositionX((this._mapNowXWindow+1) * Config.WinSizeWidth * 2 + Config.WinSizeWidth ) ;
                        res.setPositionY((this._mapNowYWindow) * Config.WinSizeHeight * 2 + Config.WinSizeHeight ); 
                    }
                    else if(i==6){
                        res.setPositionX((this._mapNowXWindow-1) * Config.WinSizeWidth * 2 + Config.WinSizeWidth)  ;
                        res.setPositionY((this._mapNowYWindow-1) * Config.WinSizeHeight * 2 + Config.WinSizeHeight ); 
                    }
                    else if(i==7){
                        res.setPositionX((this._mapNowXWindow) * Config.WinSizeWidth * 2 + Config.WinSizeWidth ) ;
                        res.setPositionY((this._mapNowYWindow-1) * Config.WinSizeHeight * 2 + Config.WinSizeHeight) ; 
                    }
                    else if(i==8){
                        res.setPositionX((this._mapNowXWindow+1) * Config.WinSizeWidth * 2 + Config.WinSizeWidth ) ;
                        res.setPositionY((this._mapNowYWindow-1) * Config.WinSizeHeight * 2 + Config.WinSizeHeight) ; 
                    }
                    this._mapListEight.set(new Vector(res.position.x,res.position.y),res);
                    break;            
                }
            }
        }.bind(this)); 

        this._mapListEightRemove.clear();
    }

    public Move():void
    {   
        let _positionPlane = GameManager.Instance.GetHost().GetMove().GetPosition(); 
    
        this._mapNowXWindow=(_positionPlane.x/Config.WinSizeWidth/2);
        this._mapNowXWindow=Math.floor(this._mapNowXWindow);
        this._mapNowYWindow=(_positionPlane.y/Config.WinSizeHeight/2);
        this._mapNowYWindow=Math.floor(this._mapNowYWindow);
        cc.log(this._mapNowXWindow +" "+this._mapNowYWindow);
    }   
    
    public Update(dt:number):void{
        this.Move();
        this.Cover();
        
    }

} 

你可能感兴趣的:(实习,游戏中的寻路)