Game Development Reference
In-Depth Information
How it works...
The following steps will produce flowing water:
1. First of all, let us create a class that contains the cell logic. We can call it Water-
Cell . It needs a float field called amount , another float field called ter-
rainHeight , and one integer field for the current direction of the flow. It should
also store any incoming water in a float field called incomingAmount .
2. In addition to the normal getter and setter for amount , add a method called ad-
justAmount that takes a float variable called delta as the input. The delta
variable should be added to amount .
3. Create a method called compareCells that will move the water between cells. It
takes another cell (where the water is coming from) as the input.
4. The first thing the method does is checks the difference in height between the two
cells as follows:
float difference = (otherCell.getTerrainHeight() +
otherCell.getAmount()) - (terrainHeight + amount);
5. The method will only move the water in one way: from the supplied cell to this cell
so it will only act if the difference is positive (and higher than an arbitrary small
amount).
6. If so, it takes half of the difference since this would even out the amount between
the two cells. Before applying it, make sure we don't move more water than there
already is in the originating cell:
amountToChange = difference * 0.5f;
amountToChange = Math.min(amountToChange,
otherCell.getAmount());
7. Add the calculated result to the incomingAmount field (we don't update the
amount for this until everything has been calculated).
8. However, we must deduct the same amount from the originating cell or there
would be a never-ending supply of water. It's done like this:
otherCell.adjustAmount(-amountToChange);
9. Finally, return the deducted amount from this method.
Search WWH ::




Custom Search