Game Development Reference
In-Depth Information
How to do it...
We start by creating the object that contains the cell data. This will have the following sev-
en steps:
1. First, create a new class called
CubeCell
.
2. It contains a
Mesh
field call
mesh
, an array of six Booleans called
neighbors
,
and another Boolean called
refresh
.
3. In addition, there is enum called
Type
where we can put names such as
Rock
,
Sand
, and
Grass
. Then, add a
Type
field called
type
.
4. Create a method called
hasNeighbor
that takes an integer parameter as an input
and return the corresponding Boolean from the array.
5. Then, add a method called
setNeighbor
that takes both an integer parameter
called
direction
and a Boolean parameter called
neighbor
as the input. If
the current Boolean at the position of the direction is not the same as that of the
neighbor, store the neighbor at that location and set
refresh
to
true
.
6. Add a method called
requestRefresh
that sets
refresh
to
true
.
7. For a mesh, add a
getMesh
method, and inside this, call a method called
CubeUtil.createMesh
if the mesh is null or refresh it if it is
true
. This will
also set
refresh
to
false
as follows:
if(mesh == null || refresh){
mesh = CubeUtil.createMesh(this);
refresh = false;
}
return mesh;
Now, let's return to the
CubeUtil
class where we add some helper methods to generate
the world. This section has the following steps:
1. First, add a
createMesh
method that takes a
CubeCell
parameter as the input.
This method will create a mesh for the cell, and here you'll use the data we set up
in the
Getting Ready
section of this recipe.
2. First of all, place the vertex data in the mesh with the following line of code:
m.setBuffer(VertexBuffer.Type.Position, 3,
BufferUtils.createFloatBuffer(vertices));