Scrollbare Karte

Federhalter

Mitglied
Hallo,

ich habe eine verschiebbare 2-dimensionale Karte aus Rechtecken erstellt. Dieses wird zur Laufzeit nicht komplett gezeichnet, sondern nur der sichtbare Bereich. Nun möchte ich zur Laufzeit irgendein beliebiges Feld anklicken, ohne dass sich dabei beim verschieben der Karte der Wert für das beliebige Feld gleichzeitig mit verändert.

Hier mal der Code:

Java:
public class Grid {
   
    private int cols;
    private int rows;
   
    private int width;
    private int height;
   
    private int mousex;
    private int mousey;
    private int tx;
    private int ty;
   
    private Rectangle[][] boxes;
   
    public Grid(int cols,int rows) {
        this.cols = cols;
        this.rows = rows;
       
        width = 32;
        height = 32;
       
        boxes = new Rectangle[cols-1][rows-1];
       
       
       
    }
   
    public void renderGrid(Graphics g,int xoffset,int yoffset){
       
        for(int j = 0;j<rows-1;j++){
            for(int i = 0;i<cols-1;i++){
                boxes[i][j] = new Rectangle(i*width,j*height,width,height);
            }
        }
       
        mousex = ((Mouse.mousex-(Mouse.mousex%width))/width)+((xoffset-(xoffset%width))/width);
        mousey = ((Mouse.mousey-(Mouse.mousey%height))/height)+((yoffset-(yoffset%height))/height);
       
        if(Mouse.right){
           
            tx = boxes[mousex][mousey].x/width;
            ty = boxes[mousex][mousey].y/height;
           
        }
       
        System.out.println(tx + "   " + ty);
       
        if(xoffset < 0)xoffset = 0;
        if(yoffset < 0)yoffset = 0;
        if(xoffset >= cols*width-Game.WIDTH)xoffset = cols*width-Game.WIDTH-1;
        if(yoffset >= rows*height-Game.HEIGHT)yoffset = rows*height-Game.HEIGHT-1;
       
        int startX = Math.max(xoffset / width, 0);
        int startY = Math.max(yoffset / height, 0);
        int limitX = Math.min((xoffset + Game.WIDTH ) / width + 1, cols );
        int limitY = Math.min((yoffset + Game.HEIGHT) / height + 1, rows);
       
        for(int j = startY;j<limitY;j++){
            for(int i = startX;i<limitX;i++){
                g.drawRect(boxes[i][j].x-xoffset,boxes[i][j].y-yoffset,width,height);
            }
        }
    }
 

Neue Beiträge

Zurück