问题
I wanna map given strings from a text file to blocks, I mean actually draw blocks. I already have a BLOCK class which works, with the following constructor:
public class Block implements Collidable, Sprite, HitNotifier {
private List<HitListener> hitListeners;
private Color color;
private Rectangle rectangle;
/**
* Instantiates a new collidable Block.
*
* @param rectangle a given rectangle
* @param color the color of the block
*/
public Block(Rectangle rectangle, Color color) {
this.color = color;
this.rectangle = rectangle;
hitListeners = new ArrayList<>();
}
Now my goal is to know how to read and parse text file and construct blocks based on the given info in this text file. The block definitions file maps characters to spacing elements or block-information.
An example of a block definitions file is given below. An explanation follows.
# default values for blocks
default height:20
# block definitions
bdef symbol:a width:20 fill:color(RGB(10,10,10)) stroke:color(black)
bdef symbol:b width:20 fill:color(RGB(10,10,10))
bdef symbol:c width:20 fill:color(red) stroke:color(blue)
bdef symbol:n width:20 fill:image(filename.png) stroke:color(black)
# spacers definitions
sdef symbol:* width:20
sdef symbol:^ width:10
We have four types of lines in the file:
comments or blanks lines are ignored.
block definitions lines begin with the token bdef, followed by a space-separated list of properties. Each property has the form key:value.
default values a single line beginning with the token default, followed by a space-separated list of properties. Each property has the form key:value. You can assume that if it exists it will be above the bdef lines.
spacer definitions lines begin with the token sdef, followed by a space-separated list of properties. Each property has the form
key:value
Each block definition has the following properties:
- symbol: the character which is used to represent the block type in the levels-information file. Must be a single character.
- height: the height of the block, in pixels. Must be positive integer.
- width: the width of the block, in pixels. Must be a positive integer.
- fill: The block should be filled using one of the following value formats: color(colorname),color(rgb(x,y,z)),image(filename.png),stroke
A spacer-definition line has two properties (it does not inherit default definitions):
- symbol: the character which is used to represent the space-type in the levels-information file. Must be a single character.
- width: the width of the spacing element in pixels.
I want to use this class to convert the information in the BLOCKS sections of the levels specification files to a list of blocks.
I also have the BlocksDefinitionReader class, that will be in charge of reading a block-definitions file and returning a BlocksFromSymbolsFactory object (i'm not sure how to write it).
My final goal is that when reading the file, the code in BlocksDefinitionReader will create the appropriate BlockCreator implementations according to the definitions in the bdef lines, and populate the BlocksFromSymbolsFactory with the BlockCreators and their associated symbols.
BlockCreator is an interface of a factory-object that is used for creating blocks:
public interface BlockCreator {
// Create a block at the specified location.
Block create(int xpos, int ypos);
}
So as you see my current problem is to write down the BlocksDefinitionReader and maybe improve the BlocksFromSymbolsFactory?
Thanks alot.
来源:https://stackoverflow.com/questions/62499353/map-characters-to-elements-or-draw-blocks-using-parsing-a-text-file