placing static function in their respective files & changing addShape to addPiece
Some checks failed
continuous-integration/drone/pr Build is failing
continuous-integration/drone/push Build is failing

Signed-off-by: Anthony Debucquoy <debucquoy.anthony@gmail.com>
This commit is contained in:
Debucquoy Anthony 2023-03-19 23:28:31 +01:00
parent b9a6badadf
commit dfb297525b
Signed by: tonitch
GPG Key ID: A78D6421F083D42E
4 changed files with 44 additions and 33 deletions

View File

@ -14,7 +14,12 @@ public class Map extends Shape{
} }
// TODO: 2/27/23 Tests for Map // TODO: 2/27/23 Tests for Map
public void AddShape(Piece piece){ public void addPiece(Piece piece){
pieces.add(piece); pieces.add(piece);
} }
public void addPiece(Piece[] pieces) {
for (Piece p : pieces)
this.addPiece(p);
}
} }

View File

@ -9,6 +9,15 @@ import java.util.Arrays;
public class BinaryParser implements FileParser { 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 { public Map getLevel(File file) throws IOException {
Map ret; Map ret;
@ -18,24 +27,29 @@ public class BinaryParser implements FileParser {
ret = new Map(ExtractMapFromLevelData(level_data)); ret = new Map(ExtractMapFromLevelData(level_data));
// TODO: 3/18/23 Cursor: have to make this into a method ret.addPiece(ExtractPiecesFromLevelData(level_data));
// 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 fileStream.close();
// the same method as the map is used to get the piece shape. return ret;
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);
/**
* 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++) { for (int piece_index = 0; piece_index < piece_count; piece_index++) {
byte[] _piece_size = Bitwise.ByteToNible(pieces_data[piece_index]); byte[] _piece_size = Bitwise.ByteToNible(pieces_data[piece_index]);
byte _piece_width = _piece_size[0], _piece_height = _piece_size[1]; 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)); 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); boolean[][] _piece_matrix = BuildMatrixFromBytes(_piece_width, _piece_height, _piece_data);
Piece _piece = new Piece(_piece_matrix); ret[piece_index] = new Piece(_piece_matrix);
ret.AddShape(_piece);
piece_index = piece_index + _piece_width * _piece_height / 8 + (_piece_height * _piece_width % 8 != 0 ? 1 : 0);
} }
// Close the file and return the generated map
fileStream.close();
return ret; return ret;
} }
@ -52,7 +66,7 @@ public class BinaryParser implements FileParser {
/** /**
* Extract Level data from file content * 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 * @return Level data as an array of byte
* @throws IOException Expected if we can't read the file * @throws IOException Expected if we can't read the file
*/ */

View File

@ -15,29 +15,12 @@ import java.io.NotSerializableException;
*/ */
public class FileParserFactory { public class FileParserFactory {
public static FileParser createParser(File file) throws NotSerializableException{ public static FileParser createParser(File file) throws NotSerializableException{
if(isBinaryFile(file)) if(BinaryParser.isBinaryFile(file))
return new BinaryParser(); return new BinaryParser();
else if (isTextFile(file)) else if (TxtParser.isTextFile(file))
return new TxtParser(); return new TxtParser();
else else
throw new NotSerializableException("This file format is not supported"); 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
}
} }

View File

@ -5,6 +5,15 @@ import school_project.Map;
import java.io.File; import java.io.File;
public class TxtParser implements FileParser{ 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 @Override
public Map getLevel(File file) { public Map getLevel(File file) {
return null; return null;