Saving and Loading file formats

main
Joshua Sigona 4 years ago
parent d0d1e497e7
commit 785d505492
  1. 34
      LLSIG/src/Canvas.java
  2. 59
      LLSIG/src/LLSIG.java
  3. 26
      LLSIG/src/Lane.java
  4. 4
      LLSIG/src/Note.java
  5. 65
      LLSIG/src/sig/utils/FileUtils.java

@ -21,22 +21,24 @@ public class Canvas extends JPanel{
final int NOTE_SIZE = 16;
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0,0,this.getWidth(),this.getHeight());
g.setColor(Color.WHITE);
g.drawString(Integer.toString(LLSIG.game!=null?LLSIG.game.frameCount:0),0,16);
g.setColor(Color.GRAY);
g.fillRect(MIDDLE_X-JUDGEMENT_LINE_WIDTH/2,MIDDLE_Y-JUDGEMENT_LINE_HEIGHT/2,JUDGEMENT_LINE_WIDTH,JUDGEMENT_LINE_HEIGHT);
g.setColor(NOTE_COLOR);
int noteCounter = 0;
Lane lane1 = LLSIG.game.lanes.get(0);
while (lane1.noteExists(noteCounter)) {
Note n = lane1.getNote(noteCounter);
int NOTE_Y_OFFSET = (int)((((double)LLSIG.game.musicPlayer.getPlayPosition()-n.getStartFrame())/1000)*60*LLSIG.game.noteSpeed);
g.fillOval(MIDDLE_X-NOTE_SIZE/2,MIDDLE_Y-NOTE_SIZE/2+NOTE_Y_OFFSET,NOTE_SIZE,NOTE_SIZE);
noteCounter++;
if (LLSIG.game!=null) {
g.setColor(Color.BLACK);
g.fillRect(0,0,this.getWidth(),this.getHeight());
g.setColor(Color.WHITE);
g.drawString(Integer.toString(LLSIG.game.frameCount),0,16);
g.setColor(Color.GRAY);
g.fillRect(MIDDLE_X-JUDGEMENT_LINE_WIDTH/2,MIDDLE_Y-JUDGEMENT_LINE_HEIGHT/2,JUDGEMENT_LINE_WIDTH,JUDGEMENT_LINE_HEIGHT);
g.setColor(NOTE_COLOR);
int noteCounter = 0;
Lane lane1 = LLSIG.game.lanes.get(0);
while (lane1.noteExists(noteCounter)) {
Note n = lane1.getNote(noteCounter);
int NOTE_Y_OFFSET = (int)((((double)LLSIG.game.musicPlayer.getPlayPosition()-n.getStartFrame())/1000)*60*LLSIG.game.noteSpeed);
g.fillOval(MIDDLE_X-NOTE_SIZE/2,MIDDLE_Y-NOTE_SIZE/2+NOTE_Y_OFFSET,NOTE_SIZE,NOTE_SIZE);
noteCounter++;
}
}
}
}

@ -9,10 +9,12 @@ import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import javax.swing.JFrame;
import javazoom.jl.decoder.JavaLayerException;
import sig.utils.FileUtils;
public class LLSIG implements KeyListener{
Player musicPlayer;
@ -24,17 +26,16 @@ public class LLSIG implements KeyListener{
int noteSpeed = 4;
List<Lane> lanes = new ArrayList<Lane>();
public boolean EDITMODE = true;
LLSIG(JFrame f) {
this.window = f;
this.musicPlayer = new Player("music/MiChi - ONE-315959669.mp3");
musicPlayer.play();
lanes.add(new Lane(Arrays.asList(new Note[] {
new Note(NoteType.NORMAL,1000),
new Note(NoteType.NORMAL,2000),
new Note(NoteType.NORMAL,3000),
new Note(NoteType.NORMAL,4000),
})));
lanes.add(new Lane(new ArrayList<Note>()));
//LoadSongData("MiChi - ONE-315959669",lanes);
Canvas canvas = new Canvas(f.getSize());
window.add(canvas);
@ -46,9 +47,52 @@ public class LLSIG implements KeyListener{
window.repaint();
}
};
//SaveSongData("MiChi - ONE-315959669",lanes);
stpe.scheduleAtFixedRate(gameLoop, 0, 16666666l, TimeUnit.NANOSECONDS);
}
private void LoadSongData(String song,List<Lane> lanes) {
try {
String[] data = FileUtils.readFromFile("music/"+song+".sig");
for (String line : data) {
String[] split = line.split(Pattern.quote(","));
int lane = Integer.parseInt(split[0]);
NoteType noteType = NoteType.valueOf(split[1]);
int offset = Integer.parseInt(split[2]);
int offset2 = -1;
while (lanes.size()<lane) {
lanes.add(new Lane(new ArrayList<Note>()));
}
if (noteType==NoteType.HOLD) {
offset2 = Integer.parseInt(split[2]);
lanes.get(lane-1).addNote(new Note(noteType,offset,offset2));
} else {
lanes.get(lane-1).addNote(new Note(noteType,offset));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void SaveSongData(String song,List<Lane> lanes) {
List<String> data = new ArrayList<String>();
for (int lane=0;lane<lanes.size();lane++) {
Lane l = lanes.get(lane);
int noteCount=0;
while (l.noteExists(noteCount)) {
Note n = l.getNote(noteCount++);
data.add(new StringBuilder().append(lane+1).append(",")
.append(n.getNoteType().name()).append(",")
.append(n.getStartFrame()).append(",")
.append(n.getEndFrame())
.toString());
}
}
FileUtils.writeToFile(data.toArray(new String[data.size()]),"music/"+song+".sig");
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setSize(640, 640);
@ -63,7 +107,8 @@ public class LLSIG implements KeyListener{
@Override
public void keyPressed(KeyEvent e) {
System.out.println("Pressed "+e.getKeyChar()+" on frame "+musicPlayer.getPlayPosition());
LLSIG.game.lanes.get(0).addNote(new Note(NoteType.NORMAL,musicPlayer.getPlayPosition()));
//System.out.println("Pressed "+e.getKeyChar()+" on frame "+musicPlayer.getPlayPosition());
}
@Override

@ -11,8 +11,8 @@ public class Lane{
public boolean endOfChart() {
return currentNoteIndex==noteChart.size()-1;
}
public void consumeNote() {
currentNoteIndex = Math.min(currentNoteIndex+1,noteChart.size()-1);
public boolean noteExists() {
return noteExists(0);
}
public boolean noteExists(int noteOffset) {
return currentNoteIndex+noteOffset<noteChart.size();
@ -23,4 +23,26 @@ public class Lane{
public Note getNote() {
return getNote(0);
}
public void addNote(Note n) {
addNote(n,false);
}
public void addNote(Note n,boolean performReorderingOfList) {
if (performReorderingOfList) {
boolean added=false;
for (int i=0;i<noteChart.size();i++) {
Note nn = noteChart.get(i);
if (nn.start>n.start) {
noteChart.add(i,n);
added=true;
break;
}
}
if (!added) {
noteChart.add(n);
}
} else {
noteChart.add(n);
}
System.out.println("Note added: "+n);
}
}

@ -28,4 +28,8 @@ public class Note {
public void setEndFrame(int end) {
this.end = end;
}
@Override
public String toString() {
return "Note [type=" + type + ", start=" + start + ", end=" + end + "]";
}
}

@ -0,0 +1,65 @@
package sig.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
public class FileUtils {
public static String[] readFromFile(String filename) throws IOException {
File file = new File(filename);
//System.out.println(file.getAbsolutePath());
List<String> contents= new ArrayList<String>();
if (file.exists()) {
FileInputStream in = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(in,Charset.forName("UTF-8"));
BufferedReader br = new BufferedReader(isr);
String readline = br.readLine();
do {
if (readline!=null) {
//System.out.println(readline);
contents.add(readline);
readline = br.readLine();
}} while (readline!=null);
br.close();
isr.close();
in.close();
}
return contents.toArray(new String[contents.size()]);
}
public static void writeToFile(String[] data, String filename) {
writeToFile(data,filename,false);
}
public static void writeToFile(String[] data, String filename, boolean append) {
File file = new File(filename);
try {
if (!file.exists()) {
file.createNewFile();
}
OutputStream out = new FileOutputStream(file,append);
Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
PrintWriter pw = new PrintWriter(writer);
//pw.print('\uFEFF');
for (String s : data) {
pw.println(s);
}
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Loading…
Cancel
Save