简体   繁体   English

Java - 如何为从数组生成的每个图块提供唯一的“ID”

[英]Java - How to give each tile generated from an array a unique "ID"

I am trying to do mouse picking and the tile I click on changes to whatever the opposite tile is ie.我正在尝试进行鼠标拾取,我点击的图块会更改为相反的图块。 grass to dirt, but every grass tile has the same "ID" so every grass tile on the screen changes to dirt.草变成泥土,但每个草块都有相同的“ID”,因此屏幕上的每个草块都会变成泥土。 How can I go about generating these tiles in a better way?我 go 如何以更好的方式生成这些图块? I want it to be randomly generated and not drawn from an array map of like 000001100.我希望它是随机生成的,而不是从类似于 000001100 的数组 map 中提取的。

Block class块 class

public class Block {

public enum BlockType {
    Dirt,
    Grass,
    Selection
}

BlockType Type;
Vector2f Position;
Image texture;
boolean breakable;

public Block(BlockType Type, Vector2f Position, Image texture, boolean breakable) {
    this.Type = Type;
    this.Position = Position;
    this.texture = texture;
    this.breakable = breakable;
}

    public BlockType getType() { 
        return Type; 
    }
    public void setType(BlockType value) {
        Type = value;
    }

    public Vector2f getPosition() { 
        return Position; 
    }
    public void setPosition(Vector2f value) { 
        Position = value; 
    }

    public Image gettexture() { 
        return texture; 
    }
    public void settexture(Image value) { 
        texture = value; 
    }

    public boolean getbreakable() { 
        return breakable; 
    }
    public void setbreakable(boolean value) { 
        breakable = value; 
    }
}

Tile Generation Class瓦片生成 Class

public class TileGen {

Block block;
public Block[] tiles = new Block[2];
public int width, height;
public int[][] index;
boolean selected;
int mouseX, mouseY;
int tileX, tileY;

Image dirt, grass, selection;
SpriteSheet tileSheet;

public void init() throws SlickException {
    tileSheet = new SpriteSheet("assets/tiles/tileSheet.png", 64, 64);

    grass = tileSheet.getSprite(0,0);
    dirt = tileSheet.getSprite(1,0);
    selection = tileSheet.getSprite(2,0);

    tiles[0] = new Block(BlockType.Grass, new Vector2f(tileX,tileY), grass, true);
    tiles[1] = new Block(BlockType.Dirt, new Vector2f(tileX,tileY), dirt, true);

    width = 50;
    height = 50;

    index = new int[width][height];

    Random rand = new Random();
    for (int x = 0; x < width; x++)
    {
        for (int y = 0; y < height; y++)
        {
            index[x][y] = rand.nextInt(2);
        }
    }
}

public void update(GameContainer gc) {
    Input input = gc.getInput();
    mouseX = input.getMouseX();
    mouseY = input.getMouseY();
    tileX = mouseX / width;
    tileY = mouseY / height;

    if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
        selected = true;
    }
    else{
        selected = false;
    }
    System.out.println(tiles[index[tileX][tileY]]);
}

public void render() {
    for (int x = 0; x < width; x++)
    {
        for (int y = 0; y < height; y++)
        {
            tiles[index[x][y]].texture.draw(x * 64, y *64);

            if(IsMouseInsideTile(x, y))
                selection.draw(x * 64, y * 64);
            if(selected && tiles[index[x][y]].breakable) {
                if(tiles[index[tileX][tileY]].Type == BlockType.Grass)
                    tiles[index[tileX][tileY]].texture = dirt;
            }
        }
    }
}

public boolean IsMouseInsideTile(int x, int y)
{
    return (mouseX >= x * 64 && mouseX <= (x + 1) * 64 &&
            mouseY >= y * 64 && mouseY <= (y + 1) * 64);
}

I am using slick2d library.我正在使用 slick2d 库。 I'm not sure if ID is the right word, but I hope you can understand what I am trying to ask.我不确定 ID 是否是正确的词,但我希望你能理解我想问的问题。

It looks like your existing structure is fine, but it doesn't look like you understand what it does.看起来您现有的结构很好,但看起来您并不了解它的作用。 The int[][] index array holds a grid of tile types, corresponding to x and y coordinates. int[][] index数组包含瓦片类型的网格,对应于xy坐标。 To change the tile type at a particular coordinate, all you need to do is set the index array to the type you want.要更改特定坐标处的瓦片类型,您需要做的就是将index数组设置为您想要的类型。

Specifically, in your render function, you would have something like:具体来说,在您的渲染 function 中,您会看到类似以下内容:

if(IsMouseInsideTile(x, y) && selected && tiles[index[x][y]].breakable)
    if(tiles[index[tileX][tileY]].Type == BlockType.Grass)
        tiles[index[tileX][tileY]].texture = dirt;

I'd try to figure out exactly what the code you have is doing before modifying it further.在进一步修改之前,我会尝试弄清楚您所拥有的代码到底在做什么。

Note: why is this in the render function anyways?注意:为什么这个在渲染 function 中呢? You should probably have it in its own function or at least inside your update function.您可能应该将它放在自己的 function 中,或者至少在您的update function 中。

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

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