package view; import controller.MouseListener; import controller.MouseWheelZoom; import model.Inventory; import view.tile.TileManager; import util.GAMESTATE; import controller.KeyHandler; import view.util.screenCoordinatesToWorld; import javax.swing.*; import java.awt.*; public class GamePanel extends JPanel implements Runnable, screenCoordinatesToWorld { //Screen Settings final static int originalTileSize = 16; final static int scale = 4; int fps = 60; public Inventory inventory; public static int static_TileSize = originalTileSize * scale; public int tileSize = originalTileSize * scale; public int maxScreenCol = 16; public int maxScreenRow = 12; public int screenWidth = tileSize * maxScreenCol; public int screenHeight = tileSize * maxScreenRow; // World Settings public final int maxWorldCol = 50; public final int maxWorldRow = 50; public final int worldWidth = tileSize * maxWorldCol; public final int worldHeight = tileSize * maxWorldRow; //System public UI ui = new UI(this); // Game State public GAMESTATE gameState; TileManager tileManager = new TileManager(this); KeyHandler keyH = new KeyHandler(this); Thread gameThread; public InventoryView inventoryView = new InventoryView(this); public Camera camera = new Camera(this, keyH); public GamePanel(){ inventory = new Inventory(this); MouseListener mdl = new MouseListener(this, camera, tileManager, inventoryView); this.addMouseListener(mdl); this.addMouseMotionListener(mdl); this.addMouseWheelListener(new MouseWheelZoom(this)); this.setPreferredSize(new Dimension(screenWidth, screenHeight)); this.setBackground(Color.BLACK); this.setDoubleBuffered(true); this.addKeyListener(keyH); this.setFocusable(true); gameState = GAMESTATE.PLAY; System.out.println("Screen Width: " + screenWidth); System.out.println("Screen Height: " + screenHeight); } public void zoomInOut(int i){ if(gameState != GAMESTATE.PLAY){ return; } int oldWorldWidth = tileSize * maxWorldCol; if((tileSize >= originalTileSize*scale/4 && i < 0) || (tileSize < (originalTileSize * scale) && i > 0)){ tileSize += i; } int newWorldWidth = tileSize * maxWorldCol; double multiplier = (double)newWorldWidth/oldWorldWidth; double newCameraWorldX = camera.worldX * multiplier; double newCameraWorldY = camera.worldY * multiplier; camera.worldX = newCameraWorldX; camera.worldY = newCameraWorldY; } public void startGameThread(){ gameThread = new Thread(this); gameThread.start(); } public void clearAll(Graphics g){ super.paintComponent(g); } public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; tileManager.draw(g2); camera.draw(g2); if(gameState == GAMESTATE.INVENTORY){ inventoryView.drawInventoryOverlay(g2); } // UI ui.draw(g2); g2.dispose(); } public void update(){ if(gameState == GAMESTATE.PLAY || gameState == GAMESTATE.INVENTORY){ camera.update(); } } @Override public void run() { double drawInterval = 1000000000/fps; double delta = 0; long lastTime = System.nanoTime(); long currentTime; long timer = 0; int drawCount = 0; while (gameThread != null){ currentTime = System.nanoTime(); delta += (currentTime - lastTime) / drawInterval; timer += (currentTime - lastTime); lastTime = currentTime; if(delta >= 1){ update(); repaint(); delta--; drawCount++; } if(timer >= 1000000000){ //System.out.println("FPS: " + drawCount); drawCount = 0; timer = 0; } } } public int x(int screenX){ return (int) (camera.worldX + screenX / (double) tileSize); } public int y(int screenY){ return (int) (camera.worldY + screenY / (double) tileSize); } public void highlightTile(int screenX, int screenY) { System.out.println("Highlight X: " + screenX/tileSize+" Y: "+screenY/tileSize); } }