2012-04-10 73 views
0

我试图做鼠标采摘和瓷砖我点击变化,无论是相反的瓷砖即是。草地上的污垢,但每块草地砖都有相同的“ID”,因此屏幕上的每块草坪都会变脏。我怎样才能更好地生成这些瓷砖?我希望它是随机生成的,而不是从像000001100.爪哇 - 如何给阵列产生一个独特的“ID”

Block类

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是否合适,但我希望你能理解我想问的问题。

回答

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; 

我会尝试找出到底是什么你的代码被进一步修改之前做的事情。

注意:为什么在渲染函数中呢?你应该有自己的功能,或者至少在你的功能里面。

+0

哦,好吧,这是非常有意义的。从来没有这样想过。谢谢,我完全被难住了,它最终只是一个简单的修复。我认为我是这么想的,因为在重绘它之前我没有改变纹理,而是对如何完全做到这一点感到困惑。我现在将把它放在更新中 – Corey 2012-04-10 23:26:14