简体   繁体   English

Java 2d游戏平铺地图剪辑?

[英]Java 2d game tiled map clipping?

Well I've created a simple tile map loading, from .txt file & then drawing. 好吧,我已经创建了一个简单的图块地图加载,首先是从.txt文件开始,然后是绘图。

25
15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

So if the current tile is 1, it will be a green block. 因此,如果当前图块为1,它将是一个绿色块。

Now the game window looks like this (ignore yellow dots at top): 现在,游戏窗口如下所示(忽略顶部的黄点):

IMG
(source: gyazo.com ) (来源: gyazo.com

Okay everything is fine and great, but now can you see the red box? 好的,一切都很好,但是现在您可以看到红色框了吗? that's the character, and when it walks, I don't want it to go over the gates (any green block). 那是角色,当它走路时,我不希望它越过大门(任何绿色的方块)。

How can I do that? 我怎样才能做到这一点?

My attempt: 我的尝试:

private void makeClips() {
    int[][] tiledMap = this.map.getMap();
    for (int i = 0; i < this.map.getHeight(); i++) {
        for (int j = 0; j < this.map.getWidth(); j++) {  
            if (tiledMap[i][j] == 1) {
                clips.add(new Clip(j * 30, i * 30, 0, 0));
                System.out.println("Added clip: " + j + " " + i + " " + (j + 30) + " " + (i + 30));
            }
        }
    }
}

That should create an array of clips, so we can check if player's next walk equals to the clips coordinates, but I've had problems with setting the clip x, y like what will it's position be. 那应该创建一个剪辑数组,所以我们可以检查玩家的下一个步是否等于剪辑坐标,但是我在设置剪辑x和y时遇到问题,就像它的位置一样。

The 30 is the tile size, so each block will be 30 width 30 height sized. 30是图块大小,因此每个块将是30宽30高的大小。

And then in the walking method i've done this: 然后在walking方法中,我这样做了:

    for (Clip clip : clips) {
        if (myPlayer.getX() + x >= clip.getFirstX() && myPlayer.getX() + x <= clip.getSecX() 
                && myPlayer.getY() + y >= clip.getFirstY() && myPlayer.getY() + y <= clip.getSecY()) {
            System.out.println("Bad");
            return;
        }   
    }   

But I don't know, this is 100% incorrect, mostly the coordinate calculating part. 但是我不知道,这是100%错误的,主要是坐标计算部分。

What would you do in this case? 在这种情况下您会怎么做?

This is the drawing part for map: 这是地图的绘图部分:

private void renderMap(Graphics2D g) {
    int[][] tiledMap = this.map.getMap();
    for (int i = 0; i < this.map.getHeight(); i++) {
        for (int j = 0; j < this.map.getWidth(); j++) {
            int currentRow = tiledMap[i][j];

            if (currentRow == 1) {
                g.setColor(Color.green);
            }
            if (currentRow == 0) {
                g.setColor(Color.black);
            }           
            g.fillRect(0 + j * map.getTileSize(), 0 + i * map.getTileSize(),
                    map.getTileSize(), map.getTileSize());

            g.setColor(Color.yellow);
            for (Clip clip : clips) {
                g.fillRect(clip.getFirstX(), clip.getFirstY(), 2, 2);

            }
        }           
    }
}

What can I do? 我能做什么?

Why not do a check every time you move your player on the tile you want to move him onto. 为何每次将播放器移动到想要将其移动到的图块上时都不进行检查。

Lets assume the player is at (0,0) on this small map. 假设玩家在这张小地图上的(0,0)。

0 0 1 0 0 1

0 0 0 0 0 0

0 0 0 0 0 0

We will try and move him to the right twice. 我们将尝试将他向右移动两次。 The first time will work as at the position (1,0) the tile is equal to 0. The second time we try this the tile at (2,0) will return 1 and the player won't move. 第一次将在位置(1,0)处工作,该图块等于0。第二次尝试此操作时,在(2,0)的图块将返回1,并且玩家将不会移动。

if (tiledMap[player.getX() + 1)[player.getY()] == 1) {
//do nothing
} else {
//move player
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM