From 229bd77d401beb005edfca1b3ce387e150ddd21d Mon Sep 17 00:00:00 2001 From: sowgro Date: Mon, 5 Jun 2023 21:53:17 -0400 Subject: add level priority, fix sfx volume, fix settings file reading --- src/main/Difficulty.java | 52 ++++++++++++++++++++++++++++++++++--- src/main/Level.java | 55 +++++++++++++++++++++++++--------------- src/main/ScoreController.java | 6 ++--- src/main/SettingsController.java | 50 +++++++++++++++++------------------- src/main/SoundController.java | 28 ++++++++------------ 5 files changed, 119 insertions(+), 72 deletions(-) (limited to 'src/main') diff --git a/src/main/Difficulty.java b/src/main/Difficulty.java index 7174c33..3dba47b 100644 --- a/src/main/Difficulty.java +++ b/src/main/Difficulty.java @@ -11,7 +11,7 @@ import org.json.simple.parser.JSONParser; import javafx.collections.FXCollections; import javafx.collections.ObservableList; -public class Difficulty +public class Difficulty implements Comparable { public File thisDir; public String title = "Unnamed"; @@ -21,6 +21,7 @@ public class Difficulty public int numBeats; public Level level; public boolean isValid = false; + public int priority = 0; /** * Creates a new Difficulty and gives it a file path @@ -88,9 +89,46 @@ public class Difficulty Object obj = jsonParser.parse(reader); JSONObject diffStuff = (JSONObject)(obj); //converts read object to a JSONObject - title = (String) diffStuff.get("title"); - bpm = Double.parseDouble(diffStuff.get("bpm")+""); - numBeats = Integer.parseInt(diffStuff.get("numBeats")+""); + if (diffStuff.containsKey("title")) + { + title = (String) diffStuff.get("title"); + } + else + { + System.err.println(file+" is missing properety title"); + isValid = false; + } + + if (diffStuff.containsKey("bpm")) + { + bpm = Double.parseDouble(diffStuff.get("bpm")+""); + } + else + { + System.err.println(file+" is missing properety bpm"); + isValid = false; + } + + if (diffStuff.containsKey("numBeats")) + { + numBeats = Integer.parseInt(diffStuff.get("numBeats")+""); + } + else + { + System.err.println(file+" is missing properety numBeats"); + isValid = false; + } + + if (diffStuff.containsKey("priority")) + { + priority = Integer.parseInt(diffStuff.get("priority")+""); + + } + else + { + System.err.println(file+" is missing properety priority"); + isValid = false; + } } catch (Exception e) { @@ -114,6 +152,7 @@ public class Difficulty obj.put("title", title); obj.put("bpm", bpm); obj.put("numBeats", numBeats); + obj.put("priority", priority); obj.writeJSONString(fileWriter); fileWriter.flush(); } @@ -213,4 +252,9 @@ public class Difficulty public String getTitle() { return title; } + + @Override + public int compareTo(Difficulty d) { + return priority - d.priority; + } } diff --git a/src/main/Level.java b/src/main/Level.java index eb1d92c..79f70e1 100644 --- a/src/main/Level.java +++ b/src/main/Level.java @@ -1,6 +1,7 @@ package main; import java.io.File; +import java.util.Collections; import java.util.Comparator; import javafx.collections.FXCollections; @@ -26,7 +27,7 @@ public class Level private boolean isValid; public Image preview; //optional - public String desc = "No description"; + public String desc; public Image background; //optional public Color[] colors = {Color.RED,Color.BLUE,Color.GREEN,Color.PURPLE,Color.YELLOW};//optional, have default colors public File song; @@ -71,19 +72,11 @@ public class Level { background = new Image(new File(thisDir,"background.png").toURI().toString()); } - else - { - System.out.println(thisDir+" is missing background.png, though it is not required"); - } if (new File(thisDir, "preview.png").exists()) { preview = new Image(new File(thisDir,"preview.png").toURI().toString()); } - else - { - System.out.println(thisDir+" is missing preview.png, though it is not required"); - } diffList = FXCollections.observableArrayList(); validDiffList = FXCollections.observableArrayList(); @@ -110,6 +103,8 @@ public class Level isValid1 = false; } + Collections.sort(validDiffList); + Collections.sort(diffList); isValid = isValid1; } @@ -120,18 +115,39 @@ public class Level { boolean isValid = true; JSONParser jsonParser = new JSONParser(); //parser to read the file - - try(FileReader reader = new FileReader(new File(thisDir, "metadata.json"))) + File file = new File(thisDir, "metadata.json"); + try(FileReader reader = new FileReader(file)) { Object obj = jsonParser.parse(reader); JSONObject levelStuff = new JSONObject(); levelStuff = (JSONObject)(obj); //converts read object to a JSONObject - title = (String)(levelStuff.get("title")); - artist = (String)(levelStuff.get("artist")); - desc = (String)(levelStuff.get("desc")); + if (levelStuff.containsKey("title")) + { + title = (String) levelStuff.get("title"); + } + else + { + System.err.println(file+" is missing properety title"); + isValid = false; + } - if(( levelStuff).containsKey("color1")) //check for custom colors in a hexadecimal format + if (levelStuff.containsKey("artist")) + { + artist = (String)(levelStuff.get("artist")); + } + else + { + System.err.println(file+" is missing properety aritst"); + isValid = false; + } + + if (levelStuff.containsKey("desc")) + { + desc = (String) levelStuff.get("desc"); + } + + if(( levelStuff).containsKey("color1")) //check for custom colors in a hexadecimal format (zach was lazy for not checking all five colors but i will forgive him) { colors = new Color[5]; @@ -140,10 +156,6 @@ public class Level colors[2] = Color.web((String)(levelStuff.get("color3"))); colors[3] = Color.web((String)(levelStuff.get("color4"))); colors[4] = Color.web((String)(levelStuff.get("color5"))); - } - else - { - colors = new Color[]{Color.RED,Color.BLUE,Color.GREEN,Color.PURPLE,Color.YELLOW}; } } catch (Exception e) @@ -166,7 +178,10 @@ public class Level JSONObject obj = new JSONObject(); obj.put("title", title); obj.put("artist", artist); - obj.put("desc", desc); + if (desc != null) + { + obj.put("desc", desc); + } obj.put("color1",colors[0].toString()); obj.put("color2",colors[1].toString()); obj.put("color3",colors[2].toString()); diff --git a/src/main/ScoreController.java b/src/main/ScoreController.java index 54dd960..2b76217 100644 --- a/src/main/ScoreController.java +++ b/src/main/ScoreController.java @@ -20,7 +20,7 @@ public class ScoreController{ score += 300*comboMultiplier; scoreProperty.setValue(score+""); comboProperty.setValue(combo +""); - System.out.println("Perfect!"); + // System.out.println("Perfect!"); } /** @@ -31,7 +31,7 @@ public class ScoreController{ score += 100*comboMultiplier; scoreProperty.setValue(score+""); comboProperty.setValue(combo+""); - System.out.println("Good"); + // System.out.println("Good"); } /** @@ -43,7 +43,7 @@ public class ScoreController{ comboMultiplier = 1; scoreProperty.setValue(score+""); comboProperty.setValue(combo+""); - System.out.println("Miss"); + // System.out.println("Miss"); } /* diff --git a/src/main/SettingsController.java b/src/main/SettingsController.java index 36fda40..b7b2cb9 100644 --- a/src/main/SettingsController.java +++ b/src/main/SettingsController.java @@ -1,57 +1,53 @@ package main; import java.io.FileWriter; -import java.io.FileNotFoundException; +import java.io.File; import java.io.FileReader; import java.io.IOException; - import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; - import javafx.beans.property.SimpleDoubleProperty; -import javafx.beans.property.SimpleIntegerProperty; public class SettingsController { public SimpleDoubleProperty effectsVol = new SimpleDoubleProperty(1); public SimpleDoubleProperty musicVol = new SimpleDoubleProperty(1); - private JSONObject settings; + private File file = new File("settings.json"); + + public SettingsController() + { + read(); + } - public void read() throws ParseException + public void read() { JSONParser jsonParser = new JSONParser(); //parser to read the file - - try(FileReader reader = new FileReader("settings.json")) + try(FileReader reader = new FileReader(file)) { Object obj = jsonParser.parse(reader); - + JSONObject settings = new JSONObject(); settings = (JSONObject)(obj); //converts read object to a JSONObject - effectsVol.set((double) settings.get("effectsVol")); - musicVol.set((double) settings.get("musicVol")); + effectsVol.set(Double.parseDouble(settings.get("effectsVol")+"")); + musicVol.set(Double.parseDouble(settings.get("musicVol")+"")); } - catch (FileNotFoundException e) - { - e.printStackTrace(); - } - catch (IOException e) + catch (Exception e) { e.printStackTrace(); - } - + } } - public void write(int newEffVol, int newMusVol) + public void write() { - settings.put("musicVol", newMusVol); - settings.put("effectsVol", newEffVol); - try (FileWriter file = new FileWriter("settings.json")) + FileWriter fileWriter; + try { - //write the settings JSONObject instance to the file - file.write(settings.toJSONString()); - file.flush(); - + fileWriter = new FileWriter(file); + JSONObject obj = new JSONObject(); + obj.put("musicVol", musicVol.getValue()); + obj.put("effectsVol", effectsVol.getValue()); + obj.writeJSONString(fileWriter); + fileWriter.flush(); } catch (IOException e) { e.printStackTrace(); diff --git a/src/main/SoundController.java b/src/main/SoundController.java index b3c6d23..c8e6c8a 100644 --- a/src/main/SoundController.java +++ b/src/main/SoundController.java @@ -11,15 +11,18 @@ public class SoundController { public MediaPlayer songMediaPlayer; public MediaPlayer sfxMediaPlayer; - private HashMap presets = new HashMap<>(); + private HashMap effects = new HashMap<>(); private File mainMenuSong = new File("src/assets/fairyfountain.wav"); public SoundController() { - presets.put("forward", new File("src/assets/MenuForward.wav")); - presets.put("backward", new File("src/assets/MenuBackward.wav")); - presets.put("hit", new File("src/assets/Hitsound.wav")); - presets.put("miss", new File("src/assets/Miss.wav")); + effects.put("hit", new MediaPlayer(new Media(new File("src/assets/hit.wav").toURI().toString()))); + effects.put("miss", new MediaPlayer(new Media(new File("src/assets/miss.wav").toURI().toString()))); + effects.put("forward", new MediaPlayer(new Media(new File("src/assets/forward.wav").toURI().toString()))); + effects.put("backward", new MediaPlayer(new Media(new File("src/assets/backward.wav").toURI().toString()))); + effects.forEach((key,value) -> { + value.volumeProperty().bind(Driver.settingsController.effectsVol); + }); playMenuSong(); } @@ -50,20 +53,9 @@ public class SoundController } } - public void playSfx(File sfxFile) - { - if (sfxMediaPlayer != null) - { - sfxMediaPlayer.stop(); - } - Media sound = new Media(sfxFile.toURI().toString()); - sfxMediaPlayer = new MediaPlayer(sound); - sfxMediaPlayer.volumeProperty().bind(Driver.settingsController.effectsVol); //not working yet - sfxMediaPlayer.play(); - } - public void playSfx(String preset) { - playSfx(presets.get(preset)); + effects.get(preset).stop(); + effects.get(preset).play(); } } \ No newline at end of file -- cgit v1.2.3