Game Development Reference
In-Depth Information
dump water in it. If this try is successful, set the direction of the neigh-
borCell object to the tested direction to represent the flow of water, as follows:
WaterCell neighborCell = waterField[x+dx][y+dy];
if(cell.getAmount() > 0.01){
floatadjustAmount = neighborCell.compareCells(cell);
if(adjustAmount >
0){neighborCell.setDirection(CellUtil.getDirection(dx,
dy));
}
}
17. Before you exit the method, parse through the waterField array once again.
This time add incomingWater to the current amount of the cell and then set
incomingWater to 0 .
18. To handle the display of the result, create a new method called createGeo-
metry .
19. The first thing we need to do is check whether the Spatial of the control has a
child called Water . If it does, detach it.
20. Next, define a new Node class called water . Its name should be Water as this
is an identifier in this example:
water = new Node("Water");
21. Again, parse the waterField array. If any cell's amount is more than 0, you
should add a Geometry object that represents it.
22. We're going to add some logic to the getGeometry method to avoid recreating
the Geometry field unnecessarily. First of all, set geometry to null if the
amount value is 0.
23. Otherwise, if geometry is null, create a new geometry instance with a box-
like shape as follows:
geometry = new Geometry("WaterCell", new Box(1f, 1f,
1f));
24. To adapt it to the amount of water we have, scale the resulting cube by typing the
following code:
geometry.setLocalScale(1, 1f + amount, 1);
Search WWH ::




Custom Search