WIP: adding save data
This commit is contained in:
parent
1771fd1d42
commit
e54fab4c2f
@ -31,6 +31,9 @@ level data at the top of the file.
|
||||
- 4 first bits : width
|
||||
- 4 last bits: height
|
||||
- next bytes -> Width * Height (+1 if Width * Height % 8 is not 0)
|
||||
- Position ( only for saved file )
|
||||
- 'F' if the piece is in float state ( not placed)
|
||||
- x and y on 2 bytes for position if the piece is placed
|
||||
|
||||
## Known Limitation
|
||||
|
||||
|
@ -54,4 +54,8 @@ public class Map extends Shape{
|
||||
}
|
||||
return used;
|
||||
}
|
||||
|
||||
public ArrayList<Piece> getPieces() {
|
||||
return pieces;
|
||||
}
|
||||
}
|
||||
|
@ -10,9 +10,14 @@ import java.util.Arrays;
|
||||
|
||||
public class BinaryParser implements FileParser {
|
||||
|
||||
@Override
|
||||
public Map getSavedData(File file) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveLevelData(File file, Map levelData) throws IOException {
|
||||
// TODO: 3/20/23
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -20,7 +25,6 @@ public class BinaryParser implements FileParser {
|
||||
* @return true if it is a binary File
|
||||
*/
|
||||
public static boolean isBinaryFile(File file) throws IOException {
|
||||
// TODO: 3/20/23 Need more testing
|
||||
if (Files.probeContentType(file.toPath()) != null)
|
||||
return false ;
|
||||
|
||||
@ -42,6 +46,7 @@ public class BinaryParser implements FileParser {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map getLevel(File file) throws IOException {
|
||||
Map ret;
|
||||
|
||||
@ -57,6 +62,11 @@ public class BinaryParser implements FileParser {
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveLevel(File file, Map levelData) throws FileNotFoundException {
|
||||
// byte[] data = new byte[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract Level data from file content
|
||||
* @param fileStream file stream to read extract data from
|
||||
@ -147,4 +157,28 @@ public class BinaryParser implements FileParser {
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* give the amount of byte needed to store the given Map
|
||||
* following the binary file format
|
||||
* @param level the map to check
|
||||
* @param data should add save data or only level data
|
||||
* @return integer of the ammount of byte needed
|
||||
*/
|
||||
public static int getByteSizeForMap(Map level, boolean data){
|
||||
int ret = 6; // header + footer
|
||||
ret += 2; //size of the piece
|
||||
ret += ((level.getWidth() * level.getHeight()) / 8); // size of the map
|
||||
ret += level.getHeight() * level.getWidth() % 8 == 0 ? 0 : 1; // Add 1 if the size of map is not a mult of 8
|
||||
ret += 1; // amount of pieces
|
||||
for(Piece p: level.getPieces()){
|
||||
ret += 1; // size of the piece
|
||||
ret += p.getHeight() * p.getWidth() / 8;
|
||||
ret += p.getHeight() * p.getWidth() % 8 == 0 ? 0 : 1; // add 1 if the size of the piece is not mult of 8
|
||||
if(data){
|
||||
ret += p.getPosition() != null ? 2: 1; // if the piece is not placed, only one byte else 2
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
@ -10,13 +10,21 @@ public interface FileParser {
|
||||
|
||||
/**
|
||||
* Parse the file and create a Map with its shape and pieces setup
|
||||
* Ignore the rest of the file! (ie: save data)
|
||||
*
|
||||
* @param file file to parse
|
||||
* @param saved_data does the saved data should be added to the map
|
||||
* @return Map Object parsed with file data
|
||||
* @see "TODO: Add Specification when done"
|
||||
*/
|
||||
Map getLevel(File file) throws IOException;
|
||||
Map getLevel(File file, boolean saved_data) throws IOException;
|
||||
|
||||
/**
|
||||
* Save Map to a file without all it's data
|
||||
* Could be used for generating level file. might not be used in game.
|
||||
* @param file the file where to save
|
||||
* @param levelData the map to save
|
||||
*/
|
||||
void saveLevel(File file, Map levelData) throws FileNotFoundException;
|
||||
|
||||
/**
|
||||
* Save Map to a file with all it's data
|
||||
@ -25,6 +33,8 @@ public interface FileParser {
|
||||
*/
|
||||
void saveLevelData(File file, Map levelData) throws IOException;
|
||||
|
||||
|
||||
|
||||
// TODO: 3/18/23 tonitch: Add getSavedData, for when piece could be added to map file
|
||||
|
||||
}
|
||||
|
@ -7,6 +7,16 @@ import java.nio.file.Files;
|
||||
|
||||
public class JsonParser implements FileParser{
|
||||
|
||||
@Override
|
||||
public Map getSavedData(File file) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveLevel(File file, Map levelData) throws FileNotFoundException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveLevelData(File file, Map levelData) throws IOException {
|
||||
FileOutputStream fileStream = new FileOutputStream(file);
|
||||
|
@ -0,0 +1,43 @@
|
||||
package school_project.Parsers;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import school_project.Map;
|
||||
import school_project.Piece;
|
||||
import school_project.Vec2;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class BinaryParserTest {
|
||||
|
||||
@Test
|
||||
void getByteSizeForMap() {
|
||||
boolean[][] map_shape = {
|
||||
{ true, true, true },
|
||||
{ true, false, true },
|
||||
{ true, true, true },
|
||||
|
||||
};
|
||||
boolean[][] piece1_shape = {
|
||||
{ true, true },
|
||||
{ true, false },
|
||||
{ true, true },
|
||||
|
||||
};
|
||||
boolean[][] piece2_shape = {
|
||||
{ true },
|
||||
{ true },
|
||||
{ true },
|
||||
|
||||
};
|
||||
Map map = new Map(map_shape);
|
||||
Piece piece1 = new Piece(piece1_shape);
|
||||
Piece piece2 = new Piece(piece2_shape);
|
||||
|
||||
map.addPiece(new Piece[]{piece1, piece2});
|
||||
piece2.setPosition(new Vec2(0, 2));
|
||||
|
||||
|
||||
assertEquals(15, BinaryParser.getByteSizeForMap(map, false));
|
||||
assertEquals(18, BinaryParser.getByteSizeForMap(map, true));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user