Java相机位置计算问题

时间:2013-12-31 13:29:01

标签: java swing graphics camera tiles

我正在制作一个2D游戏,我决定实现一个跟随玩家的相机,这样你就可以探索一张大地图,并在窗口中加载一小部分地图。

但现在我有一点问题,我希望地图跟随玩家,所以玩家总是居中,例如它应该如何看起来像这样但更集中(在油漆中做到了):

img http://gyazo.com/2bdb684b11efd1221bd5f9879542257b.png

但目前它看起来像这样:

img2 http://gyazo.com/956db89381edb513988e035786291ffa.png

因为offX& offY = 0,我需要考虑一个让玩家以相机为中心的公式。

这就是我在drawMap方法中解决的问题(移动相机):

private void renderMap(Graphics2D g) {
    int[][] tiledMap = this.map.getMap();
    for (int i = 0; i < this.map.getHeight(); i++) {
        for (int j = 0; j < this.map.getWidth(); j++) {
            int currentRow = tiledMap[i][j] ;

            if (currentRow == 0) {
                g.drawImage(img, (j + offX) * map.getTileSize(), (i + offY) * map.getTileSize(), this);
            }    

            if (currentRow == 1) {
                g.setColor(Color.green);
                g.fillRect((j + offX) * map.getTileSize(),
                        (i + offY) * map.getTileSize(), map.getTileSize(),
                        map.getTileSize());                 
            }               
        }           
    }
}

基本上,它将offX广告到拼贴x,并将offY广告到拼贴y。 瓷砖尺寸是30像素,因此地图按照瓷砖尺寸移动,这是我需要排序的问题,或者只是按照瓷砖尺寸移动播放器,然后步行将太快,因为相机只能加载通过一个平铺,如果我们每个玩家移动一个平铺,相机最终将不会保持居中于玩家。

如何让相机始终居中于播放器?

这是我的代码:

package snow;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.swing.JPanel;

public class GameController extends JPanel {

    private static final long serialVersionUID = 1L;
    private Player myPlayer = new Player(5, 5, 25, 25);
    private TileMap map;
    private Image img;

    public GameController() {
        this.map = new TileMap(new File("src/snow/Tiles/map1.txt"));
        try {
            this.img = ImageIO.read(new File("data/snow.png"));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        try {
            this.map.buildTile();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }   
    }

    public void update() {
    }

    public void render(Graphics2D g) {
        g.setColor(Color.red);
        g.fillRect(myPlayer.getX(), myPlayer.getY(),
                myPlayer.getWidth(), myPlayer.getHeight());
    }

    public void paintComponent(Graphics g) { 
        Graphics2D gfx = (Graphics2D) g;
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, 765, 503);
        renderMap(gfx);
        render(gfx);
    }

    private void renderMap(Graphics2D g) {
        int[][] tiledMap = this.map.getMap();
        for (int i = 0; i < this.map.getHeight(); i++) {
            for (int j = 0; j < this.map.getWidth(); j++) {
                int currentRow = tiledMap[i][j] ;

                if (currentRow == 0) {
                    g.drawImage(img, (j + offX) * map.getTileSize(), (i + offY) * map.getTileSize(), this);
                }    

                if (currentRow == 1) {
                    g.setColor(Color.green);
                    g.fillRect((j + offX) * map.getTileSize(),
                            (i + offY) * map.getTileSize(), map.getTileSize(),
                            map.getTileSize());                 
                }               
            }           
        }
    }

    private int offX = 0, offY = 0;

    public void movePlayer(int x, int y) { 
        int[][] tiledMap = this.map.getMap();
        myPlayer.updateX(x);
        myPlayer.updateY(y); 
        offX = +10;
        offY = +7;
    }
}

package snow;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class TileMap {

    private File tileMap;
    private int tileWidth;
    private int tileHeight;
    private int tileSize = 30;
    private int[][] map;

    public TileMap(File tileMap) {
        this.tileMap = tileMap;
    }

    public void buildTile() throws NumberFormatException, IOException {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(this.tileMap));
            this.tileWidth = Integer.parseInt(reader.readLine());
            this.tileHeight = Integer.parseInt(reader.readLine());
            map = new int[this.tileHeight][this.tileWidth];
            for (int i = 0; i < this.tileHeight; i++) {
                String line = reader.readLine();
                String[] chars = line.split(" ");
                for (int j = 0; j < this.tileWidth; j++) {
                    if (j >= chars.length) {
                        return;
                    }
                    map[i][j] = Integer.parseInt(chars[j]);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public int[][] getMap() {
        return this.map;
    }

    public int getWidth() {
        return this.tileWidth;
    }

    public int getHeight() {
        return this.tileHeight;
    }

    public int getTileSize() {
        return this.tileSize;
    }
}

1 个答案:

答案 0 :(得分:0)

我不完全理解您的代码,但我建议在此代码块中

for (int i = 0; i < this.map.getHeight(); i++) {
    for (int j = 0; j < this.map.getWidth(); j++) {
你说的话是

currentRow = map[myPlayer.getX()-this.map.getWidth()/2][myPlayer.getY()-this.map.getHeight()/2]

然后按原样保留其余的绘图代码。