diff options
author | sowgro <tpoke.ferrari@gmail.com> | 2023-06-02 00:48:13 -0400 |
---|---|---|
committer | sowgro <tpoke.ferrari@gmail.com> | 2023-06-02 00:48:13 -0400 |
commit | 4e43d6f020d908ccd9b8a6b77803cac943da00ed (patch) | |
tree | ae0eb103049a5838ebc7f1de35cc16988a4f07d8 /src | |
parent | 48bfed142f7175809a43037fb695b6fcdc4146b1 (diff) | |
download | NPEhero-4e43d6f020d908ccd9b8a6b77803cac943da00ed.tar.gz NPEhero-4e43d6f020d908ccd9b8a6b77803cac943da00ed.tar.bz2 NPEhero-4e43d6f020d908ccd9b8a6b77803cac943da00ed.zip |
Finish levelUtility and project cleanup
Diffstat (limited to 'src')
-rw-r--r-- | src/devmenu/DebugMenu.java | 7 | ||||
-rw-r--r-- | src/devmenu/DiffEditor.java | 14 | ||||
-rw-r--r-- | src/devmenu/LevelEditor.java | 71 | ||||
-rw-r--r-- | src/devmenu/LevelList.java | 30 | ||||
-rw-r--r-- | src/devmenu/NotesEditor.java | 1 | ||||
-rw-r--r-- | src/gameplay/SongPlayer.java | 3 | ||||
-rw-r--r-- | src/gui/Driver.java | 14 | ||||
-rw-r--r-- | src/gui/LevelSelector.java | 3 | ||||
-rw-r--r-- | src/gui/LevelSurround.java | 1 | ||||
-rw-r--r-- | src/gui/Settings.java | 24 | ||||
-rw-r--r-- | src/main/Difficulty.java | 110 | ||||
-rw-r--r-- | src/main/Level.java | 95 | ||||
-rw-r--r-- | src/main/LevelController.java | 32 | ||||
-rw-r--r-- | src/main/SettingsController.java | 36 | ||||
-rw-r--r-- | src/sound/AudioFilePlayer.java | 100 | ||||
-rw-r--r-- | src/sound/ShortAudioPlayer.java | 170 |
16 files changed, 393 insertions, 318 deletions
diff --git a/src/devmenu/DebugMenu.java b/src/devmenu/DebugMenu.java index 9b68423..19fc8e1 100644 --- a/src/devmenu/DebugMenu.java +++ b/src/devmenu/DebugMenu.java @@ -3,7 +3,6 @@ package devmenu; import gui.Driver; import javafx.scene.Scene; import javafx.scene.control.Button; -import javafx.scene.control.TextField; import javafx.scene.layout.VBox; import javafx.stage.Stage; @@ -19,10 +18,6 @@ public class DebugMenu VBox primaryPane = new VBox(); public DebugMenu() { - Button levelCreator = new Button(); - levelCreator.setText("LEVEL CREATOR"); - levelCreator.setOnAction(e -> new LevelList()); - Button wallpaperTest = new Button(); wallpaperTest.setText("wallpaper trees"); wallpaperTest.setOnAction(e -> Driver.setBackground("assets/trees.png")); @@ -39,7 +34,7 @@ public class DebugMenu testVol.setText("print volumes"); testVol.setOnAction(e -> System.out.println("sfx:"+Driver.settingsController.effectsVol+" msc:"+Driver.settingsController.musicVol)); - primaryPane.getChildren().addAll(levelCreator,wallpaperTest,wallpaperTest2,wallpaperTest3,testVol); + primaryPane.getChildren().addAll(wallpaperTest,wallpaperTest2,wallpaperTest3,testVol); Scene primaryScene = new Scene(primaryPane); primaryStage.setScene(primaryScene); diff --git a/src/devmenu/DiffEditor.java b/src/devmenu/DiffEditor.java index fbc0d8a..6bcbd28 100644 --- a/src/devmenu/DiffEditor.java +++ b/src/devmenu/DiffEditor.java @@ -10,8 +10,15 @@ import main.Difficulty; public class DiffEditor { + /* + * this class is a layout class, most of its purpose is to place UI elements like Buttons within Panes like VBoxes. + * the creation of these UI elements are mostly not commented due to their repetitive and self explanatory nature. + * style classes are defined in the style.css file. + */ public DiffEditor(Difficulty diff) { + Stage primaryStage = new Stage(); + Text folderNameLabel = new Text("Folder name (ordered alphabetically)"); TextField folderName = new TextField(diff.thisDir.getName()); folderName.setDisable(true); @@ -31,21 +38,16 @@ public class DiffEditor Button editScores = new Button("Edit leaderboard"); Button save = new Button("Save"); - save.setOnAction(e -> { + save.setOnAction(e -> { //assigns text feilds to values diff.title = title.getText(); diff.bpm = Integer.parseInt(bpm.getText()); diff.numBeats = Integer.parseInt(numBeats.getText()); - if(! diff.thisDir.getName().equals(folderName.getText())); - { - //will rename - } diff.writeMetadata(); }); VBox main = new VBox(); main.getChildren().addAll(folderNameLabel,folderName,titleLabel,title,bpmLabel,bpm,numBeatsLabel,numBeats,editNotes,editScores,save); Scene scene = new Scene(main); - Stage primaryStage = new Stage(); primaryStage.setScene(scene); primaryStage.show(); } diff --git a/src/devmenu/LevelEditor.java b/src/devmenu/LevelEditor.java index 5e9a818..94509ac 100644 --- a/src/devmenu/LevelEditor.java +++ b/src/devmenu/LevelEditor.java @@ -1,7 +1,6 @@ package devmenu; -import javafx.beans.value.ChangeListener; -import javafx.beans.value.ObservableValue; +import java.io.File; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.Scene; @@ -12,14 +11,27 @@ import javafx.scene.control.TextField; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.text.Text; +import javafx.stage.FileChooser; import javafx.stage.Stage; +import javafx.stage.FileChooser.ExtensionFilter; import main.Difficulty; import main.Level; public class LevelEditor -{ +{ + private File selectedSong = null; + private File selectedPreview = null; + private File selectedBackground = null; + + /* + * this class is a layout class, most of its purpose is to place UI elements like Buttons within Panes like VBoxes. + * the creation of these UI elements are mostly not commented due to their repetitive and self explanatory nature. + * style classes are defined in the style.css file. + */ public LevelEditor(Level level) { + Stage primaryStage = new Stage(); + Text folderNameLabel = new Text("Folder name"); TextField folderName = new TextField(level.thisDir.getName()); folderName.setDisable(true); @@ -40,13 +52,35 @@ public class LevelEditor ColorPicker c4 = new ColorPicker(level.colors[3]); ColorPicker c5 = new ColorPicker(level.colors[4]); - ObservableList diffList2 = FXCollections.observableArrayList(); + Text filesLabel = new Text("Files"); + + FileChooser backgroundChooser = new FileChooser(); + backgroundChooser.getExtensionFilters().add(new ExtensionFilter("PNG", "*.png")); + Button backgroundButton = new Button("Import background PNG"); + backgroundButton.setOnAction(e -> {selectedBackground = backgroundChooser.showOpenDialog(primaryStage);}); + + FileChooser previewChooser = new FileChooser(); + previewChooser.getExtensionFilters().add(new ExtensionFilter("PNG", "*.png")); + Button previewButton = new Button("Import preview PNG"); + previewButton.setOnAction(e -> {selectedPreview = previewChooser.showOpenDialog(primaryStage);}); + + FileChooser songChooser = new FileChooser(); + songChooser.getExtensionFilters().add(new ExtensionFilter("WAV", "*.wav")); + Button songButton = new Button("Import song WAV"); + songButton.setOnAction(e -> selectedSong = songChooser.showOpenDialog(primaryStage)); + + Text diffLabel = new Text("Difficulties"); + + ObservableList<Difficulty> diffList2 = FXCollections.observableArrayList(); diffList2.addAll(level.getDiffList()); - ListView<Difficulty> diffList = new ListView(); + ListView<Difficulty> diffList = new ListView<Difficulty>(); diffList.setItems(diffList2); - diffList.setOnMouseClicked(e -> new DiffEditor(diffList.getSelectionModel().getSelectedItem())); - Text diffLabel = new Text("Difficulties"); + Button edit = new Button("Edit"); + edit.setOnAction(e -> new DiffEditor(diffList.getSelectionModel().getSelectedItem())); + + Button remove = new Button("Delete"); + remove.setOnAction(e -> level.removeDiff(diffList.getSelectionModel().getSelectedItem())); Button refresh = new Button("Refresh"); refresh.setOnAction(e -> { @@ -55,6 +89,9 @@ public class LevelEditor diffList.setItems(diffList2); }); + HBox buttons = new HBox(); + buttons.getChildren().addAll(edit,remove,refresh); + TextField newDiff = new TextField("new"); Button newDiffButton = new Button("add"); newDiffButton.setOnAction(e -> level.addDiff(newDiff.getText())); @@ -62,7 +99,7 @@ public class LevelEditor newDiffBox.getChildren().addAll(newDiff,newDiffButton); Button save = new Button("Save"); - save.setOnAction(e -> { + save.setOnAction(e -> { //asigns feilds to values level.setTitle(title.getText()); level.setArtist(artist.getText()); level.desc = desc.getText(); @@ -71,13 +108,25 @@ public class LevelEditor level.colors[2] = c3.getValue(); level.colors[3] = c4.getValue(); level.colors[4] = c5.getValue(); + if (selectedBackground != null && selectedBackground.exists()) + { + level.addFile(selectedBackground,"background.png"); + } + if (selectedPreview != null && selectedPreview.exists()) + { + level.addFile(selectedPreview,"preview.png"); + } + if (selectedSong != null) + { + level.addFile(selectedSong,"song.wav"); + } level.writeMetadata(); }); VBox main = new VBox(); - main.getChildren().addAll(folderNameLabel,folderName,titleLabel,title,artistLabel,artist,descLabel,desc,colorsLabel,c1,c2,c3,c4,c5,diffLabel,diffList,refresh,newDiffBox,save); - - Stage primaryStage = new Stage(); + main.getChildren().addAll(folderNameLabel,folderName,titleLabel,title,artistLabel,artist,descLabel,desc,colorsLabel, + c1,c2,c3,c4,c5,filesLabel,previewButton,backgroundButton,songButton,diffLabel,diffList,buttons,newDiffBox,save); + Scene scene = new Scene(main); primaryStage.setScene(scene); primaryStage.show(); diff --git a/src/devmenu/LevelList.java b/src/devmenu/LevelList.java index 66ff797..7eaca84 100644 --- a/src/devmenu/LevelList.java +++ b/src/devmenu/LevelList.java @@ -1,13 +1,6 @@ package devmenu; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.PrintWriter; -import java.io.UnsupportedEncodingException; - import gui.Driver; -import javafx.beans.value.ChangeListener; -import javafx.beans.value.ObservableValue; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.TableColumn; @@ -18,10 +11,17 @@ import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage; import main.Level; +import main.LevelController; public class LevelList { Stage primaryStage = new Stage(); + + /* + * this class is a layout class, most of its purpose is to place UI elements like Buttons within Panes like VBoxes. + * the creation of these UI elements are mostly not commented due to their repetitive and self explanatory nature. + * style classes are defined in the style.css file. + */ public LevelList() { //sets up table view: requires special getters, setters and constructors to work @@ -36,11 +36,19 @@ public class LevelList titleCol.setCellValueFactory(new PropertyValueFactory<Level, String>("title")); artistCol.setCellValueFactory(new PropertyValueFactory<Level, String>("artist")); - levels.setItems(Driver.levelController.levelList); - levels.setOnMouseClicked(e -> new LevelEditor(levels.getSelectionModel().getSelectedItem())); + levels.setItems(LevelController.levelList); + + Button edit = new Button("Edit"); + edit.setOnAction(e -> new LevelEditor(levels.getSelectionModel().getSelectedItem())); + + Button remove = new Button("Delete"); + remove.setOnAction(e -> gui.Driver.levelController.removeLevel(levels.getSelectionModel().getSelectedItem())); Button refresh = new Button("Refresh"); - refresh.setOnAction(e -> levels.setItems(Driver.levelController.levelList)); + refresh.setOnAction(e -> levels.setItems(LevelController.levelList)); + + HBox buttons = new HBox(); + buttons.getChildren().addAll(edit,remove,refresh); TextField newLevel = new TextField("new"); Button newLevelButton = new Button("add"); @@ -49,7 +57,7 @@ public class LevelList newLevelBox.getChildren().addAll(newLevel,newLevelButton); VBox main = new VBox(); - main.getChildren().addAll(levels,refresh,newLevelBox); + main.getChildren().addAll(levels,buttons,newLevelBox); Scene scene = new Scene(main); primaryStage.setScene(scene); primaryStage.show(); diff --git a/src/devmenu/NotesEditor.java b/src/devmenu/NotesEditor.java index 5f26890..400d547 100644 --- a/src/devmenu/NotesEditor.java +++ b/src/devmenu/NotesEditor.java @@ -1,7 +1,6 @@ package devmenu; import java.io.File; - import gameplay.Timer; import javafx.scene.Scene; import javafx.scene.control.Button; diff --git a/src/gameplay/SongPlayer.java b/src/gameplay/SongPlayer.java index f76f5e3..8745727 100644 --- a/src/gameplay/SongPlayer.java +++ b/src/gameplay/SongPlayer.java @@ -7,7 +7,6 @@ import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; -import java.util.logging.Level; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.UnsupportedAudioFileException; @@ -114,7 +113,7 @@ public class SongPlayer extends Pane { level = lvl; difficulty = d; pane = p; - music = new AudioFilePlayer(difficulty.song.getPath()); + music = new AudioFilePlayer(level.song.getPath()); System.out.println(d.bpm + " " + d.numBeats); diff --git a/src/gui/Driver.java b/src/gui/Driver.java index fdfe323..9e4fb37 100644 --- a/src/gui/Driver.java +++ b/src/gui/Driver.java @@ -1,16 +1,10 @@ package gui; -import javafx.util.Duration; -import javafx.animation.KeyFrame; -import javafx.animation.KeyValue; -import javafx.animation.Timeline; import javafx.application.Application; import javafx.application.Platform; -import javafx.beans.property.Property; import javafx.geometry.Side; import javafx.scene.Scene; import javafx.scene.image.Image; -import javafx.scene.image.ImageView; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyCombination; import javafx.scene.input.KeyEvent; @@ -23,7 +17,13 @@ import javafx.scene.layout.Pane; import javafx.stage.Stage; import main.LevelController; import main.SettingsController; -import org.json.simple.parser.ParseException; + +// import javafx.scene.image.ImageView; +// import javafx.beans.property.Property; +// import javafx.util.Duration; +// import javafx.animation.KeyFrame; +// import javafx.animation.KeyValue; +// import javafx.animation.Timeline; import devmenu.DebugMenu; diff --git a/src/gui/LevelSelector.java b/src/gui/LevelSelector.java index 9a5c6df..07ace6e 100644 --- a/src/gui/LevelSelector.java +++ b/src/gui/LevelSelector.java @@ -11,6 +11,7 @@ import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import javafx.scene.layout.VBox; import main.Level; +import main.LevelController; public class LevelSelector extends Pane { @@ -33,7 +34,7 @@ public class LevelSelector extends Pane titleCol.setCellValueFactory(new PropertyValueFactory<Level, String>("title")); artistCol.setCellValueFactory(new PropertyValueFactory<Level, String>("artist")); - levels.setItems(Driver.levelController.levelList); + levels.setItems(LevelController.levelList); levels.prefWidthProperty().bind(super.prefWidthProperty().multiply(0.25)); levels.prefHeightProperty().bind(super.prefHeightProperty().multiply(0.75)); diff --git a/src/gui/LevelSurround.java b/src/gui/LevelSurround.java index 3f10925..d5dcd33 100644 --- a/src/gui/LevelSurround.java +++ b/src/gui/LevelSurround.java @@ -18,7 +18,6 @@ import javafx.scene.text.Text; import main.Difficulty; import main.Level; import main.ScoreController; -import sound.AudioFilePlayer; public class LevelSurround extends Pane { diff --git a/src/gui/Settings.java b/src/gui/Settings.java index 0d757f5..056c71e 100644 --- a/src/gui/Settings.java +++ b/src/gui/Settings.java @@ -4,6 +4,7 @@ import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.control.Button; import javafx.scene.control.Slider; +import javafx.scene.control.ToggleButton; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; @@ -65,23 +66,42 @@ public class Settings extends Pane fullBox.setPadding(new Insets(10)); + Text devLabel = new Text("Advanced"); + devLabel.getStyleClass().add("t3"); + + Button levelEdit = new Button("Level Utility"); + levelEdit.setOnAction(e -> new devmenu.LevelList()); + Button devMenu = new Button(); devMenu.setText("Debug Menu"); devMenu.setOnAction(e -> Driver.debug.show()); + VBox devBox = new VBox(); + devBox.getStyleClass().add("box"); + devBox.getChildren().addAll(devLabel,levelEdit,devMenu); + devBox.setVisible(false); + devBox.setManaged(false); + devBox.setPadding(new Insets(10)); + + ToggleButton advanced = new ToggleButton("Advanced"); + advanced.getStyleClass().remove("toggle-button"); + advanced.getStyleClass().add("button"); + advanced.selectedProperty().bindBidirectional(devBox.managedProperty()); + advanced.selectedProperty().bindBidirectional(devBox.visibleProperty()); + Button exit = new Button(); exit.setText("Back"); exit.setOnAction(e -> Driver.setMenu(new MainMenu())); BorderPane buttonBox = new BorderPane(); buttonBox.setLeft(exit); - buttonBox.setRight(devMenu); + buttonBox.setRight(advanced); VBox options = new VBox(); options.setSpacing(10); options.setAlignment(Pos.CENTER); - options.getChildren().addAll(musicBox,SFXBox,fullBox,buttonBox); + options.getChildren().addAll(musicBox,SFXBox,fullBox,devBox,buttonBox); options.maxWidthProperty().bind(super.prefWidthProperty().multiply(0.25)); options.setMinWidth(400); options.prefHeightProperty().bind(super.prefHeightProperty()); diff --git a/src/main/Difficulty.java b/src/main/Difficulty.java index c5dfca3..18dabb3 100644 --- a/src/main/Difficulty.java +++ b/src/main/Difficulty.java @@ -1,16 +1,13 @@ package main; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.time.LocalDate; - import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; - import javafx.collections.FXCollections; import javafx.collections.ObservableList; @@ -21,15 +18,20 @@ public class Difficulty private ObservableList<LeaderboardEntry> leaderboard = FXCollections.observableArrayList(); public File notes; public int bpm; - public File song; public int numBeats; - private File leaderboardFile; - public Difficulty(File file) + /** + * Creates a new Difficulty and gives it a file path + * @param newDir: The file path of the Difficulty + */ + public Difficulty(File newDir) { - thisDir = file; + thisDir = newDir; } + /** + * Checks for files in the difficulty folder and runs cooresponding actions + */ public void readData() { for(File cur: thisDir.listFiles()) //iterates through all files/folders in src/assets/levels/LEVEL/DIFFICULTY @@ -40,20 +42,18 @@ public class Difficulty } if (cur.getName().equals("leaderboard.json")) { - parseLeaderboard(cur); + parseLeaderboard(); } if (cur.getName().equals("notes.txt")) { notes = cur; } - if (cur.getName().equals("song.wav")) - { - song = cur; - } } } - + /** + * Reads in json metadata and assigns values to variables + */ public void parseMetadata() { File file = new File(thisDir, "metadata.json"); @@ -65,20 +65,44 @@ public class Difficulty JSONObject diffStuff = (JSONObject)(obj); //converts read object to a JSONObject title = (String) diffStuff.get("title"); - bpm = (int)(diffStuff.get("bpm")); - numBeats = (int)(diffStuff.get("numBeats")); - + bpm = Integer.parseInt(diffStuff.get("bpm")+""); + numBeats = Integer.parseInt(diffStuff.get("numBeats")+""); } catch (Exception e) { - System.out.println("Error in json file "+file); - //e.printStackTrace(); + e.printStackTrace(); } } - public void parseLeaderboard(File file) + /** + * Writes metadata to json file + */ + public void writeMetadata() { - leaderboardFile = file; + FileWriter fileWriter; + try + { + File file = new File(thisDir, "metadata.json"); + fileWriter = new FileWriter(file); + JSONObject obj = new JSONObject(); + obj.put("title", title); + obj.put("bpm", bpm); + obj.put("numBeats", numBeats); + obj.writeJSONString(fileWriter); + fileWriter.flush(); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + + /** + * Reads in json leaderboard and assigns populates list with leaderboardEntries + */ + public void parseLeaderboard() + { + File file = new File(thisDir, "leaderboard.json"); JSONParser jsonParser = new JSONParser(); //parser to read the file try(FileReader reader = new FileReader(file)) @@ -99,17 +123,20 @@ public class Difficulty } catch (Exception e) { - System.out.println("Error in json file "+leaderboardFile); - //e.printStackTrace(); + e.printStackTrace(); } } - public void addToLeaderboard(String name, int score) + /** + * Writes leaderboard to json file + */ + public void writeLeaderboard() { - leaderboard.add(new LeaderboardEntry(name, score, ""+LocalDate.now())); //do not delete this tho its not a placeholder - - try (FileWriter fileWriter = new FileWriter(leaderboardFile)) + FileWriter fileWriter; + try { + File file = new File(thisDir, "leaderboard.json"); + fileWriter = new FileWriter(file); //write the settings JSONObject instance to the file JSONArray jsonArray = new JSONArray(); for (LeaderboardEntry cur: leaderboard) @@ -129,6 +156,17 @@ public class Difficulty } } + /** + * Adds new leaderboardEntry to list and updates json file + * @param name: the players name + * @param score the players score + */ + public void addToLeaderboard(String name, int score) + { + leaderboard.add(new LeaderboardEntry(name, score, ""+LocalDate.now())); //do not delete this tho its not a placeholder + writeLeaderboard(); + } + public ObservableList<LeaderboardEntry> getLeaderboard() { return leaderboard; @@ -138,24 +176,4 @@ public class Difficulty { return title; } - - public void writeMetadata() - { - FileWriter fileWriter; - try - { - File file = new File(thisDir, "metadata.json"); - fileWriter = new FileWriter(file); - JSONObject obj = new JSONObject(); - obj.put("title", title); - obj.put("bpm", bpm); - obj.put("numBeats", numBeats); - obj.writeJSONString(fileWriter); - fileWriter.flush(); - } - catch (IOException e) - { - e.printStackTrace(); - } - } } diff --git a/src/main/Level.java b/src/main/Level.java index 56433f3..591bd9a 100644 --- a/src/main/Level.java +++ b/src/main/Level.java @@ -2,18 +2,17 @@ package main; import java.io.File; import java.util.ArrayList; +import java.util.Comparator; import javafx.scene.image.Image; import javafx.scene.paint.Color; import java.io.FileWriter; -import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; - -import devmenu.LevelList; -import gui.Driver; public class Level { @@ -26,12 +25,20 @@ public class Level public String desc = "No description"; public Image background; //optional public Color[] colors = {Color.RED,Color.BLUE,Color.GREEN,Color.PURPLE,Color.YELLOW};//optional, have default colors + public File song; - public Level(File dir) + /** + * Creates a new level and gives it a file path + * @param newDir: The path of the Level + */ + public Level(File newDir) { - thisDir = dir; + thisDir = newDir; } + /** + * Checks for files in the level folder and runs cooresponding actions + */ public void readData() { diffList = new ArrayList<Difficulty>(); @@ -43,24 +50,28 @@ public class Level diff.readData(); diffList.add(diff); } - - if (cur.getName().equals("preview.png")) - { - preview = new Image(cur.toURI().toString()); - } - if (cur.getName().equals("metadata.json")) { parseMetadata(); } - + if (cur.getName().equals("preview.png")) + { + preview = new Image(cur.toURI().toString()); + } if (cur.getName().equals("background.png")) { background = new Image(cur.toURI().toString()); } + if (cur.getName().equals("song.wav")) + { + song = cur; + } } } + /** + * Reads in json metadata and assigns values to variables + */ public void parseMetadata() { JSONParser jsonParser = new JSONParser(); //parser to read the file @@ -96,6 +107,9 @@ public class Level } } + /** + * Writes metadata to json file + */ public void writeMetadata() { FileWriter fileWriter; @@ -119,27 +133,70 @@ public class Level } } - public ArrayList<Difficulty> getDiffList() - { - return diffList; - } - + /** + * Adds a difficulty by creating a directory and required files + * @param text: the name of the directory and default title + */ public void addDiff(String text) { File diffDir = new File(thisDir, text); diffDir.mkdirs(); File metadataDir = new File(diffDir, "metadata.json"); + File leaderboardDir = new File(diffDir, "leaderboard.json"); + File notesDir = new File(diffDir, "notes.txt"); try { metadataDir.createNewFile(); + leaderboardDir.createNewFile(); + notesDir.createNewFile(); } catch (IOException e) { e.printStackTrace(); } Difficulty temp = new Difficulty(diffDir); temp.title = text; temp.writeMetadata(); + temp.writeLeaderboard(); + readData(); + } + + /** + * Removes the difficaulty from the filesystem then reloads the level + * @param diff: the difficulty to be removed + */ + public void removeDiff(Difficulty diff) + { + File hold = diff.thisDir; + diffList.remove(diff); + + try { + Files.walk(hold.toPath()) + .sorted(Comparator.reverseOrder()) + .map(Path::toFile) + .forEach(File::delete); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * Copies a file into the level directory + * @param newFile: the file to be copied + * @param name: the new file name + */ + public void addFile(File newFile, String name) + { + try { + Files.copy(newFile.toPath(), new File(thisDir, name).toPath(), StandardCopyOption.REPLACE_EXISTING); + } catch (IOException e) { + e.printStackTrace(); + } readData(); } + public ArrayList<Difficulty> getDiffList() + { + return diffList; + } + public String getTitle() { return title; diff --git a/src/main/LevelController.java b/src/main/LevelController.java index 9a94838..78d638a 100644 --- a/src/main/LevelController.java +++ b/src/main/LevelController.java @@ -2,6 +2,9 @@ package main; import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Comparator; import javafx.collections.FXCollections; import javafx.collections.ObservableList; @@ -11,11 +14,17 @@ public class LevelController File thisDir = new File("levels"); public static ObservableList<Level> levelList; + /** + * Creates a levelController, which holds all the levels + */ public LevelController() { readData(); } + /** + * Reads contents of folder and creates cooresponding levels + */ public void readData() { levelList = FXCollections.observableArrayList(); @@ -27,6 +36,10 @@ public class LevelController } } + /** + * Adds a level to the list by creating a directory and required files + * @param text: the name of the directory and default title + */ public void addLevel(String text) { File levelDir = new File(thisDir,text); @@ -45,4 +58,23 @@ public class LevelController temp.writeMetadata(); readData(); } + + /** + * Removes level from the filesystem then reloads this levelController + * @param level: the level to be removed + */ + public void removeLevel(Level level) + { + File hold = level.thisDir; + levelList.remove(level); + + try { + Files.walk(hold.toPath()) + .sorted(Comparator.reverseOrder()) + .map(Path::toFile) + .forEach(File::delete); + } catch (IOException e) { + e.printStackTrace(); + } + } }
\ No newline at end of file diff --git a/src/main/SettingsController.java b/src/main/SettingsController.java index f3bd671..4bd2b24 100644 --- a/src/main/SettingsController.java +++ b/src/main/SettingsController.java @@ -15,26 +15,9 @@ public class SettingsController {
public SimpleIntegerProperty effectsVol = new SimpleIntegerProperty(0);
public SimpleIntegerProperty musicVol = new SimpleIntegerProperty(0);
- private boolean fullscreen;
private JSONObject settings;
- public void saveAndWrite(int newEffVol, int newMusVol)
- {
- settings.put("musicVol", newMusVol);
- settings.put("effectsVol", newEffVol);
- try (FileWriter file = new FileWriter("settings.json"))
- {
- //write the settings JSONObject instance to the file
- file.write(settings.toJSONString());
- file.flush();
-
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- public void readFile() throws ParseException
+ public void read() throws ParseException
{
JSONParser jsonParser = new JSONParser(); //parser to read the file
@@ -57,5 +40,20 @@ public class SettingsController }
}
-
+
+ public void write(int newEffVol, int newMusVol)
+ {
+ settings.put("musicVol", newMusVol);
+ settings.put("effectsVol", newEffVol);
+ try (FileWriter file = new FileWriter("settings.json"))
+ {
+ //write the settings JSONObject instance to the file
+ file.write(settings.toJSONString());
+ file.flush();
+
+ }
+ catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
}
diff --git a/src/sound/AudioFilePlayer.java b/src/sound/AudioFilePlayer.java index 9fc1afc..9425881 100644 --- a/src/sound/AudioFilePlayer.java +++ b/src/sound/AudioFilePlayer.java @@ -1,22 +1,15 @@ -/*Name: - *Date: - *Period: - *Teacher: - *Description: - */ package sound; -//Java program to play audio files. imports file scanning and various -//methods from the java audio class in order to do so. + import java.io.File; import java.io.IOException; -import java.util.Scanner; - import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.UnsupportedAudioFileException; +//Java program to play audio files. imports file scanning and various +//methods from the java audio class in order to do so. public class AudioFilePlayer { @@ -38,101 +31,36 @@ public class AudioFilePlayer audioFile = new File(filePath); // create AudioInputStream object try { - audioInputStream = - AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile()); - } catch (UnsupportedAudioFileException e) { - // TODO Auto-generated catch block + audioInputStream = AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile()); + } + catch (UnsupportedAudioFileException e) { e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block + } + catch (IOException e) { e.printStackTrace(); } // create clip reference try { clip = AudioSystem.getClip(); - } catch (LineUnavailableException e) { - // TODO Auto-generated catch block + } + catch (LineUnavailableException e) { e.printStackTrace(); } // open audioInputStream to the clip try { clip.open(audioInputStream); - } catch (LineUnavailableException e) { - // TODO Auto-generated catch block + } + catch (LineUnavailableException e) { e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block + } + catch (IOException e) { e.printStackTrace(); } } - - // public static void main(String[] args) - // { - // try - // { - // filePath = "src/assets/BookBetrayal.wav3"; - // AudioFilePlayer audioPlayer = new AudioFilePlayer(); - - // audioPlayer.play(); - // Scanner sc = new Scanner(System.in); - - // while (true) //until the thread closes, ask the user what they want to do with the audio file - // { - // System.out.println("1. pause"); - // System.out.println("2. resume"); - // System.out.println("3. restart"); - // System.out.println("4. stop"); - // System.out.println("5. Jump to specific time"); - // int c = sc.nextInt(); - // audioPlayer.gotoChoice(c); - // if (c == 4) - // break; - // } - // sc.close(); - // } - - // catch (Exception ex) - // { - // System.out.println("Error with playing sound."); - // ex.printStackTrace(); - - // } - // } - - // Work as the user enters his choice - - public void gotoChoice(int c)throws IOException, LineUnavailableException, UnsupportedAudioFileException - { - //reads the users input and chooses what to do based on said input - switch (c) - { - case 1: - pause(); - break; - case 2: - resumeAudio(); - break; - case 3: - restart(); - break; - case 4: - stop(); - break; - case 5: - System.out.println("Enter time (" + 0 + - ", " + clip.getMicrosecondLength() + ")"); - Scanner sc = new Scanner(System.in); - long c1 = sc.nextLong(); - jump(c1); - break; - - } - - } // Method to play the audio public void play() diff --git a/src/sound/ShortAudioPlayer.java b/src/sound/ShortAudioPlayer.java index e81c4ee..77fb52c 100644 --- a/src/sound/ShortAudioPlayer.java +++ b/src/sound/ShortAudioPlayer.java @@ -1,103 +1,73 @@ -/*Name: - *Date: - *Period: - *Teacher: - *Description: - */ package sound; - //Java program to play audio files. imports file scanning and various - //methods from the java audio class in order to do so. - import java.io.File; - import java.io.IOException; - - import javax.sound.sampled.AudioFormat; - import javax.sound.sampled.AudioInputStream; - import javax.sound.sampled.AudioSystem; - import javax.sound.sampled.Clip; - import javax.sound.sampled.DataLine; - import javax.sound.sampled.LineEvent; - import javax.sound.sampled.LineListener; - import javax.sound.sampled.LineUnavailableException; - import javax.sound.sampled.UnsupportedAudioFileException; +import java.io.File; +import java.io.IOException; - public class ShortAudioPlayer implements LineListener - { - //indicates whether the playback completes or not - boolean playCompleted; - Clip audioClip; - - public void play(String audioFilePath) - { - File audioFile = new File(audioFilePath); - - try - { - //creates an audioInput object using the file we - //declared earlier - AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile); - - //gets the format of the audioStream object - AudioFormat format = audioStream.getFormat(); - - DataLine.Info info = new DataLine.Info(Clip.class, format); - - audioClip = (Clip) AudioSystem.getLine(info); - - audioClip.addLineListener(this); - - audioClip.open(audioStream); - - audioClip.start(); - - // while (!playCompleted) - // { - // // wait for the playback to complete - // try - // { - // Thread.sleep(1000); - // } - // catch (InterruptedException ex) - // { - // ex.printStackTrace(); - // } - // } - // audioClip.close(); //stops the audio clip - } - catch (UnsupportedAudioFileException ex) - { - System.out.println("The specified audio file is not supported."); - ex.printStackTrace(); - } - catch (LineUnavailableException ex) - { - System.out.println("Audio line for playing back is unavailable."); - ex.printStackTrace(); - } - catch (IOException ex) - { - System.out.println("Error playing the audio file."); - ex.printStackTrace(); - } - } - - - /** - * Listens to the START and STOP events of the audio line. - */ - @Override - public void update(LineEvent event) - { - LineEvent.Type type = event.getType(); - - // if (type == LineEvent.Type.START) - // { - // System.out.println("Playback started."); - // } - // else if (type == LineEvent.Type.STOP) - // { - // playCompleted = true; - // System.out.println("Playback completed."); - // } - } - } +import javax.sound.sampled.AudioFormat; +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.Clip; +import javax.sound.sampled.DataLine; +import javax.sound.sampled.LineEvent; +import javax.sound.sampled.LineListener; +import javax.sound.sampled.LineUnavailableException; +import javax.sound.sampled.UnsupportedAudioFileException; + +//Java program to play audio files. imports file scanning and various +//methods from the java audio class in order to do so. +public class ShortAudioPlayer implements LineListener +{ + //indicates whether the playback completes or not + boolean playCompleted; + Clip audioClip; + + public void play(String audioFilePath) + { + File audioFile = new File(audioFilePath); + + try + { + //creates an audioInput object using the file we + //declared earlier + AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile); + + //gets the format of the audioStream object + AudioFormat format = audioStream.getFormat(); + + DataLine.Info info = new DataLine.Info(Clip.class, format); + + audioClip = (Clip) AudioSystem.getLine(info); + + audioClip.addLineListener(this); + + audioClip.open(audioStream); + + audioClip.start(); + } + catch (UnsupportedAudioFileException ex) + { + System.out.println("The specified audio file is not supported."); + ex.printStackTrace(); + } + catch (LineUnavailableException ex) + { + System.out.println("Audio line for playing back is unavailable."); + ex.printStackTrace(); + } + catch (IOException ex) + { + System.out.println("Error playing the audio file."); + ex.printStackTrace(); + } + } + + + /** + * Listens to the START and STOP events of the audio line. + */ + @Override + public void update(LineEvent event) + { + //something should prolly go here + } +} |