2012-04-10 3 views
0

私はマウスピッキングとタイルをしようとしています。芝生は汚れに似ていますが、すべての芝生タイルは同じ「ID」を持ち、画面上のすべての芝生タイルが汚れに変わります。これらのタイルをより良い方法で生成するにはどうすればよいですか?私はそれがランダムに生成され、ブロッククラスJava - 配列から生成された各タイルにユニークな "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); 
} 

Iの配列マップから引き出されていないことにしたいです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; 

私はあなたが持っているコードは、さらにそれを変更する前にやっている正確に何を把握しようと思います。

注:これはどうしてレンダリング機能にありますか?あなたはおそらくそれ自身の機能で、あるいは少なくともあなたのupdate機能の中にあるべきです。

+0

オハイオ州は完全に理にかなっています。何らかの理由でそのように考えたことはありません。ありがとう、私は完全に困惑し、それは単純な修正で終わった。私はテクスチャを変更するのではなく、再描画する前にそれを正確に行う方法が混乱していたので、そのようにしていたと思います。私は今更新にそれを入れます – Corey

関連する問題