Răsfoiți Sursa

Merge branch 'master' of http://git.famrupp.de/schule/tiny_settlement_schule

 Conflicts:
	src/main/java/view/UI.java
Jan 7 luni în urmă
părinte
comite
a675b549f6

+ 6 - 5
src/main/java/controller/MouseListener.java

@@ -47,13 +47,14 @@ public class MouseListener extends MouseMotionAdapter implements java.awt.event.
 
     @Override
     public void mouseClicked(MouseEvent e) {
-        if (gp.gameState == GAMESTATE.INVENTORY && e.getButton() == MouseEvent.BUTTON1) {
+        if(gp.gameState == GAMESTATE.PLAY){
+            gp.ui.handleClick(e.getX(), e.getY());
+        }else if (gp.gameState == GAMESTATE.INVENTORY && e.getButton() == MouseEvent.BUTTON1) {
             controller.handleInventoryClick(e.getX(), e.getY());
-        }
-        if(gp.gameState == GAMESTATE.INVENTORY && e.getButton() == 1){
+        }else if(gp.gameState == GAMESTATE.INVENTORY && e.getButton() == 1){
             controller.handleInventoryClick(e.getX(), e.getY());
         }else if(gp.gameState == GAMESTATE.PAUSED){
-            gp.ui.handleClick(e.getX(), e.getY());
+            gp.ui.handleMenuClick(e.getX(), e.getY());
         }
     }
 
@@ -75,7 +76,7 @@ public class MouseListener extends MouseMotionAdapter implements java.awt.event.
             Tile tile = tileManager.tile[tileIndex];
 
             if(tile instanceof InteractiveTile it) {
-                it.onClick(); // Consumer wird aufgerufen
+                it.click(); // Consumer wird aufgerufen
             }
         }
     }

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

@@ -44,18 +44,21 @@ public class GamePanel extends JPanel implements screenCoordinatesToWorld {
 
 
 
-    TileManager tileManager = new TileManager(this);
+    public TileManager tileManager;
     private InventoryView inventoryView;
     public Camera camera;
 
 
     public GamePanel(GameController controller) {
+
         this.gameController = controller; // get from controller
         this.camera = new Camera(this, controller.getKeyHandler());
-
+        this.tileManager = new TileManager(this);
         Inventory inventory = gameController.getModel().getInventory();
         this.inventoryView = new InventoryView(inventory);
 
+
+
         MouseListener mdl = new MouseListener(controller, this, camera, tileManager);
         this.addMouseListener(mdl);
         this.addMouseMotionListener(mdl);
@@ -107,6 +110,9 @@ 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();
@@ -117,12 +123,18 @@ public class GamePanel extends JPanel implements screenCoordinatesToWorld {
         }
     }
 
-    public int x(int screenX){
+    public int getWorldX(int screenX){
         return (int) (camera.worldX + screenX / (double) tileSize);
     }
 
-    public int y(int screenY){
+    public int getWorldY(int screenY){
         return (int) (camera.worldY + screenY / (double) tileSize);
     }
 
+    public GameController getGameController() {
+        if(gameController == null){
+            gameController = new GameController();
+        }
+        return gameController;
+    }
 }

+ 22 - 3
src/main/java/view/UI.java

@@ -2,6 +2,8 @@ package view;
 
 import util.GAMESTATE;
 import view.components.Button;
+import view.tile.interactive.Hut;
+import view.tile.interactive.InteractiveTile;
 
 import java.awt.*;
 import java.util.ArrayList;
@@ -11,7 +13,7 @@ public class UI {
     private Graphics2D g2;
     private Font arial_40;
     private ArrayList<Button> activeButtons = new ArrayList<>();
-
+    private ArrayList<InteractiveTile> interactiveTiles = new ArrayList<>();
     public UI(GamePanel gp){
         this.gp = gp;
         arial_40 = new Font("Arial", Font.PLAIN, 80);
@@ -23,11 +25,19 @@ public class UI {
         g2.setColor(Color.white);
 
         if(gp.gameState == GAMESTATE.PAUSED){
-            drawPauseScreen(g2, "Game 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 handleClick(int screenX, int screenY) {
+    }
+    public void handleMenuClick(int screenX, int screenY) {
         for (Button button : activeButtons) {
             if (button.wasClicked(screenX, screenY)) {
                 button.click();
@@ -35,6 +45,15 @@ public class UI {
             }
         }
     }
+    public void handleClick(int screenX, int screenY){
+
+        for (InteractiveTile tile : interactiveTiles) {
+            if (tile.wasClicked(screenX, screenY)) {
+                tile.click();
+                break;
+            }
+        }
+    }
     private void drawPauseScreen(Graphics2D g2, String message) {
 
 

+ 1 - 0
src/main/java/view/components/Button.java

@@ -48,6 +48,7 @@ public class Button {
         this.height = height;
         this.width = width;
     }
+
     public void setScreenCoordinates(int x, int y){
         this.screenX = x;
         this.screenY = y;

+ 4 - 2
src/main/java/view/tile/TileManager.java

@@ -1,8 +1,10 @@
 package view.tile;
 
+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.*;
@@ -19,12 +21,14 @@ public class TileManager {
     public int[][] mapTileNum;
     public Tile[][] mapTiles;
     public int mapTileOverflow = 1;
+
     public TileManager(GamePanel gp){
         this.gamePanel = gp;
         tile = new Tile[10];
         mapTileNum = new int[gp.maxWorldCol][gp.maxWorldRow];
         getTileImage();
         loadMap("/maps/world01.txt");
+
     }
 
     public void loadMap(String filePath){
@@ -86,8 +90,6 @@ public class TileManager {
                 worldRow++;
             }
         }
-        //test
-        //InteractiveTile i = new InteractiveTile(0, 0, g2);
     }
 
     public void getTileImage(){

+ 20 - 2
src/main/java/view/tile/interactive/Hut.java

@@ -1,11 +1,29 @@
 package view.tile.interactive;
 
+import controller.GameController;
 import view.GamePanel;
+import view.popUpMenu.PopUpTile;
+import java.awt.*;
 
-public class Hut {
+public class Hut extends InteractiveTile{
 
-    public Hut(int xCoordinateRandom, int yCoordinateRandom) {
+    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, () -> {;
+            // Action to perform when the hut is clicked
+            //System.out.println("CLICKED HUT");
+            //PopUpTile popUpTile = new PopUpTile(g2, xCoordinate, yCoordinate, width, height);
+        }, 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);
+    }
+
+
 }

+ 33 - 44
src/main/java/view/tile/interactive/InteractiveTile.java

@@ -1,54 +1,43 @@
 package view.tile.interactive;
 
 import model.Tile;
-import view.GamePanel;
-import view.popUpMenu.PopUpTile;
-import view.util.screenCoordinatesToWorld;
-
 import java.awt.*;
-import java.awt.event.MouseEvent;
-import java.util.function.Consumer;
-
-public class InteractiveTile extends Tile{
-    private Consumer<Point> onClick;
-
-    private screenCoordinatesToWorld converter;
-
-    public static int xCoordinate1;
-    public static int yCoordinate1;
-    public static int xCoordinate2;
-    public static int yCoordinate2;
-    public Graphics2D g2;
-
-    public InteractiveTile(int xCoordinateRandom, int yCoordinateRandom, Graphics2D g2){
-        interactive = true;
-        xCoordinate1 = xCoordinateRandom;
-        yCoordinate1 = yCoordinateRandom;
-        xCoordinate2 = xCoordinateRandom + GamePanel.static_TileSize;
-        yCoordinate2 = yCoordinateRandom + GamePanel.static_TileSize;
-
-        this.g2 = g2;
+import controller.GameController;
+
+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 InteractiveTile(int height, int width, int screenX, int screenY, Runnable c, GameController gc){
+        this.height = height;
+        this.width = width;
+        runnable = c;
+        this.gameController = gc;
+        this.screenX = screenX;
+        this.screenY = screenY;
+        this.worldX = gameController.getView().getWorldX(screenX);
+        this.worldY = gameController.getView().getWorldY(screenY);
     }
 
-    public void setOnClick(Consumer<Point> pointConsumer){
-        onClick = pointConsumer;
+    public void click(){
+        runnable.run();
     }
-    public void onClick(){
-
-    };
-
-    public void isLeftClicked(MouseEvent e){
-        if(e.getButton() == MouseEvent.BUTTON1){
-            int x = e.getX();
-            int y = e.getY();
-
-            x = converter.x(x);
-            y = converter.y(y);
-
-            if(x >= xCoordinate1 && x <= xCoordinate2 && y >= yCoordinate1 && y <= yCoordinate2){
-                PopUpTile p = new PopUpTile(g2, 3, 3, 200, 200);
-            }
-        }
+    public boolean wasClicked(int clickX, int clickY){
+        return clickX >= screenX && clickX <= screenX + width && clickY >= screenY && clickY <= screenY + height;
+    }
+    public abstract void draw(Graphics2D g2);
+    public void setDimensions(int height, int width){
+        this.height = height;
+        this.width = width;
     }
 
+    public void setScreenCoordinates(int x, int y){
+        this.worldX = x;
+        this.worldY = y;
+    }
 }

+ 2 - 2
src/main/java/view/util/screenCoordinatesToWorld.java

@@ -1,6 +1,6 @@
 package view.util;
 
 public interface screenCoordinatesToWorld {
-    int x(int screenX);
-    int y(int screenY);
+    int getWorldX(int screenX);
+    int getWorldY(int screenY);
 }