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 8 ■ Creating Your Actor Engine: Design the Characters for Your Game and Define Their Capabilities<br />

Game Actor Design: Defining the Attributes Up Front<br />

The foundation of any popular game is the characters – the Hero and his Arch Enemies – as well as the game’s<br />

obstacles, armory (projectiles), and treasures. Each of these “actors” need to have attributes, defined using variables in<br />

<strong>Java</strong>, that keep track of what is going on with each of these actors during game play in real time, using areas of system<br />

memory. I am going to try to do this right the first time, in the same sense that you want to define a database record<br />

structure to hold the data you will need out into the future correctly the first time that you define your database.<br />

This can be a challenging stage of your game development, as you need to look out into the future and ascertain what<br />

features you want your game and its actors to have, and then put those into your actor’s capabilities (variables and<br />

methods) up front. Figure 8-1 gives you an idea of some of the two dozen attributes that we will be installing for the<br />

game actors over the course of the chapter as we create over a hundred lines of code to implement our actor engine<br />

for the game.<br />

Figure 8-1. Design a public abstract Actor superclass and a public abstract Hero subclass to use to create sprite classes<br />

As you can see, I’m trying to get a balanced number of variables; in this case it’s about a dozen each, between the<br />

fixed sprite Actor class and the motion sprite Hero class. As you know from Chapter 3, because the Hero subclass we’ll<br />

be creating extends the Actor superclass, it actually has two dozen attributes or characteristics, as it assumes all of the<br />

superclass variables, in addition to having its own. A design challenge will be to put as many of these attributes in the<br />

Actor superclass as possible, so that fixed sprites have as much flexibility as possible. A good example of this is that in<br />

the first rounds of design I had the pivot point (pX and pY variables) in the Hero class, but then I thought about it and<br />

thought “what if I want to rotate fixed sprites (obstacles and treasure) later on for more level design efficiency” so I<br />

placed these variables in the Actor superclass, giving this pivot (rotate) capability to both fixed and motion sprites.<br />

Another variable that I had in the Hero class that I moved “up” into the Actor superclass is the List<br />

property. I thought to myself during this design process, “What if for some reason I want my fixed sprites to have<br />

more than one image state?” I also upgraded the Actor class from using a simple Rectangle Shape object to using a<br />

SVGPath Shape subclass, so that I can define collision geometry (which is what a spriteBounds variable is) using<br />

more complex shapes than a Rectangle to support advanced obstacle constructs in later levels of the game that are<br />

more complex.<br />

Also note that I have the spriteFrame ImageView, which holds the sprite image assets, in the Actor class, as<br />

both fixed and motion sprites use images, so I can put the ImageView into the Actor superclass. I use the imageStates<br />

List in the Actor superclass, so that fixed sprites have access to different “visual states” just like the motion<br />

sprites do. As you may have guessed, List is a <strong>Java</strong> List object filled with <strong>Java</strong>FX Image objects. The iX and iY<br />

variables in the Actor class are image (or initial) placement X and Y locations, which place a fixed sprite on your game<br />

level layout, but will also hold current sprite position for motion sprites, when assumed by the Hero subclass. Other<br />

variables hold Boolean states (alive/dead, etc.) and lifespan, damage, offset, collision, or physics data we’ll need later.<br />

166<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!