java 2D游戏摄像机问题

java 2D游戏摄像机问题,java,2d,game-development,Java,2d,Game Development,我开发了一个2D Java游戏,但我很难实现应该随英雄角色移动的camera类 这是照相课 package MyGame.Graphics; import MyGame.RefLinks; public class Camera { private RefLinks refLink; // RefLinks is a class which contains referencies to different objects in order to be easier to acce

我开发了一个2D Java游戏,但我很难实现应该随英雄角色移动的camera类

这是照相课

package MyGame.Graphics;

import MyGame.RefLinks;

public class Camera {

    private RefLinks refLink; // RefLinks is a class which contains referencies to different objects in order to be easier to acces it.
    private float xOffset, yOffset;
    public Camera(RefLinks reflink, float xOffset, float yOffset) {
        this.refLink = reflink;
        this.xOffset = xOffset;
        this.yOffset = yOffset;
    }
    public void move(float xAmt, float yAmt) {
        xOffset += xAmt;
        yOffset += yAmt;
    }

    public float getxOffset() {
        return xOffset;
    }

    public void setxOffset(float xOffset) {
        this.xOffset = xOffset;
    }

    public float getyOffset() {
        return yOffset;
    }

    public void setyOffset(float yOffset) {
        this.yOffset = yOffset;
    }
}

这是RefLinks类,在这里我还返回了我的游戏相机

package MyGame;

import MyGame.Graphics.Camera;
import MyGame.Maps.Map;

import MyGame.Input.KeyManager;

public class RefLinks
{
    private Game game;          
    private Map map;            
    private Camera camera;

    public RefLinks(Game game)
    {
        this.game = game;
    }

    public KeyManager GetKeyManager()
    {
        return game.GetKeyManager();
    }

    public int GetWidth()
    {
        return game.GetWidth();
    }

    public int GetHeight()
    {
        return game.GetHeight();
    }

    public Game GetGame()
    {
        return game;
    }

    public void SetGame(Game game)
    {
        this.game = game;
    }

    public Map GetMap()
    {
        return map;
    }

    public void SetMap(Map map)
    {
        this.map = map;
    }
    // this method returns the GameCamera. I called the method getGameCamera which I defined in Game class
    public Camera getGameCamera(){
        return game.getGameCamera();
    }

}

游戏课。这是我比赛的主要内容。在Init方法中,我创建了一个窗口设置为(0,0)的camera对象,因此起点位于左上角

package MyGame;

import MyGame.GameWindow.GameWindow;
import MyGame.Graphics.Assets;
import MyGame.Graphics.Camera;
import MyGame.Input.KeyManager;
import MyGame.States.*;
import MyGame.Tiles.Tile;

import java.awt.*;
import java.awt.image.BufferStrategy;


public class Game implements Runnable
{
    private Camera gameCamera;
    private GameWindow      wnd;       
    private boolean         runState;   
    private Thread          gameThread; 
    private BufferStrategy  bs;         
    private Graphics        g;          

        ///Available states
    private State playState;          
    private State menuState;          
    private State settingsState;       
    private State aboutState;           
    private KeyManager keyManager;      
    private RefLinks refLink;            
    private Tile tile; 

    public Game(String title, int width, int height)
    {
        wnd = new GameWindow(title, width, height);
        runState = false;
        keyManager = new KeyManager();
    }


    private void InitGame()
    {
        wnd.BuildGameWindow();
        wnd.GetWndFrame().addKeyListener(keyManager);
        Assets.Init();
        refLink = new RefLinks(this);
        gameCamera = new Camera(refLink, 0,0); // when I init the game I also ceate a camera set on point (0,0) (upper left corner)
        playState       = new PlayState(refLink);
        menuState       = new MenuState(refLink);
        settingsState   = new SettingsState(refLink);
        aboutState      = new AboutState(refLink);
        State.SetState(playState);
    }

    public void run()
    {
        InitGame();
        long oldTime = System.nanoTime();   
        long curentTime;                    
        final int framesPerSecond   = 60; 
        final double timeFrame      = 1000000000 / framesPerSecond; 
        while (runState == true)
        {         
            curentTime = System.nanoTime();
            if((curentTime - oldTime) > timeFrame)
            {
                Update();
                Draw();
                oldTime = curentTime;
            }
        }

    }

    public synchronized void StartGame()
    {
        if(runState == false)
        {      
            runState = true;
            gameThread = new Thread(this);
            gameThread.start();
        }
        else
        {
            return;
        }
    }

    public synchronized void StopGame()
    {
        if(runState == true)
        {

            runState = false;

            try
            {
                gameThread.join();
            }
            catch(InterruptedException ex)
            {
                ex.printStackTrace();
            }
        }
        else
        {
            return;
        }
    }

    private void Update()
    {
        keyManager.Update();

        if(State.GetState() != null)
        {
            State.GetState().Update();
        }
    }

    private void Draw()
    {
        bs = wnd.GetCanvas().getBufferStrategy();
        if(bs == null)
        {
            try
            {
                wnd.GetCanvas().createBufferStrategy(3);
                return;
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        g = bs.getDrawGraphics();
        g.clearRect(0, 0, wnd.GetWndWidth(), wnd.GetWndHeight());

            if(State.GetState() != null)
            {
                State.GetState().Draw(g);
            }
        bs.show();
        g.dispose();
    }

    public int GetWidth()
    {
        return wnd.GetWndWidth();
    }


    public int GetHeight()
    {
        return wnd.GetWndHeight();
    }


    public KeyManager GetKeyManager()
    {
        return keyManager;
    }

    //get game's camera
    public Camera getGameCamera(){
        return gameCamera;
    }
}

现在,在我完成Camera类之后,我尝试从Map类更新Draw方法。 地图类

package MyGame.Maps;

import MyGame.RefLinks;
import MyGame.Tiles.Tile;

import java.awt.*;

public class Map
{
    private RefLinks refLink;   
    private int width;          
    private int height;         
    private int [][] tiles;    


    public Map(RefLinks refLink)
    {
        this.refLink = refLink;
        LoadWorld();
    }

    public  void Update()
    {

    }

    public void Draw(Graphics g)
    {
        for(int y = 0; y < refLink.GetGame().GetHeight()/Tile.TILE_HEIGHT; y++)
        {
            for(int x = 0; x < refLink.GetGame().GetWidth()/Tile.TILE_WIDTH; x++)
            {
                GetTile(x, y).Draw(g, (int)(x * Tile.TILE_HEIGHT - refLink.getGameCamera().getxOffset()), (int)(y * Tile.TILE_WIDTH - refLink.getGameCamera().getyOffset()));
            }
        }
    }

    public Tile GetTile(int x, int y)
    {
        if(x < 0 || y < 0 || x >= width || y >= height)
        {
            return Tile.grassTile;
        }
        Tile t = Tile.tiles[tiles[x][y]];
        if(t == null)
        {
            return Tile.holeTile;
        }
        return t;
    }


    private void LoadWorld()
    {
        width = 100;
        height = 100;
        tiles = new int[width][height];
        for(int y = 0; y < height; y++)
        {
            for(int x = 0; x < width; x++)
            {
                tiles[x][y] = MiddleEastMap(y, x);
            }
        }
    }
     //the map is 100x100 tiles but for posting I made it 10x20 tiles
    private int MiddleEastMap(int x ,int y)
    {
        final int map[][] = {
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2},
        {0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
        return map[x][y];
    }
}
打包MyGame.Maps;
导入MyGame.RefLinks;
导入MyGame.Tiles.Tile;
导入java.awt.*;
公共类地图
{
私人重新链接重新链接;
私有整数宽度;
私人内部高度;
私有int[][]瓷砖;
公共地图(重新链接重新链接)
{
this.refLink=refLink;
LoadWorld();
}
公共无效更新()
{
}
公共空间绘制(图g)
{
对于(int y=0;y=宽度| | y>=高度)
{
返回瓷砖。草瓷砖;
}
Tile t=Tile.tiles[tiles[x][y]];
如果(t==null)
{
返回瓦片;
}
返回t;
}
私有void LoadWorld()
{
宽度=100;
高度=100;
瓷砖=新整型[宽度][高度];
对于(int y=0;y
现在我被卡住了,因为如果我创建一个窗口设置为(100100)的相机,例如,地图不会移动,它的长度和宽度会变小,窗口的其余部分是白色的。如何解决这个问题