Game Development Reference
In-Depth Information
4.
Finds a random offset position based on the difference found in step 3
multiplied by a random number that ranges from 0 to 1
5.
Calculates the final x position based on the minimum x value added with the
randomly generated x offset value
6.
Repeats steps 1 through 5 for the z axis, in order to find a random z
coordinate
7.
Returns a random location vector consisting of the random x and z values
calculated in the previous steps and the DROP_HEIGHT value as the y value
Listing 10-23. Generating a Random Grid Location
Vector3 GenerateRandomGridLocation()
{
Vector3 Location = new Vector3(0,0,0);
// Get Random X
float MinX = m_Grid.GetXMinBoundary();
float MaxX = m_Grid.GetXMaxBoundary();
float DiffX = MaxX - MinX;
float RandomXOffset = DiffX * m_RandNumber.nextFloat(); // DiffX * (Number from 0-1);
float PosX = MinX + RandomXOffset;
// Get Random Z
float MinZ = m_Grid.GetZMinBoundary();
float MaxZ = m_Grid.GetZMaxBoundary();
float DiffZ = MaxZ - MinZ;
float RandomZOffset = DiffZ * m_RandNumber.nextFloat(); // DiffX * (Number from 0-1);
float PosZ = MinZ + RandomZOffset;
// Y is 0 for Ground Level for Playfield
float PosY = DROP_HEIGHT;
// Set Random Location
Location.Set(PosX, PosY, PosZ);
return Location;
}
The GenerateGridLocationRestricted() function generates a random grid location for dropping an
enemy within the boundaries of Min and Max. (See Listing 10-24.)
The function does the following:
The GenerateRandomGridLocation() function is called to create a random
location within the grid.
1.
2.
Then, the location is limited or clamped to the maximum location value by
taking the lesser value between Max and the randomly generated location
from step 1.
 
Search WWH ::




Custom Search