Game Development Reference
In-Depth Information
java.lang.Object
> java.util.AbstractCollection<E>
> java.util.AbstractSet<E>
> java.util. HashSet<E>
The HashSet class implements the Set<E> interface in the form of a hash table ,
which is actually an instance of a HashMap . The HashSet makes no guarantees as to
the iteration order of the Set<E> of objects; in particular, it does not guarantee that the
order will remain constant over time. This class permits the use of one null element.
It is important to note that the Set<E> implementation is not synchronized . If mul-
tiple threads access your HashSet object concurrently, and at least one of these threads
modifies your Set<E>, then it should be synchronized externally. This is typically ac-
complished by synchronizing on some object that naturally encapsulates the Set<E>
such as the HashSet. We are using the HashSet in a very basic fashion: to hold objects
that are removed from the game play for one reason or another, such as treasure being
found; enemies being eliminated; or similar game design scenarios.
One of the advantages of the HashSet class (and object) offers is a constant time
performance for your basic dataset operations such as the .add(), .remove(), .contains(),
and .size() methods. Iteration over a HashSet object will require a time period propor-
tional to the sum of the Set<E> object instance size, which is determined by the num-
ber of elements currently in the Set<E> combined with the “capacity” of the backing
HashMap object instance.
Creating Your Casting Engine: Cast-
ingDirector.java
Now that you have some background on Java interfaces, the Java Collection Frame-
work, and its List<E> and Set<E> interfaces implemented by the ArrayList<E> and
HashSet<E> classes, we can move on to create our basic CastingDirector class. The
class will keep a List object of what Actor objects are currently “in play” in the current
scene and another List object of what Actor objects should be checked for collisions.
There will also be a Set object to hold Actor objects that need to be removed. Right-
click on the invincibagel package folder in the NetBeans Projects pane, and select the
New Java Class menu sequence to bring up the New Java Class dialog, which is
shown in Figure 10-2 . Name the new class CastingDirector and leave the other fields
in the dialog, which are automatically set by NetBeans.
 
Search WWH ::




Custom Search