Game Development Reference
In-Depth Information
10. We can leave this class for now and focus on creating a control that will use this
class. Create a new class called WaterFieldControl that extends Ab-
stractControl .
11. It needs two integer fields to control the width and height of the field as well as a
2D array of WaterCell called waterField . To display it, we'll add a Node
class called water and a Material class called material .
12. The setSpatial method should be overridden and the spatial variable
passed has to be an instance of Node . Look for a terrain among its children; once
found, populate waterField with WaterCells , applying the height of the
terrain for each tile as follows:
for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
WaterCell cell = new
WaterCell();cell.setTerrainHeight(((Terrain)s).getHeight(new
Vector2f(x, y)));
waterField[x][y] = cell;
}
}
13. Now, create a new method called updateCells . For this example, define a
source of water that will never run out right from the beginning by setting the
amount of water in one of the middle tiles as 1.
14. Then, parse through each cell in the waterField array in a nested for loop.
15. If the cell has an amount that is larger than 0, we can go on and check where we
should start moving the water. Start with the cell's direction, and if there is water
left after checking one direction, continue to look through the other seven direc-
tions. This is what the implementation might look like:
WaterCell cell = waterField[x][y];
float cellAmount = cell.getAmount();
if(cellAmount > 0){
int direction = cell.getDirection();
for(int i = 0; i < 8; i++){
int[] dir = CellUtil.getDirection((direction +
i) % 8);
16. For each of these directions, we must first check that it is a valid location within
the field. Then, retrieve the neighboring cell and call compareCells to try to
Search WWH ::




Custom Search