Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 19 ■ Game Content enGine: ai LoGiC with random Content SeLeCtion methodS

Summary


In this nineteenth chapter, we learned about how to implement random selection of game board square
content while implementing more gameplay code that intelligently tracks game board spins and where the
quadrant will land on every spin using Java mathematic operators and simple yet powerful programming
algorithms and structures. We debugged a few problems in the order of our angle offset evaluation and what
populateQuadrant() method these pointed to, and we found a way to load images into memory without
declaring more than two dozen game board diffuse texture images in system memory at one time. This
approach allows us to add hundreds of content images to the game app without generating out-of-memory
errors.
We constructed several new custom methods, including resetTextureMaps(),
calculateQuadrantLanding(), populateQuadrantOne(), populateQuadrantTwo(), populateQuadrantThree(),
and populateQuadrantFour().
We added more game logic to your createSceneProcessing() method MouseEvent handling logic so that
on each spin, the game AI logic will now keep track of every single spin from game startup. This will allow
us to develop an algorithm that will calculate a landing angle at the time of each spin, which will give the
game logic the knowledge of what the current “landing quadrant” is for every spin, which is critical to all the
other gameplay logic we’ll develop.
We developed an elegant solution that uses only whole numbers (degrees in an angle) or int numbers
(integers) that throws away the full rotations (360 degrees) and keeps only the delta (the latest quadrant
angle offset) by dividing a spinDeg accumulator variable by 360 and keeping only the remainder angle
offset (the part beyond a full 360-degree rotation) using the Java % remainder operator, which divides a
numerator (spinDeg total) by a divisor (360 degrees) and places the remainder in the quadrantLanding
variable on the other side of the equals (=) operator.
We developed four populateQuadrant() methods to hold the code that randomly selects content for
each of the five game board squares attached to each quadrant. These methods can be expanded as game
content is added.
We also developed a resetTextureMaps() method that resets the game board to a default blank state
before the next spin. We saw how to “reuse” Image instantiation, referencing a different texture map. This
will request Java 9 to perform garbage collection to reload image content memory locations, rather than
having to load Image objects for every texture map for the game content into system memory, which would
cause an out-of-memory error!
In Chapter 20 , you are going to develop additional gameplay code infrastructure, which will handle
what happens when a player clicks (selects) the game square content itself so that you can finish the
MouseEvent event handling code concerning click events pertaining to the 3D spinner UI and to each of
these game board squares. We’ll also add Camera Animation objects in the next chapter so your camera
object animates in closer to your board!

Free download pdf