diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/TimeAndWeatherController.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/TimeAndWeatherController.java new file mode 100644 index 0000000..c73f6b9 --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/TimeAndWeatherController.java @@ -0,0 +1,87 @@ +package edu.ntnu.idi.idatt2003.gruppe42.Controller; + +import edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers.AppController; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.Random; +import javafx.beans.property.IntegerProperty; +import javafx.beans.property.ObjectProperty; +import javafx.beans.property.SimpleIntegerProperty; +import javafx.beans.property.SimpleObjectProperty; +import javafx.beans.property.SimpleStringProperty; +import javafx.beans.property.StringProperty; + +/** + * Controller for managing game time and weather. + * Advances 1 hour per tick. + */ +public class TimeAndWeatherController implements AppController { + private final IntegerProperty hour = new SimpleIntegerProperty(0); + private final IntegerProperty dayIndex = new SimpleIntegerProperty(0); // 0 = Sunday, 1 = Monday, etc. + private final IntegerProperty temperature = new SimpleIntegerProperty(15); + private final StringProperty weather = new SimpleStringProperty("Sunny"); + private final Random random = new Random(); + + private static final String[] DAYS = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"}; + + public TimeAndWeatherController() { + updateWeather(); + } + + @Override + public void nextTick() { + int nextHour = hour.get() + 1; + if (nextHour >= 24) { + hour.set(0); + dayIndex.set((dayIndex.get() + 1) % 7); + updateWeather(); + } else { + hour.set(nextHour); + } + } + + private void updateWeather() { + // Basic seasonal/random temperature logic + // Range from -10 to 30 + int change = random.nextInt(11) - 5; // -5 to +5 + int newTemp = temperature.get() + change; + if (newTemp < -10) newTemp = -10; + if (newTemp > 30) newTemp = 30; + temperature.set(newTemp); + + if (newTemp <= 0) { + weather.set("Snow"); + } else { + // If warm, could be sunny or rain + if (random.nextBoolean()) { + weather.set("Sunny"); + } else { + weather.set("Rain"); + } + } + } + + public IntegerProperty hourProperty() { + return hour; + } + + public IntegerProperty dayIndexProperty() { + return dayIndex; + } + + public IntegerProperty temperatureProperty() { + return temperature; + } + + public StringProperty weatherProperty() { + return weather; + } + + public String getTimeString() { + return String.format("%02d:00", hour.get()); + } + + public String getDayOfWeekString() { + return DAYS[dayIndex.get()]; + } +} diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/ViewControllers/DesktopViewController.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/ViewControllers/DesktopViewController.java index 7443f94..c0ec44a 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/ViewControllers/DesktopViewController.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/ViewControllers/DesktopViewController.java @@ -6,6 +6,7 @@ import edu.ntnu.idi.idatt2003.gruppe42.Controller.GameController; import edu.ntnu.idi.idatt2003.gruppe42.Controller.MarketController; import edu.ntnu.idi.idatt2003.gruppe42.Controller.PopupController; +import edu.ntnu.idi.idatt2003.gruppe42.Controller.TimeAndWeatherController; import edu.ntnu.idi.idatt2003.gruppe42.Model.App; import edu.ntnu.idi.idatt2003.gruppe42.Model.Player; import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.AppStoreApp; @@ -36,6 +37,10 @@ public final class DesktopViewController { private final PopupController popupController; /** View for the desktop. */ private final DesktopView desktopView; + /** Player model. */ + private final Player player; + /** Time and weather controller. */ + private final TimeAndWeatherController timeAndWeatherController; /** * Constructs a new desktop view controller. @@ -47,6 +52,10 @@ public DesktopViewController( final Player player, final GameController gameController ) { + this.player = player; + this.timeAndWeatherController = new TimeAndWeatherController(); + gameController.addAppController(timeAndWeatherController); + List popups = new ArrayList<>(); AppStoreApp appStoreApp = new AppStoreApp(); @@ -83,6 +92,24 @@ public DesktopViewController( ); } + /** + * Returns the player model. + * + * @return the player model + */ + public Player getPlayer() { + return player; + } + + /** + * Returns the time and weather controller. + * + * @return the time and weather controller + */ + public TimeAndWeatherController getTimeAndWeatherController() { + return timeAndWeatherController; + } + /** * Returns the desktop view. * @@ -139,6 +166,14 @@ public Button createAppButton(final App type) { return button; } + /** + * Handles the settings button action. + */ + public void handleSettings() { + System.out.println("Settings button pressed"); + // Placeholder for settings logic + } + /** * Opens the correct popup based on the app type. * diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/ViewControllers/StartViewController.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/ViewControllers/StartViewController.java index 9a3eb7d..f62e659 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/ViewControllers/StartViewController.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/ViewControllers/StartViewController.java @@ -51,8 +51,8 @@ private void handleStart() { } private String resolveUsername(String input) { - if (username == null || username.trim().isEmpty() || !isValidName(username)) { - username = TATE_NAMES.get(new Random().nextInt(TATE_NAMES.size())); + if (input == null || input.trim().isEmpty() || !isValidName(input)) { + return TATE_NAMES.get(new Random().nextInt(TATE_NAMES.size())); } return input; } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Views/DesktopView.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Views/DesktopView.java index 35fa3ee..ddd43d1 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Views/DesktopView.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Views/DesktopView.java @@ -1,9 +1,14 @@ package edu.ntnu.idi.idatt2003.gruppe42.View.Views; +import edu.ntnu.idi.idatt2003.gruppe42.Controller.TimeAndWeatherController; import edu.ntnu.idi.idatt2003.gruppe42.Controller.ViewControllers.DesktopViewController; import edu.ntnu.idi.idatt2003.gruppe42.Model.App; import java.util.Objects; +import javafx.application.Platform; +import javafx.geometry.Insets; import javafx.geometry.Pos; +import javafx.scene.control.Button; +import javafx.scene.control.Label; import javafx.scene.image.Image; import javafx.scene.layout.Background; import javafx.scene.layout.BackgroundImage; @@ -13,9 +18,14 @@ import javafx.scene.layout.BorderPane; import javafx.scene.layout.ColumnConstraints; import javafx.scene.layout.GridPane; +import javafx.scene.layout.HBox; import javafx.scene.layout.Priority; +import javafx.scene.layout.Region; import javafx.scene.layout.RowConstraints; import javafx.scene.layout.StackPane; +import javafx.scene.paint.Color; +import javafx.scene.text.Font; +import javafx.scene.text.FontWeight; /** * View for the desktop. @@ -45,6 +55,7 @@ public DesktopView(final DesktopViewController desktopViewController) { public BorderPane getRoot() { GridPane grid = createGrid(); root.setCenter(grid); + root.setBottom(createBottomBar()); // Load background image from resources Image backgroundImage = new Image( @@ -129,4 +140,82 @@ private StackPane createCell() { return cell; } + /** + * Creates the bottom bar with settings, player name, and clock. + * + * @return the bottom bar HBox. + */ + private HBox createBottomBar() { + HBox bottomBar = new HBox(15); + bottomBar.setAlignment(Pos.CENTER_LEFT); + bottomBar.setPadding(new Insets(5, 15, 5, 15)); + bottomBar.setStyle("-fx-background-color: rgba(0, 0, 0, 0.7);"); + + // Settings Button + Button settingsButton = new Button("⚙"); + settingsButton.setStyle("-fx-background-color: transparent; -fx-text-fill: white; -fx-font-size: 18px;"); + settingsButton.setOnAction(e -> desktopViewController.handleSettings()); + + // Player Name + Label playerNameLabel = new Label("User: " + desktopViewController.getPlayer().getName()); + playerNameLabel.setTextFill(Color.WHITE); + playerNameLabel.setFont(Font.font("Arial", FontWeight.BOLD, 14)); + playerNameLabel.setMinWidth(Region.USE_PREF_SIZE); + + // Spacer to push clock to the right + Region spacer = new Region(); + HBox.setHgrow(spacer, Priority.ALWAYS); + spacer.setMinWidth(20); + + // Time and Weather elements + TimeAndWeatherController twc = desktopViewController.getTimeAndWeatherController(); + + Label weatherLabel = new Label(); + weatherLabel.setTextFill(Color.WHITE); + weatherLabel.setFont(Font.font("Arial", 14)); + + Label tempLabel = new Label(); + tempLabel.setTextFill(Color.WHITE); + tempLabel.setFont(Font.font("Arial", 14)); + + Label dayLabel = new Label(); + dayLabel.setTextFill(Color.WHITE); + dayLabel.setFont(Font.font("Arial", FontWeight.BOLD, 14)); + + Label clockLabel = new Label(); + clockLabel.setTextFill(Color.WHITE); + clockLabel.setFont(Font.font("Arial", FontWeight.BOLD, 14)); + + // Update labels when properties change + twc.hourProperty().addListener((obs, oldVal, newVal) -> Platform.runLater(() -> { + clockLabel.setText(twc.getTimeString()); + })); + + twc.dayIndexProperty().addListener((obs, oldVal, newVal) -> Platform.runLater(() -> { + dayLabel.setText(twc.getDayOfWeekString()); + })); + + twc.weatherProperty().addListener((obs, oldVal, newVal) -> Platform.runLater(() -> { + weatherLabel.setText(newVal); + })); + + twc.temperatureProperty().addListener((obs, oldVal, newVal) -> Platform.runLater(() -> { + tempLabel.setText(newVal + "°C"); + })); + + // Initial values + clockLabel.setText(twc.getTimeString()); + dayLabel.setText(twc.getDayOfWeekString()); + weatherLabel.setText(twc.weatherProperty().get()); + tempLabel.setText(twc.temperatureProperty().get() + "°C"); + + HBox rightInfo = new HBox(10); + rightInfo.setAlignment(Pos.CENTER_RIGHT); + rightInfo.setMinWidth(Region.USE_PREF_SIZE); + rightInfo.getChildren().addAll(weatherLabel, tempLabel, dayLabel, clockLabel); + + bottomBar.getChildren().addAll(settingsButton, playerNameLabel, spacer, rightInfo); + + return bottomBar; + } }