简体   繁体   中英

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. How can I go about generating these tiles in a better way? I want it to be randomly generated and not drawn from an array map of like 000001100.

Block 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

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. I'm not sure if ID is the right word, but I hope you can understand what I am trying to ask.

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. To change the tile type at a particular coordinate, all you need to do is set the index array to the type you want.

Specifically, in your render function, you would have something like:

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? You should probably have it in its own function or at least inside your update function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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