8.极其简陋的地图编辑器

因为我的地图都是通过原有的素材转化过来的,并且我的地图并没有用瓦片地图,所以我想要对其编辑必须制作属于自己的地图编辑器

设想主要通过快捷键来实现,其简要功能大概是设置地图素材,地图类型,复制粘贴等

需求整理如下

点击显示该六边形地块

按住shift可以多选地块

按z键切换快捷键模式

模式0: 1层地块,带地理属性,wasd变为1层装饰偏移,c复制(复制地理属性),v粘贴(批量),x清除(不清除地理属性)
模式1: 2层地块,wasd变为2层装饰偏移,c复制,v粘贴(批量),x清除
模式2: 粘贴区块,x重置为当前id,c复制,v粘贴

功能实现如下:

点击显示该六边形地块,按住shift可以多选地块

package com.zhfy.game.model.framework;

public class Coord {
    //定义坐标 开始均为0
    
    public Coord(int x,int y){
        this.x=x;//至少为0
        this.y=y;//至少为0
    }
    public Coord(int x,int y,float m,float n){
        this.x=x;//至少为0
        this.y=y;//至少为0
        this.vertexX=m;
        this.vertexY=n;
    }
    
    public int x;
    public int y;
    
    public float vertexX;//左上顶点坐标
    public float vertexY;//左上顶点坐标
    public int id;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public float getVertexX() {
        return vertexX;
    }
    public void setVertexX(float vertexX) {
        this.vertexX = vertexX;
    }
    public float getVertexY() {
        return vertexY;
    }
    public void setVertexY(float vertexY) {
        this.vertexY = vertexY;
    }
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    
    
}
Coord类
addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {

                {//点击坐标
                    coord = GameMap.getHotCell2(getX() - (x) / getZoom(), getImgY(getY() - yMin - (y) / getZoom()));
                    coord.setVertexY(getMapYByImgY(coord.vertexY));
                    coord.setId(w * coord.getY() + coord.getX());
                    Gdx.app.log("坐标", "imgX:" + coord.getX() + " imgY:" + coord.getY()+" id:"+coord.getId());
                    //判断是否点击shift
                    if (Gdx.input.isKeyPressed(Input.Keys.SHIFT_LEFT)) {
                        //如果是持续点击,则添加
                        coords.add(coord);
                    } else {
                        coords.clear();
                        coords.add(coord);
                    }
                   
                    fMax = coords.size();
                }
            }
        });
actor中的点击监听
// 六边形网格定位
    // @param xPos 输入,所需查询的点的x坐标 
    // @param yPos 输入,所需查询的点的y坐标,y必须转换,因为以左上开始计算
    // @param cell_x 输出,改点所在网格的x坐标
    // @param cell_y 输出,改点所在网格的y坐标
    public static Coord getHotCell2(float xPos, float yPos) {
        
        float GRID_WIDTH =112;
        float GRID_HEIGHT =128;// (CELL_BORDER*0.8660254f)
        float Gride_BORDER=GRID_WIDTH/1.5f;//75
        float GRID_W_B=(GRID_WIDTH/4);//宽-变成/2 即小长 
        
        int cell_y;
        int cell_x;
        float vertex_x_px;
        float vertex_y_px;
        xPos=xPos/Config.Map.MAP_SCALE;//不知道为什么会出现偏移,所以通过这里来减少偏移
        yPos=yPos/Config.Map.MAP_SCALE;
        cell_x = (int) (xPos / GRID_WIDTH);
        float x = Math.abs(xPos - cell_x * GRID_WIDTH);
        //TODO 因为计算公式为负数 所以与以前的计算相驳
        cell_y = (int) (yPos   / GRID_HEIGHT);
        float y = Math.abs(yPos -     cell_y * GRID_HEIGHT);
        
        //if(! (Gride_BORDER-Math.abs((x-1/2*GRID_WIDTH))>Math.abs(y-1/2*GRID_HEIGHT)/Math.sqrt(3))) {
        //if(! (Math.abs(GRID_WIDTH/2-x)+Math.abs(GRID_HEIGHT/2-y)/Math.sqrt(3)<=Gride_BORDER*Math.sqrt(3))) { 
        
        /*if(Gride_BORDER-Math.abs(x)<=Math.abs(y)/Math.sqrt(3)) { 
            //在六边形内部
            if(x>Gride_BORDER) {
                    x++;
            }
            Gdx.app.log("", "cx:"+cell_x+" gw:"+x);
        }*/
        //先判断位置
        if(x<GRID_W_B) {
            if(y<GRID_HEIGHT/2) {
                //在上
                if(x/Math.tan(60)<y) {
                    cell_x++;
                }
            }else {
                //在下
                if(x/Math.tan(60)<(y-GRID_HEIGHT/2)) {
                    cell_x++;
                }
            }
        //右边
        }/**/
        
        
        if( cell_x % 2==0 && y<64) {
            cell_y--;
        }else if(cell_x % 2!=0){
            cell_y--;
        }
        
        vertex_x_px=cell_x*GRID_WIDTH*Config.Map.MAP_SCALE;
        vertex_y_px=cell_x%2!=0?(cell_y+1)*GRID_HEIGHT*Config.Map.MAP_SCALE:(cell_y+0.5f)*GRID_HEIGHT*Config.Map.MAP_SCALE;
        Coord coord = new Coord(-cell_x, cell_y-1,vertex_x_px,vertex_y_px);
        return coord;
    }


//将点击坐标转化,因为actor中的y轴起点在下方,而游戏坐标上上方,所以需要转化
public float getImgY(float print_y) {
        return (this.mapH_px * Config.Map.MAP_SCALE + print_y);
    }
用到的一些辅助方法
 //测试 绘制 顶点坐标
                for (f = 0; f < fMax; f++) {
                    batch.draw(spriteList.get(Integer.parseInt(spriteMap.get("fog"))).getSprite(), getStaticImgX(coords.get(f).getVertexX()), getStaticImgY(coords.get(f).getVertexY()), getOriginX(), getOriginY(), spriteList.get(Integer.parseInt(spriteMap.get("fog"))).getSprite().getWidth() * Config.Map.MAP_SCALE,
                            spriteList.get(Integer.parseInt(spriteMap.get("fog"))).getSprite().getHeight() * Config.Map.MAP_SCALE, getZoom(), getZoom(), getRotation());
                }



//辅助方法
//假如静态绘制,通过此方法获得绘制坐标
    public float getStaticImgX(float x) {
        return (this.x - x) * zoom;
    }
    
    public float getStaticImgY(float y) {
        return (this.y - y) * zoom;
    }
draw方法的绘制

按z键切换快捷键模式

//初始化增加Label类加入舞台
         label=new Label("FPS:"+Gdx.graphics.getFramesPerSecond(),new LabelStyle(new BitmapFont(), null));
        
        label.setWidth(200);//设置每行的宽度
        label.setWrap(true);//开启换行
        label.setPosition(20, 40);
        gameStage.addActor(label);

//render方法中增加 显示快捷键模式
label.getText().appendLine("inputKeyMode:"+bgActor.getKeyMode());//快捷键模式

//render方法中handleInput();的按键方法增加切换
    //切换快捷键模式
           if (Gdx.input.isKeyJustPressed(Input.Keys.Z)) {
               if(bgActor.getKeyMode()+1<bgActor.getKeyModeMax()) {
                   bgActor.setKeyMode(bgActor.getKeyMode()+1);
               }else {
                   bgActor.setKeyMode(0);
               }
           }
舞台类screen增加显示快捷键方案
//定义变量
    private int keyMode = 0;//切换按钮为z 
    private int keyModeMax = 3;//0-2
    
    private int tempKeyBlockType;//地理区域
    private int tempRegionId;//区块区域
    private int tempKeyTile;//
    private int tempKeyIdx;

//定义快捷键方法
private void keyInput() {
        /*
         * 0 1层地块,带地理属性,wasd变为1层装饰偏移,c复制(复制地理属性),v粘贴,x清除(不清除地理属性) 
         * 1 2层地块,wasd变为2层装饰偏移,c复制(复制地理属性),v粘贴(批量),x清除
         * 2 粘贴区块,x重置为当前id,c复制(不复制地理属性),v粘贴(批量)
         */
        {//快捷键
            switch(keyMode) {
                case 0:
                    if (Gdx.input.isKeyPressed(Input.Keys.A)) {
                        mapBinDAO.getMapbin().get(coord.getId()).setBackRefX(mapBinDAO.getMapbin().get(coord.getId()).getBackRefX()+1);
                        mx=1;
                    }
                    if (Gdx.input.isKeyPressed(Input.Keys.D)) {
                        mapBinDAO.getMapbin().get(coord.getId()).setBackRefX(mapBinDAO.getMapbin().get(coord.getId()).getBackRefX()-1);
                        mx=1;
                    }
                    if (Gdx.input.isKeyPressed(Input.Keys.S)) {
                        mapBinDAO.getMapbin().get(coord.getId()).setBackRefY(mapBinDAO.getMapbin().get(coord.getId()).getBackRefY()-1);
                        mx=1;
                    }
                    if (Gdx.input.isKeyPressed(Input.Keys.W)) {
                        mapBinDAO.getMapbin().get(coord.getId()).setBackRefY(mapBinDAO.getMapbin().get(coord.getId()).getBackRefY()+1);
                        mx=1;
                    }
                    if (Gdx.input.isKeyJustPressed(Input.Keys.X)) {
                        mapBinDAO.getMapbin().get(coord.getId()).setBackRefX(0);
                        mapBinDAO.getMapbin().get(coord.getId()).setBackRefY(0);
                        mapBinDAO.getMapbin().get(coord.getId()).setBackTile(0);
                        mapBinDAO.getMapbin().get(coord.getId()).setBackIdx(0);
                        mx=1;
                    }
                    if (Gdx.input.isKeyJustPressed(Input.Keys.C)) {
                        tempKeyBlockType=mapBinDAO.getMapbin().get(coord.getId()).getBlockType();
                        tempKeyTile=mapBinDAO.getMapbin().get(coord.getId()).getBackTile();
                        tempKeyIdx=mapBinDAO.getMapbin().get(coord.getId()).getBackIdx();
                        mx=1;
                    }
                    if (Gdx.input.isKeyJustPressed(Input.Keys.V)) {
                        //mapBinDAO.getMapbin().get(coord.getId()).setBlockType(tempKeyBlockType);
                        //mapBinDAO.getMapbin().get(coord.getId()).setBackTile(tempKeyTile);
                        //mapBinDAO.getMapbin().get(coord.getId()).setBackIdx(tempKeyIdx);
                        for(Coord coord:coords) {
                            mapBinDAO.getMapbin().get(coord.getId()).setBlockType(tempKeyBlockType);
                            mapBinDAO.getMapbin().get(coord.getId()).setBackTile(tempKeyTile);
                            mapBinDAO.getMapbin().get(coord.getId()).setBackIdx(tempKeyIdx);
                        }
                        mx=1;
                    }
                case 1:
                    if (Gdx.input.isKeyPressed(Input.Keys.A)) {
                        mapBinDAO.getMapbin().get(coord.getId()).setForeRefX(mapBinDAO.getMapbin().get(coord.getId()).getForeRefX()+1);
                        mx=1;
                    }
                    if (Gdx.input.isKeyPressed(Input.Keys.D)) {
                        mapBinDAO.getMapbin().get(coord.getId()).setForeRefX(mapBinDAO.getMapbin().get(coord.getId()).getForeRefX()-1);
                        mx=1;
                    }
                    if (Gdx.input.isKeyPressed(Input.Keys.S)) {
                        mapBinDAO.getMapbin().get(coord.getId()).setForeRefY(mapBinDAO.getMapbin().get(coord.getId()).getForeRefY()-1);
                        mx=1;
                    }
                    if (Gdx.input.isKeyPressed(Input.Keys.W)) {
                        mapBinDAO.getMapbin().get(coord.getId()).setForeRefY(mapBinDAO.getMapbin().get(coord.getId()).getForeRefY()+1);
                        mx=1;
                    }
                    if (Gdx.input.isKeyJustPressed(Input.Keys.X)) {
                        mapBinDAO.getMapbin().get(coord.getId()).setForeRefX(0);
                        mapBinDAO.getMapbin().get(coord.getId()).setForeRefY(0);
                        mapBinDAO.getMapbin().get(coord.getId()).setForeTile(0);
                        mapBinDAO.getMapbin().get(coord.getId()).setForeIdx(0);
                        mx=1;
                    }
                    if (Gdx.input.isKeyJustPressed(Input.Keys.C)) {
                        tempKeyTile=mapBinDAO.getMapbin().get(coord.getId()).getForeTile();
                        tempKeyIdx=mapBinDAO.getMapbin().get(coord.getId()).getForeIdx();
                    }
                    if (Gdx.input.isKeyJustPressed(Input.Keys.V)) {
                        //mapBinDAO.getMapbin().get(coord.getId()).setForeTile(tempKeyTile);
                        //mapBinDAO.getMapbin().get(coord.getId()).setForeIdx(tempKeyIdx);
                        for(Coord coord:coords) {
                            mapBinDAO.getMapbin().get(coord.getId()).setForeTile(tempKeyTile);
                            mapBinDAO.getMapbin().get(coord.getId()).setForeIdx(tempKeyIdx);
                        }
                        mx=1;
                    }
                case 2: 
                    if (Gdx.input.isKeyJustPressed(Input.Keys.X)) {
                        mapBinDAO.getMapbin().get(coord.getId()).setRegionId(coord.getId());;
                        mx=1;
                    }
                    if (Gdx.input.isKeyJustPressed(Input.Keys.C)) {
                        tempRegionId=mapBinDAO.getMapbin().get(coord.getId()).getRegionId();
                    }
                    if (Gdx.input.isKeyJustPressed(Input.Keys.V)) {
                        for(Coord coord:coords) {
                            mapBinDAO.getMapbin().get(coord.getId()).setRegionId(tempRegionId);
                        }
                        mx=1;
                    }   
            }
        }
    }

draw方法中增加keyInput(); 即可
actor实现快捷键功能
//draw方法中显示颜色
                     if (ifGrid&&keyMode==2) {
                        newColor = new Color(GameUtils.getColorByNum(drawCycleList.get(i).getRegionId()));
                        oldColor = batch.getColor();
                        batch.setColor(newColor);
                        batch.draw(spriteList.get(Integer.parseInt(spriteMap.get("hexagon"))).getSprite(), drawCycleList.get(i).getDx_px(), drawCycleList.get(i).getDy_px(), getOriginX(), getOriginY(), spriteList.get(Integer.parseInt(spriteMap.get("hexagon"))).getSprite().getWidth() * Config.Map.MAP_SCALE,
                                spriteList.get(Integer.parseInt(spriteMap.get("hexagon"))).getSprite().getHeight() * Config.Map.MAP_SCALE, getZoom(), getZoom(), getRotation());
                        batch.setColor(oldColor);
                    } 

if (ifGrid) {
                        batch.draw(spriteList.get(Integer.parseInt(spriteMap.get("grid"))).getSprite(), drawCycleList.get(i).getDx_px(), drawCycleList.get(i).getDy_px(), getOriginX(), getOriginY(), spriteList.get(Integer.parseInt(spriteMap.get("grid"))).getSprite().getWidth() * Config.Map.MAP_SCALE,
                                spriteList.get(Integer.parseInt(spriteMap.get("grid"))).getSprite().getHeight() * Config.Map.MAP_SCALE, getZoom(), getZoom(), getRotation());
                        if(keyMode==2) {
                            bitmapFont.draw(batch, "ri:" +drawCycleList.get(i).getRegionId(), drawCycleList.get(i).getDx_px() + 16 * zoom,drawCycleList.get(i).getDy_px() + 30 * zoom);
                        }else {
                            bitmapFont.draw(batch, " x:" + drawCycleList.get(i).getDraw_gx(), drawCycleList.get(i).getDx_px() + 14 * zoom, drawCycleList.get(i).getDy_px() + 30 * zoom);
                            bitmapFont.draw(batch, "id:" + drawCycleList.get(i).getDraw_gd(), drawCycleList.get(i).getDx_px() + 11 * zoom, drawCycleList.get(i).getDy_px() + 20 * zoom);
                            bitmapFont.draw(batch, " y:" + drawCycleList.get(i).getDraw_gy(), drawCycleList.get(i).getDx_px() + 14 * zoom, drawCycleList.get(i).getDy_px() + 10 * zoom);
                        }
                    }
一起其他方法

效果:模式2区块图,省区的编辑

猜你喜欢

转载自www.cnblogs.com/tysk/p/10803187.html