I am making a serializable class that contains a name and a collection of grid cells that I can build stuff on them.
An example use case is to build a farm grid for the scene, I use it to mark the areas that can be planted or used for placing animal shelters.
This is the class I have created.
/**
* Defines areas on the grid which we can build on them (i.e place objects).
*
* @author Ali-RS
*/
public class GridBuildingArea implements Serializable {
static final long serialVersionUID = 1L;
private String name;
private Grid grid;
// Keep track of cells by the cell ID
private final Map<Long, Vec3i> buildableCells = new HashMap<>();
public GridBuildingArea(String name, Grid grid) {
this.name = name;
this.grid = grid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Grid getGrid() {
return grid;
}
public void setGrid(Grid grid) {
this.grid = grid;
}
/**
* Specify if we can build on the cell.
*/
public void setBuildable(Vec3i cell, boolean buildable) {
if (buildable) {
buildableCells.put(grid.cellToId(cell), cell);
} else {
buildableCells.remove(grid.cellToId(cell));
}
}
/**
* @return true if we can build objects on the specified cell.
*/
public boolean canBuildOn(Vec3i cell) {
return buildableCells.containsKey(grid.cellToId(cell));
}
/**
* @return true if we can build objects on the specified cell.
*/
public boolean canBuildOn(int xCell, int yCell, int zCell) {
return buildableCells.containsKey(grid.cellToId(xCell, yCell, zCell));
}
/**
* @return an unmodifiable map of cells we can build objects on them.
*/
public Map<Long, Vec3i> getBuildableCells() {
return Collections.unmodifiableMap(buildableCells);
}
/**
* Clear all buildable cells.
*/
public void clear() {
buildableCells.clear();
}
}
But I am not satisfied with the class naming so I am making a poll.
If it were me, I would probably name the entire plot for the farm a BuildingGrid and would name the many cells it contains BuildingGridCell or BuildingCell and store those in a 2d array in the BuildingGrid.
I think GridArea or GridAreaManager would be best & having a static subclass called Builder to build grid data like setBuildable(book:Boolean), addCell(), removeCell(), notifyManager(), & a build(), but that may expand code a little bit.
To me, it’s just a marked grid. Or a set of cells. You are using it for “what areas are buildable?” but it’s distinguishing trait is that it efficiently keeps track of a set of grid cells.
So other than the name field part, it could even be: GridCellSet extends AbstractSet<GridCell> with additional methods for using Vec3i. Or just GridCellSet extends AbstractSet<Vec3i> if you only ever deal with grid cells as Vec3i.
…internally you can always translate back and forth between long and Vec3i to keep a Set<Long> internally.
Two things I always try to keep in mind when naming:
Does the name fully describe the function of the class?
Is the name generic enough explain all use cases of the class?
With these things in mind, I have often found where I will name a class something to short that does not fully describe its function. I have also found that I may put something too specific in the name of a class, when it has uses outside of that. Do not be afraid of long class names. Many developers I have worked with want nice short class names, but it always comes to bite them when other developers are trying to understand their code. I just recently named a class ComponentNameComponentRegistry yes it is long, but anyone working in the code base will know exactly what it is, especially since a NamedComponentRegistry also exists. One stores components based on the component name, the other stores them based on an assigned name. Two very different functions.
You will notice that @pspeed’s example of GridCellSet take this philosophy of having a name that is generic but fully explains the class.
As an newbie may I ask :
why double ? Couldn’t a float value plus a scaler do the trick?
Is there any performance impact on AMD GPUs ? (lack of support to double ? )
Under what circumstance should we choose double instead of float ?