|
|
@@ -1,7 +1,9 @@
|
|
|
package view;
|
|
|
|
|
|
+import controller.GameController;
|
|
|
import controller.MouseListener;
|
|
|
import controller.MouseWheelZoom;
|
|
|
+import model.GameSaver;
|
|
|
import model.Inventory;
|
|
|
import view.tile.TileManager;
|
|
|
import util.GAMESTATE;
|
|
|
@@ -11,7 +13,7 @@ import view.util.screenCoordinatesToWorld;
|
|
|
import javax.swing.*;
|
|
|
import java.awt.*;
|
|
|
|
|
|
-public class GamePanel extends JPanel implements Runnable, screenCoordinatesToWorld {
|
|
|
+public class GamePanel extends JPanel implements screenCoordinatesToWorld {
|
|
|
|
|
|
//Screen Settings
|
|
|
final static int originalTileSize = 16;
|
|
|
@@ -19,8 +21,7 @@ public class GamePanel extends JPanel implements Runnable, screenCoordinatesToWo
|
|
|
|
|
|
int fps = 60;
|
|
|
|
|
|
- public Inventory inventory;
|
|
|
-
|
|
|
+ public GameController gameController;
|
|
|
public static int static_TileSize = originalTileSize * scale;
|
|
|
|
|
|
public int tileSize = originalTileSize * scale;
|
|
|
@@ -44,16 +45,18 @@ public class GamePanel extends JPanel implements Runnable, screenCoordinatesToWo
|
|
|
|
|
|
|
|
|
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);
|
|
|
+ private InventoryView inventoryView;
|
|
|
+ public Camera camera;
|
|
|
+
|
|
|
|
|
|
+ public GamePanel(GameController controller) {
|
|
|
+ this.gameController = controller; // get from controller
|
|
|
+ this.camera = new Camera(this, controller.getKeyHandler());
|
|
|
|
|
|
- public GamePanel(){
|
|
|
- inventory = new Inventory(this);
|
|
|
+ Inventory inventory = gameController.getModel().getInventory();
|
|
|
+ this.inventoryView = new InventoryView(inventory);
|
|
|
|
|
|
- MouseListener mdl = new MouseListener(this, camera, tileManager, inventoryView);
|
|
|
+ MouseListener mdl = new MouseListener(controller, this, camera, tileManager);
|
|
|
this.addMouseListener(mdl);
|
|
|
this.addMouseMotionListener(mdl);
|
|
|
this.addMouseWheelListener(new MouseWheelZoom(this));
|
|
|
@@ -61,13 +64,10 @@ public class GamePanel extends JPanel implements Runnable, screenCoordinatesToWo
|
|
|
this.setPreferredSize(new Dimension(screenWidth, screenHeight));
|
|
|
this.setBackground(Color.BLACK);
|
|
|
this.setDoubleBuffered(true);
|
|
|
- this.addKeyListener(keyH);
|
|
|
+ this.addKeyListener(controller.getKeyHandler()); // now safe
|
|
|
this.setFocusable(true);
|
|
|
|
|
|
gameState = GAMESTATE.PLAY;
|
|
|
-
|
|
|
- System.out.println("Screen Width: " + screenWidth);
|
|
|
- System.out.println("Screen Height: " + screenHeight);
|
|
|
}
|
|
|
|
|
|
public void zoomInOut(int i){
|
|
|
@@ -85,31 +85,29 @@ public class GamePanel extends JPanel implements Runnable, screenCoordinatesToWo
|
|
|
double newCameraWorldX = camera.worldX * multiplier;
|
|
|
double newCameraWorldY = camera.worldY * multiplier;
|
|
|
|
|
|
- camera.worldX = newCameraWorldX;
|
|
|
- camera.worldY = newCameraWorldY;
|
|
|
+ camera.worldX = (int) newCameraWorldX;
|
|
|
+ camera.worldY = (int) newCameraWorldY;
|
|
|
}
|
|
|
-
|
|
|
- public void startGameThread(){
|
|
|
- gameThread = new Thread(this);
|
|
|
- gameThread.start();
|
|
|
+ public InventoryView getInventoryView(){
|
|
|
+ return inventoryView;
|
|
|
}
|
|
|
-
|
|
|
- public void clearAll(Graphics g){
|
|
|
- super.paintComponent(g);
|
|
|
- }
|
|
|
-
|
|
|
- public void paintComponent(Graphics g){
|
|
|
+ @Override
|
|
|
+ public void paintComponent(Graphics g) {
|
|
|
super.paintComponent(g);
|
|
|
-
|
|
|
Graphics2D g2 = (Graphics2D) g;
|
|
|
|
|
|
tileManager.draw(g2);
|
|
|
camera.draw(g2);
|
|
|
|
|
|
- if(gameState == GAMESTATE.INVENTORY){
|
|
|
+ if (gameState == GAMESTATE.INVENTORY) {
|
|
|
inventoryView.drawInventoryOverlay(g2);
|
|
|
}
|
|
|
- // UI
|
|
|
+
|
|
|
+ if (gameState == GAMESTATE.QUIT) {
|
|
|
+ GameSaver.saveGame(gameController);
|
|
|
+ System.exit(0);
|
|
|
+ }
|
|
|
+
|
|
|
ui.draw(g2);
|
|
|
g2.dispose();
|
|
|
}
|
|
|
@@ -119,34 +117,6 @@ public class GamePanel extends JPanel implements Runnable, screenCoordinatesToWo
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @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);
|
|
|
}
|
|
|
@@ -155,7 +125,4 @@ public class GamePanel extends JPanel implements Runnable, screenCoordinatesToWo
|
|
|
return (int) (camera.worldY + screenY / (double) tileSize);
|
|
|
}
|
|
|
|
|
|
- public void highlightTile(int screenX, int screenY) {
|
|
|
- System.out.println("Highlight X: " + screenX/tileSize+" Y: "+screenY/tileSize);
|
|
|
- }
|
|
|
}
|