Game Development Reference
In-Depth Information
If this were implemented in a language that provides multiple inheritance, such
as C++, it might be tempting to have
DUGameObject
just directly inherit from
UGameObject
and
DGameObject
. But this will make your code very com-
plicated, because
DUGameObject
will inherit from two different parents
(
UGameObject
and
DGameObject
) that in turn both inherit from the same
grandparent (
GameObject
). This issue is known as the
diamond problem
, and
although there are solutions to this problem, it's typically best to avoid the situ-
ation unless there's a very good reason for it.
Once these three types of classes are implemented, it's easy to incorporate them
into the game loop. There could be a
GameWorld
class that has separate lists for
all the updateable and drawable game objects in the world:
class
GameWorld
List
updateable
Objects
List
drawableObjects
end
When a game object is created, it must be added to the appropriate object list(s).
Conversely, when an object is removed from the world, it must be removed from
the list(s). Once we have storage for all our game objects, we can flesh out the
“update game world” part of our loop, as shown in
Listing 1.4
.
Listing 1.4
Final Game Loop
while
game is running
realDeltaTime
= time since last frame
gameDeltaTime
=
realDeltaTime
*
gameTimeFactor
// Process inputs
...
// Update game world
foreach
Updateable
o
in
GameWorld.updateableObjects