0

我正在尝试进行鼠标选择,并且我单击的图块会更改为对面的图块,即。将草变为泥土,但每个草砖都有相同的“ID”,因此屏幕上的每个草砖都会变为泥土。我怎样才能以更好的方式生成这些图块?我希望它是随机生成的,而不是从像 000001100 这样的数组映射中绘制的。

块类

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; 
    }
}

瓦片生成类

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);
}

我正在使用 slick2d 库。我不确定 ID 是否是正确的词,但我希望你能理解我想问的问题。

4

1 回答 1

1

看起来您现有的结构很好,但看起来您并不了解它的作用。该int[][] index数组包含一个瓦片类型的网格,对应于xy坐标。要更改特定坐标处的瓷砖类型,您只需将index数组设置为您想要的类型。

具体来说,在您的渲染函数中,您将拥有如下内容:

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

在进一步修改之前,我会尝试弄清楚你的代码在做什么。

注意:为什么这仍然在渲染函数中?您可能应该将它放在自己的函数中,或者至少在您的update函数中。

于 2012-04-10T23:18:54.097 回答