我正在尝试进行鼠标拾取,而我点击的图块会更改为相反的图块即ie。草到污垢,但每个草砖都有相同的“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是否是正确的词,但我希望你能理解我想要问的内容。
答案 0 :(得分:1)
看起来您现有的结构很好,但看起来并不像您了解它的作用。 int[][] index
数组包含一个tile类型网格,对应x
和y
坐标。要更改特定坐标处的切片类型,您需要做的就是将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
函数内部使用它。