|
|
@@ -7,6 +7,7 @@ import controller.factories.InteractiveTileFactory;
|
|
|
import controller.input.KeyHandler;
|
|
|
import controller.tiles.interactive.*;
|
|
|
import controller.tiles.interactive.upgradeable.*;
|
|
|
+import controller.ui.ShopController;
|
|
|
import model.GameModel;
|
|
|
import model.GameSaver;
|
|
|
import model.Inventory;
|
|
|
@@ -18,9 +19,6 @@ import model.tiles.InteractiveTileModel;
|
|
|
import model.tiles.InteractiveTileType;
|
|
|
import util.GAMESTATE;
|
|
|
import view.GamePanel;
|
|
|
-import view.tile.interactive.InteractiveTileView;
|
|
|
-
|
|
|
-import java.awt.*;
|
|
|
import java.io.Serializable;
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
@@ -38,10 +36,9 @@ public class GameController implements Runnable, Serializable {
|
|
|
private transient Thread gameThread;
|
|
|
private transient RessourceManager ressourceManager;
|
|
|
private transient GamePanel view;
|
|
|
+ private transient ShopController shopController;
|
|
|
public transient ArrayList<EntityController> entityControllers;
|
|
|
public transient ArrayList<InteractiveTileController> interactiveTileControllers;
|
|
|
- private transient InteractiveTileController draggingTile;
|
|
|
- private transient int residualShiftX, residualShiftY;
|
|
|
private transient boolean gameActive;
|
|
|
|
|
|
/**
|
|
|
@@ -56,9 +53,17 @@ public class GameController implements Runnable, Serializable {
|
|
|
initializeInteractiveTiles();
|
|
|
initializeEntities();
|
|
|
initResourceManager();
|
|
|
+ initShopController();
|
|
|
view.loadMap(worldPath);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Creates a new ShopController
|
|
|
+ */
|
|
|
+ private void initShopController() {
|
|
|
+ shopController = new ShopController(this);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Creates a new ResourceManager
|
|
|
*/
|
|
|
@@ -145,7 +150,7 @@ public class GameController implements Runnable, Serializable {
|
|
|
*
|
|
|
* @param tileController the interactive tile controller to add
|
|
|
*/
|
|
|
- private void addInteractiveTileController(InteractiveTileController tileController) {
|
|
|
+ public void addInteractiveTileController(InteractiveTileController tileController) {
|
|
|
interactiveTileControllers.add(tileController);
|
|
|
}
|
|
|
|
|
|
@@ -198,6 +203,17 @@ public class GameController implements Runnable, Serializable {
|
|
|
getModel().getInventory().select(slot);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Handles a click on the shop UI.
|
|
|
+ * @param x screen x-coordinate of click
|
|
|
+ * @param y screen y-coordinate of click
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public void handleShopClick(int x, int y) {
|
|
|
+ InteractiveTileController controller = view.getShopView().getClickedOffer(x, y);
|
|
|
+ shopController.setDraggingOffer(controller);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Starts the main game loop and resource manager thread.
|
|
|
*/
|
|
|
@@ -327,9 +343,8 @@ public class GameController implements Runnable, Serializable {
|
|
|
if (view.gameState == GAMESTATE.INVENTORY) {
|
|
|
view.gameState = GAMESTATE.PLAY;
|
|
|
getModel().getInventory().deselectAll();
|
|
|
- } else if (view.gameState == GAMESTATE.PLAY) {
|
|
|
+ } else
|
|
|
view.gameState = GAMESTATE.INVENTORY;
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -385,45 +400,12 @@ public class GameController implements Runnable, Serializable {
|
|
|
getView().generateNewWorld();
|
|
|
}
|
|
|
|
|
|
- private int originalGridX, originalGridY;
|
|
|
-
|
|
|
- /**
|
|
|
- * Called from GameMouseListener.mousePressed when starting a drag on right-click.
|
|
|
- */
|
|
|
- public void setDraggingTile(InteractiveTileController tile) {
|
|
|
- draggingTile = tile;
|
|
|
- if (draggingTile != null) {
|
|
|
- InteractiveTileModel model = draggingTile.getModel();
|
|
|
- originalGridX = model.getWorldGridX();
|
|
|
- originalGridY = model.getWorldGridY();
|
|
|
- // Optionally, ensure view's screen coords are set initially
|
|
|
- InteractiveTileView view = draggingTile.getView();
|
|
|
- int sx = worldColToScreenX(originalGridX);
|
|
|
- int sy = worldRowToScreenY(originalGridY);
|
|
|
- view.setScreenCoordinates(sx, sy);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Called from GameMouseListener.mousePressed to clear any residual state.
|
|
|
- * Retained for compatibility; residual shifting no longer used for mouse drag.
|
|
|
- */
|
|
|
- public void resetResidualShift() {
|
|
|
- // No-op or clear any previous state related to grid-shift dragging
|
|
|
- // Previously, residualShiftX/Y were used; now smooth drag uses pixel offsets via handleTileShift
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* Entry point for dragging movement: called from GameMouseListener.mouseDragged when right mouse button.
|
|
|
* dx, dy are screen pixel deltas.
|
|
|
*/
|
|
|
public void handleTileShift(int dx, int dy) {
|
|
|
- if (draggingTile == null) {
|
|
|
- return;
|
|
|
- }
|
|
|
- // Smooth pixel-based move: update view's screen coordinates by delta
|
|
|
- InteractiveTileView view = draggingTile.getView();
|
|
|
- view.setScreenCoordinates(view.getScreenX() + dx, view.getScreenY() + dy);
|
|
|
+ getView().tileManager.handleTileShift(dx, dy);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -431,130 +413,10 @@ public class GameController implements Runnable, Serializable {
|
|
|
* Snaps the tile to grid, validates position, reverts if invalid.
|
|
|
*/
|
|
|
public void handleTileRelease(int mouseX, int mouseY) {
|
|
|
- if (draggingTile == null) {
|
|
|
- return;
|
|
|
- }
|
|
|
- InteractiveTileView view = draggingTile.getView();
|
|
|
- InteractiveTileModel model = draggingTile.getModel();
|
|
|
-
|
|
|
- GamePanel gp = getView();
|
|
|
- int tileSize = gp.tileSize;
|
|
|
- int widthTiles = view.getScaleX();
|
|
|
- int heightTiles = view.getScaleY();
|
|
|
-
|
|
|
- // Compute the screen coordinates of the tile's top-left
|
|
|
- int dropScreenX = view.getScreenX();
|
|
|
- int dropScreenY = view.getScreenY();
|
|
|
- // Compute the center pixel position of the tile
|
|
|
- int centerScreenX = dropScreenX + (widthTiles * tileSize) / 2;
|
|
|
- int centerScreenY = dropScreenY + (heightTiles * tileSize) / 2;
|
|
|
- // Convert center to world grid coordinate (floor)
|
|
|
- int centerGridX = screenToWorldX(centerScreenX);
|
|
|
- int centerGridY = screenToWorldY(centerScreenY);
|
|
|
- // Compute top-left grid position so that tile center aligns to centerGrid
|
|
|
- int targetGridX = centerGridX - (widthTiles / 2);
|
|
|
- int targetGridY = centerGridY - (heightTiles / 2);
|
|
|
-
|
|
|
- if (isValidPosition(targetGridX, targetGridY)) {
|
|
|
- model.setWorldGridX(targetGridX);
|
|
|
- model.setWorldGridY(targetGridY);
|
|
|
- } else {
|
|
|
- // Revert to original grid position
|
|
|
- model.setWorldGridX(originalGridX);
|
|
|
- model.setWorldGridY(originalGridY);
|
|
|
- }
|
|
|
- // After updating model, reset view’s screen coords to the snapped grid position
|
|
|
- int snappedScreenX = worldColToScreenX(model.getWorldGridX());
|
|
|
- int snappedScreenY = worldRowToScreenY(model.getWorldGridY());
|
|
|
- view.setScreenCoordinates(snappedScreenX, snappedScreenY);
|
|
|
-
|
|
|
- // Clear dragging state
|
|
|
- draggingTile = null;
|
|
|
- }
|
|
|
- /**
|
|
|
- * Checks if draggingTile can be placed at new grid coordinates without out-of-bounds or collision.
|
|
|
- *
|
|
|
- * @param newX target grid X
|
|
|
- * @param newY target grid Y
|
|
|
- * @return true if valid position
|
|
|
- */
|
|
|
- private boolean isValidPosition(int newX, int newY) {
|
|
|
- if (draggingTile == null) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- InteractiveTileModel model = draggingTile.getModel();
|
|
|
- InteractiveTileView tileView = draggingTile.getView();
|
|
|
- int width = tileView.getScaleX();
|
|
|
- int height = tileView.getScaleY();
|
|
|
-
|
|
|
- // Check world bounds
|
|
|
- if (newX < 0 || newY < 0
|
|
|
- || newX + width > GamePanel.maxWorldCol
|
|
|
- || newY + height > GamePanel.maxWorldRow) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- // Check collisions
|
|
|
- for (int x = newX; x < newX + width; x++) {
|
|
|
- for (int y = newY; y < newY + height; y++) {
|
|
|
- InteractiveTileController other = getTileAt(x, y);
|
|
|
- if (other != null && other != draggingTile) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return true;
|
|
|
- }
|
|
|
- /**
|
|
|
- * Returns the InteractiveTileController at the specified grid coordinates, or null if none.
|
|
|
- *
|
|
|
- * @param worldX grid X coordinate
|
|
|
- * @param worldY grid Y coordinate
|
|
|
- * @return the tile controller at that position, or null
|
|
|
- */
|
|
|
- public InteractiveTileController getTileAt(int worldX, int worldY) {
|
|
|
- if (interactiveTileControllers == null) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- for (InteractiveTileController tile : interactiveTileControllers) {
|
|
|
- InteractiveTileModel model = tile.getModel();
|
|
|
- int tileX = model.getWorldGridX();
|
|
|
- int tileY = model.getWorldGridY();
|
|
|
- int width = tile.getView().getScaleX();
|
|
|
- int height = tile.getView().getScaleY();
|
|
|
-
|
|
|
- boolean withinX = (worldX >= tileX && worldX < tileX + width);
|
|
|
- boolean withinY = (worldY >= tileY && worldY < tileY + height);
|
|
|
- if (withinX && withinY) {
|
|
|
- return tile;
|
|
|
- }
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- // Ensure you have utility methods:
|
|
|
- private int worldColToScreenX(int worldCol) {
|
|
|
- GamePanel gp = getView();
|
|
|
- return gp.camera.screenX + worldCol * gp.tileSize - gp.camera.worldX;
|
|
|
- }
|
|
|
+ getView().tileManager.handleTileRelease(mouseX, mouseY);
|
|
|
|
|
|
- private int worldRowToScreenY(int worldRow) {
|
|
|
- GamePanel gp = getView();
|
|
|
- return gp.camera.screenY + worldRow * gp.tileSize - gp.camera.worldY;
|
|
|
}
|
|
|
|
|
|
- // screenToWorldX/Y assumed unchanged
|
|
|
- public int screenToWorldX(int screenX) {
|
|
|
- GamePanel gp = getView();
|
|
|
- return (gp.camera.worldX + screenX - gp.camera.screenX) / gp.tileSize;
|
|
|
- }
|
|
|
-
|
|
|
- public int screenToWorldY(int screenY) {
|
|
|
- GamePanel gp = getView();
|
|
|
- return (gp.camera.worldY + screenY - gp.camera.screenY) / gp.tileSize;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
/**
|
|
|
* Loads interactive tiles and entities from the saved GameModel.
|
|
|
*/
|
|
|
@@ -562,6 +424,7 @@ public class GameController implements Runnable, Serializable {
|
|
|
loadInteractiveTilesFromSave();
|
|
|
loadEntitiesFromSave();
|
|
|
initResourceManager();
|
|
|
+ initShopController();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -594,19 +457,6 @@ public class GameController implements Runnable, Serializable {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- *
|
|
|
- */
|
|
|
- public boolean isTileDragged(InteractiveTileController controller){
|
|
|
- return draggingTile == controller;
|
|
|
- }
|
|
|
- /**
|
|
|
- * Initializes the Main Menu for changing worlds etc.
|
|
|
- */
|
|
|
- public void loadMainMenu() {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
public boolean isRunning() {
|
|
|
return running;
|
|
|
}
|
|
|
@@ -615,8 +465,9 @@ public class GameController implements Runnable, Serializable {
|
|
|
getModel().getInventory().addToInventory(collected);
|
|
|
}
|
|
|
|
|
|
- public InteractiveTileController getDraggedTile() {
|
|
|
- return draggingTile;
|
|
|
+
|
|
|
+ public void toggleShop() {
|
|
|
+ getView().toggleShop();
|
|
|
}
|
|
|
}
|
|
|
|