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

This class is a member of the <strong>Java</strong> Collections Framework, as you might have surmised, as a List, as well as an<br />

Array, both contain collections of data, much like a data structure (or a data store) does, only in a simpler format.<br />

An ArrayList class can “implement” or support the following <strong>Java</strong> Interfaces: Serializable, Cloneable, Iterable,<br />

Collection, List, and RandomAccess. We will be using the List or, in our case, List <strong>Java</strong> interface,<br />

which we will be looking at in the next section on List when we will learn about <strong>Java</strong> Interfaces.<br />

Essentially the ArrayList class (and object) creates a resizable Array implementation of the List interface. An<br />

ArrayList object thus implements all optional List operations, and permits all types of List elements, including null. In<br />

addition to implementing the List <strong>Java</strong> interface, this class also provides method calls, including a .removeAll(),<br />

.addAll(), and .clear(), which we will be using in our class, to manipulate both the List content as well as the size of the<br />

ArrayList object that is used internally to store the List of Actors (for List) or Images (for List) used.<br />

Each ArrayList object instance has a capacity. The capacity is the size of the Array used to store the elements<br />

(objects) in the List implementation: in our case, a List. The capacity will always be at least as large as the<br />

List size. As elements (Actor objects) are added to an ArrayList, its capacity will grow automatically, which makes it<br />

perfect for our CastingDirector class, as we can make levels of the game more and more complex that is, it can utilize<br />

more Actor objects in the ArrayList of List.<br />

It is important to note that the List implementation is not synchronized (capable of running on multiple<br />

threads simultaneously). If you need to have multiple threads access an ArrayList instance concurrently (at the same<br />

exact time), and at least one of these multiple threads modifies your List structure, it must be synchronized<br />

externally (manually, using your code). We are going to call the CastingDirector class specifically when an enemy is<br />

killed, or a projectile is shot, or a treasure is found (collected) and will not have it being continually called on a pulse.<br />

A structural modification of an ArrayList object is an operation that adds or removes one or more elements;<br />

merely setting the value of an element (Actor) in the ArrayList would not be considered to be structural modification.<br />

The <strong>Java</strong> Interface: Defining Rules for Implementing Your Class<br />

Before we look at the List <strong>Java</strong> interface, let’s take a look at what <strong>Java</strong> interfaces do in general, as we did not have<br />

enough pages to cover all of the <strong>Java</strong> programming language back in Chapter 3. So I am going to cover some of the<br />

more advanced <strong>Java</strong> topics as we need to learn them during the book. A good example of this is lambda expressions<br />

in Chapter 9 and <strong>Java</strong> interfaces here in Chapter 10. The reason for using a <strong>Java</strong> interface is to make sure that other<br />

programmers who are going to use your code implement it correctly; that is, include everything necessary for the code<br />

to work properly.<br />

Essentially, all an interface specifies is the group of related methods that is needed for another developer to<br />

implement your class. These are specified with “empty method” code bodies. If you wanted to have other developers<br />

use the Hero class, for instance, you would specify a Hero interface. This would be done using the following <strong>Java</strong> code:<br />

public interface Hero {<br />

public void update();<br />

public boolean collide(Actor actor);<br />

}<br />

As you can see, this is similar to what we did with the .update() method in the Actor superclass, as there is no<br />

{code body} specified as there usually is in a method. Thus, in a sense, a <strong>Java</strong> interface is also used in an abstract<br />

fashion to define what needs to be included in a class that “implements” the <strong>Java</strong> interface. As you probably have<br />

guessed, you would thus use the <strong>Java</strong> implements keyword in your class declaration to implement a <strong>Java</strong> interface.<br />

www.it-ebooks.info<br />

209

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

Saved successfully!

Ooh no, something went wrong!