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 10 ■ Directing the Cast of Actors: Creating a Casting Director Engine and Creating the Bagel Actor Class<br />

The HashSet class implements the Set interface in the form of a hash table, which is actually an instance of<br />

a HashMap. The HashSet makes no guarantees as to the iteration order of the Set of objects; in particular, it does<br />

not guarantee that the order will remain constant over time. This class permits the use of one null element.<br />

It is important to note that the Set implementation is not synchronized. If multiple threads access your<br />

HashSet object concurrently, and at least one of these threads modifies your Set, then it should be synchronized<br />

externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the Set<br />

such as the HashSet. We are using the HashSet in a very basic fashion: to hold objects that are removed from the<br />

game play for one reason or another, such as treasure being found; enemies being eliminated; or similar game design<br />

scenarios.<br />

One of the advantages of the HashSet class (and object) offers is a constant time performance for your basic<br />

dataset operations such as the .add(), .remove(), .contains(), and .size() methods. Iteration over a HashSet object will<br />

require a time period proportional to the sum of the Set object instance size, which is determined by the number<br />

of elements currently in the Set combined with the “capacity” of the backing HashMap object instance.<br />

Creating Your Casting Engine: CastingDirector.java<br />

Now that you have some background on <strong>Java</strong> interfaces, the <strong>Java</strong> Collection Framework, and its List and<br />

Set interfaces implemented by the ArrayList and HashSet classes, we can move on to create our basic<br />

CastingDirector class. The class will keep a List object of what Actor objects are currently “in play” in the current<br />

scene and another List object of what Actor objects should be checked for collisions. There will also be a Set object to<br />

hold Actor objects that need to be removed. Right-click on the invincibagel package folder in the NetBeans Projects<br />

pane, and select the New ➤ <strong>Java</strong> Class menu sequence to bring up the New <strong>Java</strong> Class dialog, which is shown in<br />

Figure 10-2. Name the new class CastingDirector and leave the other fields in the dialog, which are automatically set<br />

by NetBeans.<br />

Figure 10-2. Create a New <strong>Java</strong> Class in the invincibagel package; name it CastingDirector for the InvinciBagel Project<br />

We will start by creating the List ArrayList objects first, one to hold the current cast for<br />

your scene and then a second List to hold objects to be checked for collision detection. After that we will<br />

create the Set HashSet object, which will provide an unordered Set object, which will collect<br />

those Actor objects that need to be removed from the scene. Let’s get started creating the body of our public<br />

CastingDirector class.<br />

www.it-ebooks.info<br />

213

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

Saved successfully!

Ooh no, something went wrong!