28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

It is important to note that we could also accomplish this objective by overloading this .addCurrentCast()<br />

method as well. If you wanted to do this in this fashion, the <strong>Java</strong> code would look like the following method bodies:<br />

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

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

}<br />

public void addCurrentCast(Actor actor) {<br />

CURRENT_CAST.add(actor);<br />

}<br />

Once your game design becomes more advanced and you have decorative Actor objects on the Stage, you can<br />

implement the COLLIDE_CHECKLIST in your if() structures that need to iterate through (only) the Actor objects in<br />

your Scene that need to be processed for collision.<br />

At this point in our game design, we are processing all of the Actor objects for collision, thus, we don’t yet need<br />

to implement the COLLIDE_CHECKLIST List Array. I’ve included it in the class design to be thorough and<br />

because I design foundational classes for games by looking out into the future toward what I will need in order for me<br />

to create an advanced (complete) game engine. That said, we may not have enough pages in this Beginner title to get<br />

that advanced, but the functionality is there if you needed to use it in your games, and after this chapter you will have<br />

plenty of experience using if() structures!<br />

Now that we can add cast members on an individual basis, it is time to write the methods that will allow us to<br />

check the CURRENT_CAST List Array to make sure that there are iBullet, iCheese, and iBeagle Actor objects in<br />

place for us to use for the next iteration of the auto-attack engine. What these methods will do is to look for these Actor<br />

objects in the CURRENT_CAST List, and if they are not present, will add one of them to the List so that it is ready for<br />

any type of attack that our conditional if() statements and random number generator create together!<br />

We’ll write three methods, one for deadly projectiles, called .loadBullet(); one for healthy projectiles, called<br />

.loadCheese(); and one for Enemy objects, called loadEnemy(). This will give us the ultimate flexibility, later on in<br />

our game development, of calling each type of functional Actor object type in its own “replenishment function.”<br />

Each method will look through the entire CURRENT_CAST List, using a for() loop, along with a .size() method<br />

call. In this way, the entire List is processed, from the first element (zero), through the last element (the List’s size).<br />

If there is no Actor object of that type found in the cast, that is, if the for() loop is completed, and no object of that<br />

type is found, after looking through the entire CURRENT_CAST List for a match using object.equals(), then<br />

the two statements after the for loop will be executed.<br />

The first statement will add one Actor object of that type to the CURRENT_CAST List, and the second<br />

statement will then add one Actor object of that type to the <strong>Java</strong>FX SceneGraph. At that point, the method is finished,<br />

and will then return control to the calling entity.<br />

If an Actor object of that type is found in the CURRENT_CAST List Array, a return; statement will be<br />

called to immediately exit the method, and return control to the calling entity. This means that the statements at the<br />

end of the for() loop, which add a new Actor object of that type to the cast, as well as adding a new Actor object to the<br />

<strong>Java</strong>FX Scene Graph root object, will not be executed.<br />

440<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!