Преглед на файлове

InteractiveTile in teilen implementiert

Oskar Berger преди 7 месеца
родител
ревизия
5e0cf0d489

+ 1 - 1
src/main/java/model/tiles/InteractiveTile.java

@@ -1,4 +1,4 @@
 package model.tiles;
 
-public abstract class InteractiveTile {
+public class InteractiveTile {
 }

+ 17 - 5
src/main/java/view/GamePanel.java

@@ -6,20 +6,23 @@ 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{
+public class GamePanel extends JPanel implements Runnable, screenCoordinatesToWorld {
 
     //Screen Settings
-    final int originalTileSize = 16;
-    final int scale = 4;
+    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;
@@ -62,6 +65,9 @@ public class GamePanel extends JPanel implements Runnable{
         this.setFocusable(true);
 
         gameState = GAMESTATE.PLAY;
+
+        System.out.println("Screen Width: " + screenWidth);
+        System.out.println("Screen Height: " + screenHeight);
     }
 
     public void zoomInOut(int i){
@@ -140,9 +146,15 @@ public class GamePanel extends JPanel implements Runnable{
             }
         }
     }
-    public void screenCoordinatesToWorld(int screenX, int screenY){
-        // Todo
+
+    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("X: " + screenX/tileSize+" Y: "+screenY/tileSize);
     }

+ 1 - 1
src/main/java/view/UI.java

@@ -6,7 +6,7 @@ import java.awt.*;
 
 public class UI {
     private GamePanel gp;
-    private Graphics2D g2;
+    private Graphics2D g2; //test
     private Font arial_40;
     private String message = "";
     private boolean messageOn;

+ 22 - 0
src/main/java/view/popUpMenu/PopUpTile.java

@@ -0,0 +1,22 @@
+package view.popUpMenu;
+
+import java.awt.*;
+
+public class PopUpTile {
+
+    public int overlayX;
+    public int overlayY;
+    public int overlayWidth;
+    public int overlayHeight;
+
+    public PopUpTile(Graphics2D g2, int x, int y, int width, int height) {
+        overlayX = x;
+        overlayY = y;
+        overlayWidth = width;
+        overlayHeight = height;
+
+        g2.setColor(new Color(0, 0, 0, 160)); // semi-transparent black
+        g2.fillRoundRect(overlayX, overlayY, overlayWidth, overlayHeight, 15, 15);
+    }
+
+}

+ 10 - 0
src/main/java/view/tile/TileManager.java

@@ -3,6 +3,7 @@ package view.tile;
 import model.Tile;
 import model.tiles.BackgroundTile;
 import view.GamePanel;
+import view.tile.interactive.InteractiveTile;
 
 import java.awt.*;
 import java.io.BufferedReader;
@@ -85,6 +86,8 @@ public class TileManager {
                 worldRow++;
             }
         }
+        //test
+        //InteractiveTile i = new InteractiveTile(0, 0, g2);
     }
 
     public void getTileImage(){
@@ -106,4 +109,11 @@ public class TileManager {
         tile[index] = new BackgroundTile();
         tile[index].setImage("/tiles/" + path + ".png");
     }
+
+    private void setupInteractiveTile( int index, String path) throws IOException {
+        tile[index] = new BackgroundTile();
+        tile[index].setImage("/tiles/" + path + ".png");
+    }
+
 }
+

+ 11 - 0
src/main/java/view/tile/interactive/Hut.java

@@ -0,0 +1,11 @@
+package view.tile.interactive;
+
+import view.GamePanel;
+
+public class Hut {
+
+    public Hut(int xCoordinateRandom, int yCoordinateRandom) {
+
+    }
+
+}

+ 41 - 4
src/main/java/view/tile/interactive/InteractiveTile.java

@@ -1,17 +1,54 @@
 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 abstract class InteractiveTile extends Tile {
+public class InteractiveTile extends Tile{
     private Consumer<Point> onClick;
-    public InteractiveTile(){
+
+    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;
     }
+
     public void setOnClick(Consumer<Point> pointConsumer){
         onClick = pointConsumer;
     }
-    public abstract void onClick();
-}
+    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);
+            }
+        }
+    }
+
+}

+ 6 - 0
src/main/java/view/util/screenCoordinatesToWorld.java

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