Jan 7 hónapja
szülő
commit
63c660ffee

+ 7 - 0
src/main/java/controller/GameController.java

@@ -6,6 +6,7 @@ import model.Item;
 import model.items.ITEM_NAME;
 import util.GAMESTATE;
 import view.GamePanel;
+import view.tile.interactive.Hut;
 
 import java.io.Serializable;
 
@@ -23,6 +24,12 @@ public class GameController implements Runnable, Serializable {
         this.gameModel = new GameModel();
         this.view = new GamePanel(this);
         setStartInventory();
+        setupInteractiveTiles();
+    }
+
+    private void setupInteractiveTiles() {
+        Hut hut = new Hut(23, 20, this);
+        view.tileManager.addInteractiveTile(hut);
     }
 
     private void setStartInventory() {

+ 2 - 16
src/main/java/view/GamePanel.java

@@ -13,16 +13,15 @@ import view.util.screenCoordinatesToWorld;
 import javax.swing.*;
 import java.awt.*;
 
-public class GamePanel extends JPanel implements screenCoordinatesToWorld {
+public class GamePanel extends JPanel{
 
     //Screen Settings
     final static int originalTileSize = 16;
-    final static int scale = 4;
+    public final static int scale = 4;
 
     int fps = 60;
 
     public GameController gameController;
-    public static int static_TileSize = originalTileSize * scale;
 
     public int tileSize = originalTileSize * scale;
     public int maxScreenCol = 16;
@@ -57,8 +56,6 @@ public class GamePanel extends JPanel implements screenCoordinatesToWorld {
         Inventory inventory = gameController.getModel().getInventory();
         this.inventoryView = new InventoryView(inventory);
 
-
-
         MouseListener mdl = new MouseListener(controller, this, camera, tileManager);
         this.addMouseListener(mdl);
         this.addMouseMotionListener(mdl);
@@ -110,10 +107,6 @@ public class GamePanel extends JPanel implements screenCoordinatesToWorld {
             GameSaver.saveGame(gameController);
             System.exit(0);
         }
-        if(gameState == GAMESTATE.PLAY){
-            ui.drawHut(g2);
-        }
-
         ui.draw(g2);
         g2.dispose();
     }
@@ -123,13 +116,6 @@ public class GamePanel extends JPanel implements screenCoordinatesToWorld {
         }
     }
 
-    public int getWorldX(int screenX){
-        return (int) (camera.worldX + screenX / (double) tileSize);
-    }
-
-    public int getWorldY(int screenY){
-        return (int) (camera.worldY + screenY / (double) tileSize);
-    }
 
     public GameController getGameController() {
         if(gameController == null){

+ 2 - 13
src/main/java/view/UI.java

@@ -2,7 +2,6 @@ package view;
 
 import util.GAMESTATE;
 import view.components.Button;
-import view.tile.interactive.Hut;
 import view.tile.interactive.InteractiveTile;
 
 import java.awt.*;
@@ -27,15 +26,6 @@ public class UI {
         if(gp.gameState == GAMESTATE.PAUSED){
             drawPauseScreen(g2, "Game paused");
         }
-        if(gp.gameState == GAMESTATE.PLAY){
-            drawHut(g2);
-        }
-    }
-    public void drawHut(Graphics2D g2){
-        Hut hut = new Hut(gp.getGameController());
-        hut.draw(g2);
-        interactiveTiles.add(hut);
-
     }
     public void handleMenuClick(int screenX, int screenY) {
         for (Button button : activeButtons) {
@@ -47,10 +37,9 @@ public class UI {
     }
     public void handleClick(int screenX, int screenY){
 
-        for (InteractiveTile tile : interactiveTiles) {
-            if (tile.wasClicked(screenX, screenY)) {
+        for (InteractiveTile tile : gp.tileManager.interactiveTiles) {
+            if (tile.isClicked(screenX, screenY, gp.camera)) {
                 tile.click();
-                break;
             }
         }
     }

+ 20 - 3
src/main/java/view/tile/TileManager.java

@@ -4,7 +4,6 @@ import controller.GameController;
 import model.Tile;
 import model.tiles.BackgroundTile;
 import view.GamePanel;
-import view.tile.interactive.Hut;
 import view.tile.interactive.InteractiveTile;
 
 import java.awt.*;
@@ -12,6 +11,7 @@ import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.util.ArrayList;
 
 public class TileManager {
 
@@ -21,7 +21,7 @@ public class TileManager {
     public int[][] mapTileNum;
     public Tile[][] mapTiles;
     public int mapTileOverflow = 1;
-
+    public ArrayList<InteractiveTile> interactiveTiles = new ArrayList<>();
     public TileManager(GamePanel gp){
         this.gamePanel = gp;
         tile = new Tile[10];
@@ -30,7 +30,9 @@ public class TileManager {
         loadMap("/maps/world01.txt");
 
     }
-
+    public void addInteractiveTile(InteractiveTile tile) {
+        interactiveTiles.add(tile);
+    }
     public void loadMap(String filePath){
         try{
             InputStream is = getClass().getResourceAsStream(filePath);
@@ -81,8 +83,10 @@ public class TileManager {
 
 
                 g2.drawImage(tile[tileNum].image, (int) screenX, (int) screenY, gamePanel.tileSize, gamePanel.tileSize, null);
+
             }
 
+
             worldCol++;
 
             if(worldCol == gamePanel.maxWorldCol){
@@ -90,8 +94,21 @@ public class TileManager {
                 worldRow++;
             }
         }
+        for (InteractiveTile tile : interactiveTiles) {
+            tile.updateCoordinates();
+            tile.draw(g2, gamePanel.camera);
+        }
+
+    }
+    public int worldColToScreenX(int worldCol) {
+        double worldX = worldCol * gamePanel.tileSize; // Reactively use tileSize
+        return (int) (worldX - gamePanel.camera.worldX + gamePanel.camera.screenX);
     }
 
+    public int worldRowToScreenY(int worldRow) {
+        double worldY = worldRow * gamePanel.tileSize; // Reactively use tileSize
+        return (int) (worldY - gamePanel.camera.worldY + gamePanel.camera.screenY);
+    }
     public void getTileImage(){
         try{
             setupTile(0, "grass");

+ 13 - 16
src/main/java/view/tile/interactive/Hut.java

@@ -1,28 +1,25 @@
 package view.tile.interactive;
 
 import controller.GameController;
+import view.Camera;
 import view.GamePanel;
 import view.popUpMenu.PopUpTile;
 import java.awt.*;
 
-public class Hut extends InteractiveTile{
+public class Hut extends InteractiveTile {
 
-    private static int xCoordinate =  0, yCoordinate = 0;
-    private static int width = 100, height = 100;
-    Graphics2D g2;
-
-    public Hut(GameController gc) {
-        super(height, width, xCoordinate, yCoordinate, () -> {
-            System.out.println("CLICKED HUT");
-            //PopUpTile popUpTile = new PopUpTile(g2, xCoordinate, yCoordinate, width, height);
+    public Hut(int worldGridX, int worldGridY, GameController gc) {
+        super(worldGridX, worldGridY, gc.getView().tileSize, gc.getView().tileSize, () -> {
+            System.out.println("Hut clicked!");
+            // Add click logic here
         }, gc);
     }
 
-    public void draw(Graphics2D g2) {
-        this.g2 = g2;
-        g2.setColor(new Color(0, 0, 0, 160)); // semi-transparent black
-        g2.fillRoundRect(xCoordinate, yCoordinate, width, height, 15, 15);
+    @Override
+    public void draw(Graphics2D g2, Camera camera) {
+        // Convert world to screen coordinates
+        int size = gameController.getView().tileSize;
+        g2.setColor(new Color(0, 0, 0, 160));
+        g2.fillRoundRect(this.screenX, this.screenY, size, size, 15, 15);
     }
-
-
-}
+}

+ 29 - 27
src/main/java/view/tile/interactive/InteractiveTile.java

@@ -3,41 +3,43 @@ package view.tile.interactive;
 import model.Tile;
 import java.awt.*;
 import controller.GameController;
+import view.Camera;
+import view.GamePanel;
 
-public abstract class InteractiveTile extends Tile{
-    // This class is used to create interactive tiles that can be clicked on
-    private int worldX, worldY;
-    private int height;
-    private int width;
-    private Runnable runnable;
-    public GameController gameController;
-    private int screenX, screenY;
+public abstract class InteractiveTile extends Tile {
+    public int worldGridX, worldGridY;
+    public int screenX, screenY;
+    protected int width, height;
+    protected Runnable onClick;
+    protected GameController gameController;
+
+    public InteractiveTile(int worldGridX, int worldGridY, int width, int height, Runnable onClick, GameController gc) {
+        this.worldGridX = worldGridX;
+        this.worldGridY = worldGridY;
+
+        this.screenX = gc.getView().tileManager.worldColToScreenX(worldGridX);
+        this.screenY = gc.getView().tileManager.worldRowToScreenY(worldGridY);
 
-    public InteractiveTile(int height, int width, int screenX, int screenY, Runnable c, GameController gc){
-        this.height = height;
         this.width = width;
-        runnable = c;
+        this.height = height;
+        this.onClick = onClick;
         this.gameController = gc;
-        this.screenX = screenX;
-        this.screenY = screenY;
-        this.worldX = gameController.getView().getWorldX(screenX);
-        this.worldY = gameController.getView().getWorldY(screenY);
     }
 
-    public void click(){
-        runnable.run();
-    }
-    public boolean wasClicked(int clickX, int clickY){
-        return clickX >= screenX && clickX <= screenX + width && clickY >= screenY && clickY <= screenY + height;
+    public void click() {
+        onClick.run();
     }
-    public abstract void draw(Graphics2D g2);
-    public void setDimensions(int height, int width){
-        this.height = height;
-        this.width = width;
+
+    public void updateCoordinates(){
+        this.screenX = gameController.getView().tileManager.worldColToScreenX(worldGridX);
+        this.screenY = gameController.getView().tileManager.worldRowToScreenY(worldGridY);
     }
+    public boolean isClicked(int mouseScreenX, int mouseScreenY, Camera camera) {
+        double mouseWorldGridX = (camera.worldX + mouseScreenX - camera.screenX) / gameController.getView().tileSize;
+        double mouseWorldGridY = (camera.worldY + mouseScreenY - camera.screenY) / gameController.getView().tileSize;
 
-    public void setScreenCoordinates(int x, int y){
-        this.worldX = x;
-        this.worldY = y;
+        return mouseWorldGridX == worldGridX &&  mouseWorldGridY == worldGridY;
     }
+
+    public abstract void draw(Graphics2D g2, Camera camera);
 }