微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java 2D游戏相机问题

如何解决java 2D游戏相机问题

我开发了一个 2D Java 游戏,但在实现应该与英雄角色一起移动的相机类时遇到了困难。

这是相机类

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 类,我也返回我的 GameCamera

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) 的相机对象,因此起点位于左上角

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); // 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,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,2},{0,1,0},2,3,4,0}};
        return map[x][y];
    }
}

现在我卡住了,因为如果我创建一个窗口设置为 (100,100) 的相机,地图不会移动,它的长度和宽度变小,窗口的其余部分是白色的。如何解决

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。