From 2e22c68df9761b1079bb578ddfca37c415748830 Mon Sep 17 00:00:00 2001
From: Peter <pt033@hdm-stuttgart.de>
Date: Thu, 18 Jan 2024 01:13:16 +0100
Subject: [PATCH] add(ui): add basic loading screen #14

---
 .../Controller/IntroController.java           |  42 ++-----
 .../Controller/LoadingScreenController.java   | 107 ++++++++++++++++++
 src/main/resources/fxml/LoadingScreen.fxml    |  27 +++++
 src/main/resources/player/appSettings.json    |   2 +-
 src/main/resources/styles/style.css           |   4 +
 5 files changed, 147 insertions(+), 35 deletions(-)
 create mode 100644 src/main/java/de/hdm_stuttgart/battlearena/Controller/LoadingScreenController.java
 create mode 100644 src/main/resources/fxml/LoadingScreen.fxml

diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Controller/IntroController.java b/src/main/java/de/hdm_stuttgart/battlearena/Controller/IntroController.java
index 0ee284ce..240ff559 100644
--- a/src/main/java/de/hdm_stuttgart/battlearena/Controller/IntroController.java
+++ b/src/main/java/de/hdm_stuttgart/battlearena/Controller/IntroController.java
@@ -50,11 +50,7 @@ public class IntroController implements Initializable {
 
         mediaPlayer.setOnEndOfMedia(() -> {
             if (counter == videos.length - 1) {
-                try {
-                    videoEnd();
-                } catch (IOException e) {
-                    throw new RuntimeException(e);
-                }
+                videoEnd();
             } else {
                 nextVideo();
             }
@@ -63,42 +59,20 @@ public class IntroController implements Initializable {
 //        TODO: make any button pressed work
         introParent.setOnMouseClicked((mouseEvent) -> {
             if (counter == videos.length - 1) {
-                try {
-                    videoEnd();
-                } catch (IOException e) {
-                    throw new RuntimeException(e);
-                }
+                videoEnd();
             } else {
                 nextVideo();
             }
         });
     }
 
-    private void videoEnd() throws IOException {
-        boolean isStartUp = true, noAccount = false, statsLoaded = false;
-        while (isStartUp) {
-            try {
-                persistence.loadPlayerAccount();
-                persistence.verifyPlayerAccount();
-                mediaPlayer.dispose();
-                isStartUp = false;
-            } catch (DatabaseException e) {
-                noAccount = true;
-//            TODO: reset player account if account fails to load in loadPlayerAccount()
-            }
-            if (noAccount) {
-                introParent.getScene().setRoot(FXMLLoader.load(Objects.requireNonNull(getClass().getResource("/fxml/PlayerCreateScene.fxml"))));
-            }
-        }
-        while (!statsLoaded) { // while loop because Martin said so
-            try {
-                persistence.loadPlayerStatistics();
-                statsLoaded = true;
-            } catch (DatabaseException e) {
-                log.error(e);
-            }
+    private void videoEnd() {
+        try {
+            mediaPlayer.dispose();
+            introParent.getScene().setRoot(FXMLLoader.load(Objects.requireNonNull(getClass().getResource("/fxml/LoadingScreen.fxml"))));
+        } catch (IOException e) {
+            throw new RuntimeException();
         }
-        introParent.getScene().setRoot(FXMLLoader.load(Objects.requireNonNull(getClass().getResource("/fxml/MenuBorderPane.fxml"))));
     }
 
     private void nextVideo() {
diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Controller/LoadingScreenController.java b/src/main/java/de/hdm_stuttgart/battlearena/Controller/LoadingScreenController.java
new file mode 100644
index 00000000..df510df0
--- /dev/null
+++ b/src/main/java/de/hdm_stuttgart/battlearena/Controller/LoadingScreenController.java
@@ -0,0 +1,107 @@
+package de.hdm_stuttgart.battlearena.Controller;
+
+import de.hdm_stuttgart.battlearena.Model.DataStorage.Classes.Exceptions.DatabaseException;
+import de.hdm_stuttgart.battlearena.Model.DataStorage.Classes.Persistence;
+import javafx.application.Platform;
+import javafx.fxml.FXML;
+import javafx.fxml.FXMLLoader;
+import javafx.fxml.Initializable;
+import javafx.scene.control.Label;
+import javafx.scene.layout.BorderPane;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.*;
+import java.util.concurrent.TimeUnit;
+
+public class LoadingScreenController implements Initializable {
+    @FXML
+    private Label tips;
+    @FXML
+    private BorderPane parent;
+
+    private int counter = 0;
+
+    private boolean statsLoaded = false;
+
+    private final String[] loadingTips = {
+            "Did you know, that since 2009, Martin isn't on the FBI list?",
+            "BattleArena is actually derived from the combination of the words 'Battle' and 'Arena'.",
+            "Yannik ist over 1,80m tall.",
+            "Electricity is a vital part to compute this game.",
+            "There is a typo in one of the tips.",
+            "Over 1000 people play games every day.",
+            "A lot of people believe Nikola Tesla was the first to invent electricity but some clouds beat him to it.",
+            "Monkeys usually don't have keys.",
+            "While you're reading this, you've wasted 2 seconds."};
+
+    private final List<String> shuffledTips = Arrays.asList(loadingTips);
+
+    private final Persistence persistence = Persistence.getInstance();
+    private static final Logger log = LogManager.getLogger(Persistence.class);
+
+
+    @Override
+    public void initialize(URL url, ResourceBundle resourceBundle) {
+        Collections.shuffle(shuffledTips);
+        Thread tipsThread = new Thread(() -> {
+            try {
+                setLoadingTips();
+            } catch (InterruptedException e) {
+                throw new RuntimeException(e);
+            }
+        });
+        tipsThread.start();
+        Thread loadingStats = new Thread(() -> {
+            try {
+                TimeUnit.SECONDS.sleep(4);
+                asd();
+            } catch (IOException e) {
+                throw new RuntimeException();
+            } catch (InterruptedException e) {
+                throw new RuntimeException(e);
+            }
+        });
+        loadingStats.start();
+    }
+
+    private void setLoadingTips() throws InterruptedException {
+        while (!statsLoaded) {
+            if (counter < loadingTips.length) {
+                String nextTip = shuffledTips.get(counter);
+                Platform.runLater(() -> tips.setText(nextTip)); // JavaFX method to queue a task that needs to be executed on the JavaFX application thread (update some ui components)
+                counter++;
+                TimeUnit.MILLISECONDS.sleep(3500);
+                setLoadingTips();
+            }
+        }
+    }
+
+    private void asd() throws IOException {
+        boolean isStartUp = true, noAccount = false;
+        while (isStartUp) {
+            try {
+                persistence.loadPlayerAccount();
+                persistence.verifyPlayerAccount();
+                isStartUp = false;
+            } catch (DatabaseException e) {
+                noAccount = true;
+//            TODO: reset player account if account fails to load in loadPlayerAccount()
+            }
+            if (noAccount) {
+                parent.getScene().setRoot(FXMLLoader.load(Objects.requireNonNull(getClass().getResource("/fxml/PlayerCreateScene.fxml"))));
+            }
+        }
+        while (!statsLoaded) { // while loop because Martin said so
+            try {
+                persistence.loadPlayerStatistics();
+                statsLoaded = true;
+            } catch (DatabaseException e) {
+                log.error(e);
+            }
+        }
+        parent.getScene().setRoot(FXMLLoader.load(Objects.requireNonNull(getClass().getResource("/fxml/MenuBorderPane.fxml"))));
+    }
+}
diff --git a/src/main/resources/fxml/LoadingScreen.fxml b/src/main/resources/fxml/LoadingScreen.fxml
new file mode 100644
index 00000000..78e7ff27
--- /dev/null
+++ b/src/main/resources/fxml/LoadingScreen.fxml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<?import javafx.scene.control.Label?>
+<?import javafx.scene.layout.BorderPane?>
+<?import javafx.scene.layout.StackPane?>
+<?import javafx.scene.layout.VBox?>
+<?import javafx.scene.media.MediaView?>
+<?import javafx.scene.text.Font?>
+
+<BorderPane fx:id="parent" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.hdm_stuttgart.battlearena.Controller.LoadingScreenController">
+   <center>
+      <StackPane BorderPane.alignment="CENTER">
+         <children>
+            <VBox alignment="CENTER">
+               <children>
+                  <MediaView fx:id="mediaView" fitHeight="200.0" fitWidth="200.0" />
+                  <Label fx:id="tips" text="If you see this, then something went wrong lmao" textFill="WHITE">
+                     <font>
+                        <Font size="60.0" />
+                     </font>
+                  </Label>
+               </children>
+            </VBox>
+         </children>
+      </StackPane>
+   </center>
+</BorderPane>
diff --git a/src/main/resources/player/appSettings.json b/src/main/resources/player/appSettings.json
index ec5bc62a..cdc51fe7 100644
--- a/src/main/resources/player/appSettings.json
+++ b/src/main/resources/player/appSettings.json
@@ -1,4 +1,4 @@
 {
   "sfxVolume": 50,
-  "musicVolume": 0
+  "musicVolume": 2
 }
\ No newline at end of file
diff --git a/src/main/resources/styles/style.css b/src/main/resources/styles/style.css
index e676c8da..2eef5146 100644
--- a/src/main/resources/styles/style.css
+++ b/src/main/resources/styles/style.css
@@ -13,6 +13,10 @@
     -fx-background-color: black;
 }
 
+/*#tips {*/
+/*    -fx-font-size: 60;*/
+/*}*/
+
 #mainMenu{
     -fx-background-image: url("../textures/images/background.png");
     -fx-background-size: cover;
-- 
GitLab