|
|
@@ -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);
|
|
|
}
|