Saving and Loading file formats
This commit is contained in:
parent
d0d1e497e7
commit
785d505492
@ -21,10 +21,11 @@ public class Canvas extends JPanel{
|
|||||||
final int NOTE_SIZE = 16;
|
final int NOTE_SIZE = 16;
|
||||||
|
|
||||||
super.paintComponent(g);
|
super.paintComponent(g);
|
||||||
|
if (LLSIG.game!=null) {
|
||||||
g.setColor(Color.BLACK);
|
g.setColor(Color.BLACK);
|
||||||
g.fillRect(0,0,this.getWidth(),this.getHeight());
|
g.fillRect(0,0,this.getWidth(),this.getHeight());
|
||||||
g.setColor(Color.WHITE);
|
g.setColor(Color.WHITE);
|
||||||
g.drawString(Integer.toString(LLSIG.game!=null?LLSIG.game.frameCount:0),0,16);
|
g.drawString(Integer.toString(LLSIG.game.frameCount),0,16);
|
||||||
|
|
||||||
g.setColor(Color.GRAY);
|
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.fillRect(MIDDLE_X-JUDGEMENT_LINE_WIDTH/2,MIDDLE_Y-JUDGEMENT_LINE_HEIGHT/2,JUDGEMENT_LINE_WIDTH,JUDGEMENT_LINE_HEIGHT);
|
||||||
@ -40,3 +41,4 @@ public class Canvas extends JPanel{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -9,10 +9,12 @@ import java.util.Arrays;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import javax.swing.JFrame;
|
import javax.swing.JFrame;
|
||||||
|
|
||||||
import javazoom.jl.decoder.JavaLayerException;
|
import javazoom.jl.decoder.JavaLayerException;
|
||||||
|
import sig.utils.FileUtils;
|
||||||
|
|
||||||
public class LLSIG implements KeyListener{
|
public class LLSIG implements KeyListener{
|
||||||
Player musicPlayer;
|
Player musicPlayer;
|
||||||
@ -24,17 +26,16 @@ public class LLSIG implements KeyListener{
|
|||||||
int noteSpeed = 4;
|
int noteSpeed = 4;
|
||||||
List<Lane> lanes = new ArrayList<Lane>();
|
List<Lane> lanes = new ArrayList<Lane>();
|
||||||
|
|
||||||
|
public boolean EDITMODE = true;
|
||||||
|
|
||||||
LLSIG(JFrame f) {
|
LLSIG(JFrame f) {
|
||||||
this.window = f;
|
this.window = f;
|
||||||
this.musicPlayer = new Player("music/MiChi - ONE-315959669.mp3");
|
this.musicPlayer = new Player("music/MiChi - ONE-315959669.mp3");
|
||||||
musicPlayer.play();
|
musicPlayer.play();
|
||||||
|
|
||||||
lanes.add(new Lane(Arrays.asList(new Note[] {
|
lanes.add(new Lane(new ArrayList<Note>()));
|
||||||
new Note(NoteType.NORMAL,1000),
|
|
||||||
new Note(NoteType.NORMAL,2000),
|
//LoadSongData("MiChi - ONE-315959669",lanes);
|
||||||
new Note(NoteType.NORMAL,3000),
|
|
||||||
new Note(NoteType.NORMAL,4000),
|
|
||||||
})));
|
|
||||||
|
|
||||||
Canvas canvas = new Canvas(f.getSize());
|
Canvas canvas = new Canvas(f.getSize());
|
||||||
window.add(canvas);
|
window.add(canvas);
|
||||||
@ -46,9 +47,52 @@ public class LLSIG implements KeyListener{
|
|||||||
window.repaint();
|
window.repaint();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//SaveSongData("MiChi - ONE-315959669",lanes);
|
||||||
stpe.scheduleAtFixedRate(gameLoop, 0, 16666666l, TimeUnit.NANOSECONDS);
|
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) {
|
public static void main(String[] args) {
|
||||||
JFrame f = new JFrame();
|
JFrame f = new JFrame();
|
||||||
f.setSize(640, 640);
|
f.setSize(640, 640);
|
||||||
@ -63,7 +107,8 @@ public class LLSIG implements KeyListener{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void keyPressed(KeyEvent e) {
|
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
|
@Override
|
||||||
|
@ -11,8 +11,8 @@ public class Lane{
|
|||||||
public boolean endOfChart() {
|
public boolean endOfChart() {
|
||||||
return currentNoteIndex==noteChart.size()-1;
|
return currentNoteIndex==noteChart.size()-1;
|
||||||
}
|
}
|
||||||
public void consumeNote() {
|
public boolean noteExists() {
|
||||||
currentNoteIndex = Math.min(currentNoteIndex+1,noteChart.size()-1);
|
return noteExists(0);
|
||||||
}
|
}
|
||||||
public boolean noteExists(int noteOffset) {
|
public boolean noteExists(int noteOffset) {
|
||||||
return currentNoteIndex+noteOffset<noteChart.size();
|
return currentNoteIndex+noteOffset<noteChart.size();
|
||||||
@ -23,4 +23,26 @@ public class Lane{
|
|||||||
public Note getNote() {
|
public Note getNote() {
|
||||||
return getNote(0);
|
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) {
|
public void setEndFrame(int end) {
|
||||||
this.end = end;
|
this.end = end;
|
||||||
}
|
}
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Note [type=" + type + ", start=" + start + ", end=" + end + "]";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
65
LLSIG/src/sig/utils/FileUtils.java
Normal file
65
LLSIG/src/sig/utils/FileUtils.java
Normal file
@ -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…
x
Reference in New Issue
Block a user