From 9c7aa6cab36884d4dda8b3dbf50791f2e2d810d2 Mon Sep 17 00:00:00 2001 From: sowgro Date: Wed, 24 May 2023 17:24:54 -0400 Subject: add testSongPlayer (copy of newSongPlayer) to test the song player inside levelSurround --- src/fallTest/Driver.java | 4 +- src/fallTest/testSongPlayer.java | 278 +++++++++++++++++++++++++++++++++++++++ src/gui/LevelSurround.java | 5 +- src/gui/style.css | 10 +- src/main/LevelController.java | 70 +++++----- 5 files changed, 323 insertions(+), 44 deletions(-) create mode 100644 src/fallTest/testSongPlayer.java diff --git a/src/fallTest/Driver.java b/src/fallTest/Driver.java index 7e08e46..f6d7a2c 100644 --- a/src/fallTest/Driver.java +++ b/src/fallTest/Driver.java @@ -35,8 +35,8 @@ public class Driver extends Application primaryStage.setScene(primaryScene); newSongPlayer player = new newSongPlayer(); primaryStage.setTitle("TEST"); - primaryPane.getChildren().add(player); - setBackground("assets/water.png"); + primaryPane.getChildren().add(player); + setBackground("assets/water.png"); primaryStage.show(); player.init(); } diff --git a/src/fallTest/testSongPlayer.java b/src/fallTest/testSongPlayer.java new file mode 100644 index 0000000..cc98824 --- /dev/null +++ b/src/fallTest/testSongPlayer.java @@ -0,0 +1,278 @@ +package fallTest; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.Queue; + +import javafx.geometry.Pos; +import javafx.scene.input.KeyCode; +import javafx.scene.layout.HBox; +import javafx.scene.layout.Pane; +import javafx.scene.layout.StackPane; +import javafx.scene.layout.VBox; +import javafx.scene.paint.Color; +import javafx.scene.shape.Rectangle; +import javafx.animation.*; +import javafx.util.*; +import main.Difficulty; +import main.ScoreController; + +public class testSongPlayer extends Pane { + Timer timer = new Timer(); + final int TIME = 1500; // delay for notes falling down the screen + + Score scoreCounter = new Score(); + + Rectangle goalPerfect = new Rectangle(); + HBox buttonBox = new HBox(); + VBox polish = new VBox(); + VBox place = new VBox(); + + TButton dButton = new TButton(Color.RED, 50, 50, 5); + Queue dSends = new LinkedList(); // Queue that dictates when to send the notes + ArrayList dLane = new ArrayList(); // Array list containing all the notes currently on the field + + TButton fButton = new TButton(Color.BLUE, 50, 50, 5); + Queue fSends = new LinkedList(); + ArrayList fLane = new ArrayList(); + + TButton sButton = new TButton(Color.GREEN, 50, 50, 5); + Queue spaceSends = new LinkedList(); + ArrayList spaceLane = new ArrayList(); + + TButton jButton = new TButton(Color.PURPLE, 50, 50, 5); + Queue jSends = new LinkedList(); + ArrayList jLane = new ArrayList(); + + TButton kButton = new TButton(Color.YELLOW, 50, 50, 5); + Queue kSends = new LinkedList(); + ArrayList kLane = new ArrayList(); + + /** + * Establishes what the chart for the song is going to look like + */ + public void loadSong() { + dSends.add(new NoteInfo(4000)); + dSends.add(new NoteInfo(4333)); + dSends.add(new NoteInfo(4666)); + fSends.add(new NoteInfo(5000)); + kSends.add(new NoteInfo(5500)); + spaceSends.add(new NoteInfo(6000)); + jSends.add(new NoteInfo(6000)); + jSends.add(new NoteInfo(6250)); + dSends.add(new NoteInfo(6500)); + jSends.add(new NoteInfo(6750)); + spaceSends.add(new NoteInfo(7000)); + fSends.add(new NoteInfo(7500)); + jSends.add(new NoteInfo(7750)); + spaceSends.add(new NoteInfo(8000)); + fSends.add(new NoteInfo(8500)); + jSends.add(new NoteInfo(8500)); + dSends.add(new NoteInfo(9000)); + spaceSends.add(new NoteInfo(9000)); + kSends.add(new NoteInfo(9000)); + spaceSends.add(new NoteInfo(9500)); + + kSends.add(new NoteInfo(10000)); + dSends.add(new NoteInfo(10000)); + kSends.add(new NoteInfo(10333)); + fSends.add(new NoteInfo(10333)); + kSends.add(new NoteInfo(10666)); + spaceSends.add(new NoteInfo(10666)); + dSends.add(new NoteInfo(11000)); + spaceSends.add(new NoteInfo(11000)); + dSends.add(new NoteInfo(11333)); + + jSends.add(new NoteInfo(11333)); + dSends.add(new NoteInfo(11666)); + kSends.add(new NoteInfo(11666)); + spaceSends.add(new NoteInfo(12000)); + } + + public testSongPlayer(main.Level lvl, Difficulty d, Pane p, ScoreController cntrl) { + loadSong(); + + Rectangle field = new Rectangle(50, 50, new Color(0, 0, 0, 0.7)); + field.heightProperty().bind(super.heightProperty()); + field.widthProperty().bind(super.widthProperty()); + + goalPerfect.heightProperty().bind(super.heightProperty().divide(32)); + goalPerfect.heightProperty().bind(super.widthProperty()); + + genButton(dButton); + genButton(fButton); + genButton(sButton); + genButton(jButton); + genButton(kButton); + + super.setOnKeyPressed(e -> { + if (e.getCode() == KeyCode.D) { + checkNote(dLane, dButton); + } + if (e.getCode() == KeyCode.F) { + checkNote(fLane, fButton); + } + if (e.getCode() == KeyCode.SPACE) { + checkNote(spaceLane, sButton); + } + if (e.getCode() == KeyCode.J) { + checkNote(jLane, jButton); + } + if (e.getCode() == KeyCode.K) { + checkNote(kLane, kButton); + } + System.out.println("Score: " + scoreCounter.getScore() + "\nCombo: " + scoreCounter.getCombo() + "\n"); + }); + + // buttonBox.setStyle("-fx-padding: 0;" + "-fx-border-style: solid inside;" + // + "-fx-border-width: 0;" + "-fx-border-insets: 20;" + // + "-fx-background-color: black;" + "-fx-opacity: 0.67;"); + buttonBox.setAlignment(Pos.CENTER); + buttonBox.getChildren().addAll(dButton, fButton, sButton, jButton, kButton); + buttonBox.setSpacing(10); + + polish.getChildren().addAll(field); + polish.setAlignment(Pos.BASELINE_CENTER); + + place.prefWidthProperty().bind(super.widthProperty()); + place.prefHeightProperty().bind(super.heightProperty()); + place.getChildren().addAll(buttonBox); + place.setAlignment(Pos.BOTTOM_CENTER); + place.setSpacing(10); + + StackPane root = new StackPane(); + root.getChildren().addAll(polish, place); + + goalPerfect.setY(dButton.getY()); + super.getChildren().addAll(root, goalPerfect); + + gameLoop.start(); + } + + /** + * Checks if a note should be sent at the current time, and sends the note if it + * needs to be + * + * @param sends the queue to check + * @param lane the lane to send the note to + * @param pos the x pos of the note to be sent + * @param c the color of the sent note + */ + public void sendNote(Queue sends, ArrayList lane, double pos, Color c) { + if (sends.peek() != null && timer.time() > sends.peek().getTime()) { + TranslateTransition anim = new TranslateTransition(Duration.millis(TIME)); + + lane.add(new Block(c, 50, 50, 5)); + int index = lane.size() - 1; + sends.remove(); + lane.get(lane.size() - 1).heightProperty().bind(super.widthProperty().divide(8)); + lane.get(lane.size() - 1).widthProperty().bind(super.widthProperty().divide(8)); + lane.get(lane.size() - 1).setX(pos); + lane.get(index).setY(-lane.get(index).getHeight()); + anim.setByY(super.getHeight() + lane.get(index).getHeight()); + anim.setCycleCount(1); + anim.setAutoReverse(false); + anim.setNode(lane.get(lane.size() - 1)); + anim.play(); + + anim.setOnFinished(e -> { + if (super.getChildren().removeAll(anim.getNode())){ + scoreCounter.miss(); + } + }); + super.getChildren().add(lane.get(lane.size() - 1)); + } + } + + /** + * Sets up the given button + * + * @param button + */ + public void genButton(TButton button) { + button.heightProperty().bind(super.widthProperty().divide(8)); + button.widthProperty().bind(super.widthProperty().divide(8)); + button.setArcWidth(5); + button.setArcHeight(5); + button.setStrokeWidth(3); + } + + /** + * The background test that is run on every frame of the game + */ + AnimationTimer gameLoop = new AnimationTimer() { + + @Override + public void handle(long arg0) { + sendNote(dSends, dLane, dButton.getLayoutX(), Color.RED); + sendNote(fSends, fLane, fButton.getLayoutX(), Color.BLUE); + sendNote(spaceSends, spaceLane, sButton.getLayoutX(), Color.GREEN); + sendNote(jSends, jLane, jButton.getLayoutX(), Color.PURPLE); + sendNote(kSends, kLane, kButton.getLayoutX(), Color.YELLOW); + } + }; + + /** + * returns the pos in the lane array of the closest note to the goal + * + * @param searchLane + * @return the position of the note + */ + private int getClosestNote(ArrayList searchLane) { + int pos = 0; + + for (int i = 0; i < searchLane.size(); i++) { + if (distanceToGoal(searchLane.get(i)) < distanceToGoal(searchLane.get(pos))) { + pos = i; + } + } + return pos; + } + + /** + * Returns the distance to the goal of the given note + * + * @param note + * @return + */ + private double distanceToGoal(Block note) { + return Math.abs((super.getHeight() - note.getTranslateY()) - dButton.getY()); + } + + /** + * When the player hits the key, checks the quality of the hit + * @param lane the lane checking for a hit + * @param button the button checking for a hit + * @return 2 for a perfect hit, 1 for a good hit, 0 for a miss, and -1 if there are no notes to hit + */ + private int checkNote(ArrayList lane, TButton button) { + double distance = distanceToGoal(lane.get(getClosestNote(lane))); + if (lane.size() > 0 && distance < super.getHeight() / 3) { + + FillTransition ft = new FillTransition(Duration.millis(500), button); + ft.setToValue(button.getColor()); + + super.getChildren().removeAll(lane.get(getClosestNote(lane))); + lane.remove(lane.get(getClosestNote(lane))); + if (distance < super.getHeight() / 16) { + ft.setFromValue(Color.WHITE); + ft.play(); + scoreCounter.combo(); + scoreCounter.perfect(); + return 2; + } + if (distance < super.getHeight() / 5) { + ft.setFromValue(Color.CYAN); + ft.play(); + scoreCounter.combo(); + scoreCounter.good(); + return 1; + } + ft.setFromValue(Color.RED); + ft.play(); + scoreCounter.miss(); + return 0; + } + return -1; + } +} \ No newline at end of file diff --git a/src/gui/LevelSurround.java b/src/gui/LevelSurround.java index 2f9ec75..512a088 100644 --- a/src/gui/LevelSurround.java +++ b/src/gui/LevelSurround.java @@ -1,6 +1,7 @@ package gui; import fallTest.newSongPlayer; +import fallTest.testSongPlayer; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.control.Button; @@ -82,14 +83,14 @@ public class LevelSurround extends Pane comboTextBox.getChildren().addAll(comboLabel,comboDisplay); comboTextBox.setPadding(new Insets(10)); - Pane game = new Pane(); + testSongPlayer game = new testSongPlayer(level, difficulty, prev, sc); game.minWidthProperty().bind(super.prefHeightProperty().multiply(0.66)); game.minHeightProperty().bind(super.prefHeightProperty()); game.getStyleClass().add("box"); + comboTextBox.minWidthProperty().bind(super.prefWidthProperty().subtract(game.minWidthProperty()).divide(2)); scoreTextBox.minWidthProperty().bind(super.prefWidthProperty().subtract(game.minWidthProperty()).divide(2)); - new fallTest.newSongPlayer(level, difficulty, prev, sc); HBox centerBox = new HBox(); centerBox.getChildren().addAll(comboTextBox,game, scoreTextBox); diff --git a/src/gui/style.css b/src/gui/style.css index 04d7031..304ee9a 100644 --- a/src/gui/style.css +++ b/src/gui/style.css @@ -235,11 +235,11 @@ Slider:focused .thumb{ /* debug */ .debug { - -fx-background-radius: 5; - -fx-background-color: rgba(255, 0, 0, 0.281); - -fx-border-color: red; - -fx-text-fill: white; - -fx-border-width: 20; + /* -fx-background-radius: 5; */ + -fx-background-color: rgb(255, 0, 0); + /* -fx-border-color: red; */ + /* -fx-text-fill: white; */ + /* -fx-border-width: 20; */ } diff --git a/src/main/LevelController.java b/src/main/LevelController.java index 5d2654b..3e10ed3 100644 --- a/src/main/LevelController.java +++ b/src/main/LevelController.java @@ -12,41 +12,41 @@ public class LevelController public LevelController() { - // Difficulty d1 = new Difficulty(); - // d1.title = "Easy"; - // LeaderboardEntry lb = new LeaderboardEntry("t-bone", 1000, "DATE"); - // //lb.setName("t-bone"); - // //lb.setScore(1000); - // d1.leaderboard.add(lb); - - // Difficulty d2 = new Difficulty(); - // d2.title = "Medium"; - // Difficulty d3 = new Difficulty(); - // d3.title = "Hard"; - // Difficulty d4 = new Difficulty(); - // d4.title = "Expert"; - // Difficulty d5 = new Difficulty(); - // d5.title = "Impossible"; - - // Level testLevel = new Level("test level class","testArtist"); - // //testLevel.setTitle("test level class"); - // testLevel.desc = "this level is being used to test the LevelController class"; - // //testLevel.setAritst("testArtist"); - // testLevel.setColors(Color.RED, Color.BLUE, Color.GREEN, Color.ORANGE, Color.PURPLE); - // testLevel.diffList.add(d1); - // testLevel.diffList.add(d2); - // levelList.add(testLevel); - - // Level testLevel2 = new Level("another one", "testArtist2"); - // //testLevel2.setTitle("another one"); - // testLevel2.desc = "it can say something else too"; - // //testLevel2.setAritst("testArtist2"); - // testLevel2.setColors(Color.RED, Color.BLUE, Color.GREEN, Color.ORANGE, Color.PURPLE); - // testLevel2.diffList.add(d2); - // testLevel2.diffList.add(d3); - // testLevel2.diffList.add(d4); - // testLevel2.preview = new Image("assets/pico.png"); - // levelList.add(testLevel2); + Difficulty d1 = new Difficulty(); + d1.title = "Easy"; + LeaderboardEntry lb = new LeaderboardEntry("t-bone", 1000, "DATE"); + //lb.setName("t-bone"); + //lb.setScore(1000); + d1.leaderboard.add(lb); + + Difficulty d2 = new Difficulty(); + d2.title = "Medium"; + Difficulty d3 = new Difficulty(); + d3.title = "Hard"; + Difficulty d4 = new Difficulty(); + d4.title = "Expert"; + Difficulty d5 = new Difficulty(); + d5.title = "Impossible"; + + Level testLevel = new Level("test level class","testArtist"); + //testLevel.setTitle("test level class"); + testLevel.desc = "this level is being used to test the LevelController class"; + //testLevel.setAritst("testArtist"); + testLevel.setColors(Color.RED, Color.BLUE, Color.GREEN, Color.ORANGE, Color.PURPLE); + testLevel.diffList.add(d1); + testLevel.diffList.add(d2); + levelList.add(testLevel); + + Level testLevel2 = new Level("another one", "testArtist2"); + //testLevel2.setTitle("another one"); + testLevel2.desc = "it can say something else too"; + //testLevel2.setAritst("testArtist2"); + testLevel2.setColors(Color.RED, Color.BLUE, Color.GREEN, Color.ORANGE, Color.PURPLE); + testLevel2.diffList.add(d2); + testLevel2.diffList.add(d3); + testLevel2.diffList.add(d4); + testLevel2.preview = new Image("assets/pico.png"); + levelList.add(testLevel2); } -- cgit v1.2.3