Finishing SaveLevel without saved data
This commit is contained in:
parent
e54fab4c2f
commit
ad301ca7fb
@ -10,16 +10,6 @@ import java.util.Arrays;
|
|||||||
|
|
||||||
public class BinaryParser implements FileParser {
|
public class BinaryParser implements FileParser {
|
||||||
|
|
||||||
@Override
|
|
||||||
public Map getSavedData(File file) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void saveLevelData(File file, Map levelData) throws IOException {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* check if the file is a binary file suited for parsing
|
* check if the file is a binary file suited for parsing
|
||||||
* @return true if it is a binary File
|
* @return true if it is a binary File
|
||||||
@ -47,7 +37,7 @@ public class BinaryParser implements FileParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map getLevel(File file) throws IOException {
|
public Map getLevel(File file, boolean saved_data) throws IOException {
|
||||||
Map ret;
|
Map ret;
|
||||||
|
|
||||||
FileInputStream fileStream = new FileInputStream(file);
|
FileInputStream fileStream = new FileInputStream(file);
|
||||||
@ -58,13 +48,34 @@ public class BinaryParser implements FileParser {
|
|||||||
|
|
||||||
ret.addPiece(ExtractPiecesFromLevelData(level_data));
|
ret.addPiece(ExtractPiecesFromLevelData(level_data));
|
||||||
|
|
||||||
|
if(saved_data)
|
||||||
|
// TODO: 3/23/23 getSavedDat
|
||||||
|
|
||||||
fileStream.close();
|
fileStream.close();
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void saveLevel(File file, Map levelData) throws FileNotFoundException {
|
public void saveLevel(File file, Map level_data, boolean save_data) throws FileNotFoundException {
|
||||||
// byte[] data = new byte[];
|
int byteSize = getByteSizeForMap(level_data, false);
|
||||||
|
byte[] data = new byte[byteSize];
|
||||||
|
int i = 0;
|
||||||
|
data[i++] = 'S'; data[i++] = 'M'; data[i++] = 'S';
|
||||||
|
data[i++] = (byte) level_data.getWidth(); data[i++] = (byte) level_data.getHeight();
|
||||||
|
for(byte b : BuildByteFromMatrix(level_data.getShape())){
|
||||||
|
data[i++] = b;
|
||||||
|
}
|
||||||
|
data[i++] = (byte) level_data.getPieces().size();
|
||||||
|
for (Piece p : level_data.getPieces()) {
|
||||||
|
data[i++] = Bitwise.NibbleToByte((byte) p.getWidth(), (byte) p.getHeight());
|
||||||
|
for(byte b : BuildByteFromMatrix(p.getShape())){
|
||||||
|
data[i++] = b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (save_data){
|
||||||
|
// TODO: 3/23/23 Save Data
|
||||||
|
}
|
||||||
|
data[i++] = 'S'; data[i++] = 'M'; data[i++] = 'E';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -126,6 +137,30 @@ public class BinaryParser implements FileParser {
|
|||||||
return BuildMatrixFromBytes(map_width, map_height, map_data);
|
return BuildMatrixFromBytes(map_width, map_height, map_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* take a boolean matrix and build an array of byte following the specs of the parser
|
||||||
|
* @param shape bolean matrix where true are 1 and false are 0
|
||||||
|
* @return byte array with each element compiled for file format
|
||||||
|
*/
|
||||||
|
private static byte[] BuildByteFromMatrix(boolean[][] shape){
|
||||||
|
int width = shape[0].length , height = shape.length;
|
||||||
|
boolean[] list = new boolean[width * height];
|
||||||
|
for (int x = 0; x < shape.length; x++) {
|
||||||
|
for (int y = 0; y < shape[x].length; y++) {
|
||||||
|
list[x * shape[x].length + y] = shape[x][y];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
byte[] ret = new byte[width * height / 8 + width * height % 8 == 0 ? 0 : 1 ];
|
||||||
|
for (int i = 0; i < ret.length; i++) {
|
||||||
|
byte b = 0;
|
||||||
|
for (int b_count = i * 8; b_count < i * 8 + 8; b_count++) {
|
||||||
|
b = (byte) ((b << 1) | (list[b_count] ? 1 : 0));
|
||||||
|
}
|
||||||
|
ret[i] = b;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build a boolean Matrix From a byte array
|
* Build a boolean Matrix From a byte array
|
||||||
* Each Byte is composed of 8 bit, each bit is 1 or 0
|
* Each Byte is composed of 8 bit, each bit is 1 or 0
|
||||||
|
@ -24,17 +24,5 @@ public interface FileParser {
|
|||||||
* @param file the file where to save
|
* @param file the file where to save
|
||||||
* @param levelData the map to save
|
* @param levelData the map to save
|
||||||
*/
|
*/
|
||||||
void saveLevel(File file, Map levelData) throws FileNotFoundException;
|
void saveLevel(File file, Map levelData, boolean save_data) throws FileNotFoundException;
|
||||||
|
|
||||||
/**
|
|
||||||
* Save Map to a file with all it's data
|
|
||||||
* @param file the file where to save
|
|
||||||
* @param levelData the map to save
|
|
||||||
*/
|
|
||||||
void saveLevelData(File file, Map levelData) throws IOException;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// TODO: 3/18/23 tonitch: Add getSavedData, for when piece could be added to map file
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -27,4 +27,14 @@ public class Bitwise {
|
|||||||
ret[1] = (byte) (in & 15); // apply the mask '00001111'
|
ret[1] = (byte) (in & 15); // apply the mask '00001111'
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transform 2 byte into 1 with a left part ( 4 bits ) and a right part ( 4 bits)
|
||||||
|
* @param left first 4 bits
|
||||||
|
* @param right last 4 bits
|
||||||
|
* @return concatenated byte
|
||||||
|
*/
|
||||||
|
public static byte NibbleToByte(byte left, byte right){
|
||||||
|
return (byte) ((left << 4) | right);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user