28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 17 ■ Enhancing Game Play: Creating a Scoring Engine, Adding Treasure and an Enemy Auto-Attack Engine<br />

Adding Bullets to a Clip: Updating .addCurrentCast( )<br />

Since we are going to add single (one at a time, not unmarried) Actor objects back into the CURRENT_CAST List, we<br />

will need to modify the .addCurrentCast() method as it currently only takes an Actor . . . series of objects and we need<br />

it to accommodate a single added Actor object, like the addToRemovedActors() method does currently. You can see<br />

the .addToRemovedActors() method at the bottom of Figure 17-47. The .addCurrentCast() method was designed<br />

to be used statically in the InvinciBagel.java class, in the start() method, to add all of the cast members at one time<br />

(remember static versus dynamic). Now I am going to show you how to redesign it to allow “one off” additions during<br />

gameplay, which is a dynamic use of the method, as the List will be modified dynamically in real-time during<br />

gameplay. To upgrade the .addCurrentCast() method, simply “wrap” the .addAll() method call inside of the method<br />

with an if(actors.length > 1) if-else structure, with the original code inside of the if() portion and a CURRENT_CAST.<br />

add(actors[0]); statement inside of the else portion to accommodate single Actor method calls. The <strong>Java</strong> code for<br />

the new method structure can be seen in Figure 17-47, and should look like the following:<br />

public void addCurrentCast(Actor... actors) {<br />

if(actors.length > 1) {<br />

CURRENT_CAST.addAll(Arrays.asList(actors));<br />

} else {<br />

CURRENT_CAST.add(actors[0]);<br />

}<br />

}<br />

Figure 17-47. Upgrade addCurrentCast() method in the CastingDirector class to accept a single object in parameter list<br />

www.it-ebooks.info<br />

439

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!