Game Development Reference
In-Depth Information
When that happens, we generate another random position for that glitter so that it
appears elsewhere. We do not want to start increasing the scale of each separate
glitter at the same time, but we want the glitters to show up randomly. So, inside the
Update method, we iterate through all the glitter positions and scales in the list and
depending on the value of a random variable, we start increasing their scale:
for ( int i = 0; i < scales.Count; i++)
{
if (scales[i] == 0 && JewelJam.Random.NextDouble() < 0.001)
scales[i] += 0.05f;
}
We only start increasing the scale if it is zero and a random number value is smaller
than 0 . 001. This makes sure that not all scales are immediately increases. Once a
scale is not zero anymore, we simply increase it:
else if (scales[i] != 0)
{
scales[i] += 0.05f;
// more code here
}
However, we cannot infinitely increase the scale, we want to start decreasing it
again. But how do we know if we should increase or decrease the scale? We do
not know in the Update method if we are in the increasing part of the slope or in
the decreasing part. We can use a small trick here. We let the scale run from 0 to 2
instead of 0 to 1, and in the Draw method we will calculate the real scale from that
value (0 to 1 means increasing, and 1 to 2 means decreasing scale). In the Update
method we add an if -instruction to deal with the situation when the scale is larger
than its maximum value (2):
if (scales[i] >= 2.0f)
{
scales[i] = 0f;
positions[i] = this .CreateRandomPosition();
}
When that happens, we reset the scale to zero, and we generate a new random posi-
tion for a new glitter.
18.4.5 Drawing the Glitter Field
Inside the Draw method of the glitter field, we have to draw all the glitters on the
screen at the desired scale. We want to draw these glitters with their origin at the
center, because otherwise the scaling animation would not give the desired result.
So, we calculate this center once in the beginning of the method call:
Search WWH ::




Custom Search