GamePanel.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package view;
  2. import controller.MouseListener;
  3. import controller.MouseWheelZoom;
  4. import model.Inventory;
  5. import view.tile.TileManager;
  6. import util.GAMESTATE;
  7. import controller.KeyHandler;
  8. import view.util.screenCoordinatesToWorld;
  9. import javax.swing.*;
  10. import java.awt.*;
  11. public class GamePanel extends JPanel implements Runnable, screenCoordinatesToWorld {
  12. //Screen Settings
  13. final static int originalTileSize = 16;
  14. final static int scale = 4;
  15. int fps = 60;
  16. public Inventory inventory;
  17. public static int static_TileSize = originalTileSize * scale;
  18. public int tileSize = originalTileSize * scale;
  19. public int maxScreenCol = 16;
  20. public int maxScreenRow = 12;
  21. public int screenWidth = tileSize * maxScreenCol;
  22. public int screenHeight = tileSize * maxScreenRow;
  23. // World Settings
  24. public final int maxWorldCol = 50;
  25. public final int maxWorldRow = 50;
  26. public final int worldWidth = tileSize * maxWorldCol;
  27. public final int worldHeight = tileSize * maxWorldRow;
  28. //System
  29. public UI ui = new UI(this);
  30. // Game State
  31. public GAMESTATE gameState;
  32. TileManager tileManager = new TileManager(this);
  33. KeyHandler keyH = new KeyHandler(this);
  34. Thread gameThread;
  35. public InventoryView inventoryView = new InventoryView(this);
  36. public Camera camera = new Camera(this, keyH);
  37. public GamePanel(){
  38. inventory = new Inventory(this);
  39. MouseListener mdl = new MouseListener(this, camera, tileManager, inventoryView);
  40. this.addMouseListener(mdl);
  41. this.addMouseMotionListener(mdl);
  42. this.addMouseWheelListener(new MouseWheelZoom(this));
  43. this.setPreferredSize(new Dimension(screenWidth, screenHeight));
  44. this.setBackground(Color.BLACK);
  45. this.setDoubleBuffered(true);
  46. this.addKeyListener(keyH);
  47. this.setFocusable(true);
  48. gameState = GAMESTATE.PLAY;
  49. System.out.println("Screen Width: " + screenWidth);
  50. System.out.println("Screen Height: " + screenHeight);
  51. }
  52. public void zoomInOut(int i){
  53. if(gameState != GAMESTATE.PLAY){
  54. return;
  55. }
  56. int oldWorldWidth = tileSize * maxWorldCol;
  57. if((tileSize >= originalTileSize*scale/4 && i < 0) || (tileSize < (originalTileSize * scale) && i > 0)){
  58. tileSize += i;
  59. }
  60. int newWorldWidth = tileSize * maxWorldCol;
  61. double multiplier = (double)newWorldWidth/oldWorldWidth;
  62. double newCameraWorldX = camera.worldX * multiplier;
  63. double newCameraWorldY = camera.worldY * multiplier;
  64. camera.worldX = newCameraWorldX;
  65. camera.worldY = newCameraWorldY;
  66. }
  67. public void startGameThread(){
  68. gameThread = new Thread(this);
  69. gameThread.start();
  70. }
  71. public void clearAll(Graphics g){
  72. super.paintComponent(g);
  73. }
  74. public void paintComponent(Graphics g){
  75. super.paintComponent(g);
  76. Graphics2D g2 = (Graphics2D) g;
  77. tileManager.draw(g2);
  78. camera.draw(g2);
  79. if(gameState == GAMESTATE.INVENTORY){
  80. inventoryView.drawInventoryOverlay(g2);
  81. }
  82. // UI
  83. ui.draw(g2);
  84. g2.dispose();
  85. }
  86. public void update(){
  87. if(gameState == GAMESTATE.PLAY || gameState == GAMESTATE.INVENTORY){
  88. camera.update();
  89. }
  90. }
  91. @Override
  92. public void run() {
  93. double drawInterval = 1000000000/fps;
  94. double delta = 0;
  95. long lastTime = System.nanoTime();
  96. long currentTime;
  97. long timer = 0;
  98. int drawCount = 0;
  99. while (gameThread != null){
  100. currentTime = System.nanoTime();
  101. delta += (currentTime - lastTime) / drawInterval;
  102. timer += (currentTime - lastTime);
  103. lastTime = currentTime;
  104. if(delta >= 1){
  105. update();
  106. repaint();
  107. delta--;
  108. drawCount++;
  109. }
  110. if(timer >= 1000000000){
  111. //System.out.println("FPS: " + drawCount);
  112. drawCount = 0;
  113. timer = 0;
  114. }
  115. }
  116. }
  117. public int x(int screenX){
  118. return (int) (camera.worldX + screenX / (double) tileSize);
  119. }
  120. public int y(int screenY){
  121. return (int) (camera.worldY + screenY / (double) tileSize);
  122. }
  123. public void highlightTile(int screenX, int screenY) {
  124. System.out.println("Highlight X: " + screenX/tileSize+" Y: "+screenY/tileSize);
  125. }
  126. }