File Parser for levels #18
@ -14,7 +14,12 @@ public class Map extends Shape{
|
||||
}
|
||||
|
||||
// TODO: 2/27/23 Tests for Map
|
||||
public void AddShape(Piece piece){
|
||||
public void addPiece(Piece piece){
|
||||
pieces.add(piece);
|
||||
}
|
||||
|
||||
public void addPiece(Piece[] pieces) {
|
||||
for (Piece p : pieces)
|
||||
this.addPiece(p);
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,15 @@ import java.util.Arrays;
|
||||
|
||||
public class BinaryParser implements FileParser {
|
||||
|
||||
/**
|
||||
* Check if the file is a binary file suited for parsing
|
||||
* @return true if it is a binary File
|
||||
*/
|
||||
static boolean isBinaryFile(File file){
|
||||
// TODO: 3/18/23
|
||||
return false;
|
||||
}
|
||||
|
||||
public Map getLevel(File file) throws IOException {
|
||||
Map ret;
|
||||
|
||||
@ -18,24 +27,29 @@ public class BinaryParser implements FileParser {
|
||||
|
||||
ret = new Map(ExtractMapFromLevelData(level_data));
|
||||
|
||||
// TODO: 3/18/23 Cursor: have to make this into a method
|
||||
// Get the amount of pieces
|
||||
// For each of these pieces, get the size of the piece on 1 octet, 4 byte are for width then 4 byte are for height
|
||||
// the same method as the map is used to get the piece shape.
|
||||
byte piece_count = level_data[3 + map_width * map_height / 8 + (map_height * map_width % 8 != 0 ? 1 : 0)];
|
||||
byte[] pieces_data = Arrays.copyOfRange(level_data, 4 + map_width * map_height / 8 + (map_height * map_width % 8 != 0 ? 1 : 0), level_data.length-1);
|
||||
ret.addPiece(ExtractPiecesFromLevelData(level_data));
|
||||
|
||||
fileStream.close();
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Pieces out of the level data
|
||||
* @param levelData full data of the level without header and footer
|
||||
* @return array of Piece from level data
|
||||
*/
|
||||
private static Piece[] ExtractPiecesFromLevelData(byte[] levelData) {
|
||||
byte map_width = levelData[0], map_height = levelData[1];
|
||||
byte piece_count = levelData[3 + map_width * map_height / 8 + (map_height * map_width % 8 != 0 ? 1 : 0)];
|
||||
Piece[] ret = new Piece[piece_count];
|
||||
byte[] pieces_data = Arrays.copyOfRange(levelData, 4 + map_width * map_height / 8 + (map_height * map_width % 8 != 0 ? 1 : 0), levelData.length-1);
|
||||
for (int piece_index = 0; piece_index < piece_count; piece_index++) {
|
||||
byte[] _piece_size = Bitwise.ByteToNible(pieces_data[piece_index]);
|
||||
byte _piece_width = _piece_size[0], _piece_height = _piece_size[1];
|
||||
byte[] _piece_data = Arrays.copyOfRange(pieces_data, piece_index + 1, 1 + _piece_width * _piece_height / 8 + (_piece_height * _piece_width % 8 != 0 ? 1 : 0));
|
||||
boolean[][] _piece_matrix = BuildMatrixFromBytes(_piece_width, _piece_height, _piece_data);
|
||||
Piece _piece = new Piece(_piece_matrix);
|
||||
ret.AddShape(_piece);
|
||||
piece_index = piece_index + _piece_width * _piece_height / 8 + (_piece_height * _piece_width % 8 != 0 ? 1 : 0);
|
||||
ret[piece_index] = new Piece(_piece_matrix);
|
||||
}
|
||||
|
||||
// Close the file and return the generated map
|
||||
fileStream.close();
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -52,7 +66,7 @@ public class BinaryParser implements FileParser {
|
||||
|
||||
/**
|
||||
* Extract Level data from file content
|
||||
* @param fileStream
|
||||
* @param fileStream file stream to read extract data from
|
||||
* @return Level data as an array of byte
|
||||
* @throws IOException Expected if we can't read the file
|
||||
*/
|
||||
|
@ -15,29 +15,12 @@ import java.io.NotSerializableException;
|
||||
*/
|
||||
public class FileParserFactory {
|
||||
public static FileParser createParser(File file) throws NotSerializableException{
|
||||
if(isBinaryFile(file))
|
||||
if(BinaryParser.isBinaryFile(file))
|
||||
return new BinaryParser();
|
||||
else if (isTextFile(file))
|
||||
else if (TxtParser.isTextFile(file))
|
||||
return new TxtParser();
|
||||
else
|
||||
throw new NotSerializableException("This file format is not supported");
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the file is a binary file suited for parsing
|
||||
* @return true if it is a binary File
|
||||
*/
|
||||
private static boolean isBinaryFile(File file){
|
||||
// TODO: 3/18/23
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the file is a text file
|
||||
* @return true if it is a text File
|
||||
*/
|
||||
private static boolean isTextFile(File file){
|
||||
return false;
|
||||
// TODO: 3/18/23
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,15 @@ import school_project.Map;
|
||||
import java.io.File;
|
||||
|
||||
public class TxtParser implements FileParser{
|
||||
/**
|
||||
* Check if the file is a text file
|
||||
* @return true if it is a text File
|
||||
*/
|
||||
static boolean isTextFile(File file){
|
||||
return false;
|
||||
// TODO: 3/18/23
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map getLevel(File file) {
|
||||
return null;
|
||||
|
Loading…
Reference in New Issue
Block a user