aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/net/sowgro/npehero/levelapi/Notes.java
blob: 1df0248389df1cad2db022ea8ecd836041dbf2fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package net.sowgro.npehero.levelapi;

import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.collections.FXCollections;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * Stores all the notes for a difficulty.
 */
public class Notes {

    private final File file;
    private final Difficulty diff;

    public ListProperty<Note> list = new SimpleListProperty<>(FXCollections.observableArrayList());

    /**
     * Create a new Notes object
     * @param file The notes.txt file
     * @param diff The difficulty these notes belong to
     * @throws IOException If there is a problem reading the notes file
     */
    public Notes(File file, Difficulty diff) throws IOException {
        this.file = file;
        this.diff = diff;
        readFile();
    }

    /**
     * Read notes.txt and add the notes to the list
     * @throws IOException if there is a problem reading the file.
     */
    public void readFile() throws IOException {
        if (!file.exists()) {
            return;
        }
        Scanner scan = new Scanner(file);
        while (scan.hasNext()) {
            String input = scan.next();
            int lane = switch (input.charAt(0)) {
                case 'd' -> 0;
                case 'f' -> 1;
                case 's' -> 2;
                case 'j' -> 3;
                case 'k' -> 4;
                default -> -1;
            };
            if (lane == -1) {
                continue;
            }
            double time = Double.parseDouble(input.substring(1));

            if (diff.bpm != 0.0) {
                time = beatToSecond(time, diff.bpm);
            }
            list.add(new Note(time, lane));
        }
    }

    /**
     * Writes the notes to notes.txt
     * @throws IOException If there is a problem writing to the file.
     */
    public void writeFile() throws IOException{
        var _ = file.createNewFile();
        PrintWriter writer = new PrintWriter(file, StandardCharsets.UTF_8);
        for (Note note : list) {
            Character lane = switch (note.lane) {
                case 0 -> 'd';
                case 1 -> 'f';
                case 2 -> 's';
                case 3 -> 'j';
                case 4 -> 'k';
                default -> null;
            };
            if (lane == null) {
                continue;
            }
            writer.println(lane + "" + note.time.get());
        }
        writer.close();
    }

    /**
     * Converts a beat to a second using the levels bpm
     * @param beat The beat to convert to seconds
     * @param bpm The beats per minute to use for conversion
     * @return The time in seconds the beat was at
     */
    public static double beatToSecond(double beat, double bpm) {
        return beat/(bpm/60);
    }

    /**
     * Performs a deep copy of the notes list.
     * @return a new list of notes with the same notes.
     */
    public ListProperty<Note> deepCopyList() {
        ListProperty<Note> ret = new SimpleListProperty<>(FXCollections.observableArrayList());
        list.forEach(e -> ret.add(new Note(e)));
        return ret;
    }
}