Ape Attack

Ape Attack is a 3D Java demonstration game using the Eclipse IDE, the  JPCT 3D Java graphics engine, and SoundSystemJPCT, the JPCT friendly 3D sound library. The program was written to experiment with loading and manipulating animated 3D models. There’s also an element of a pathfinding AI involving the A* algrorithm. A screen shot is shown below:


1. Gameplay

Since it’s a demo, the Ape Attack gameplay is minimal. There are these two apes see, and they’re attacking. Specifically, they’re attacking the warrior who runs around under keyboard and mouse control. In addition to running around the warrior can fire a blast at the apes which will eventually finish them off. The main point of interest is that when the apes chase the warrior, they will avoid the two cabins and thanks to the A* algorithm will follow the most efficient non-obstructed path to the warrior.

There are limits to the game which are the the limits of the plane used for the terrain. When the player tries to go beyond the limits he will simply stop. The mountains in the background are just a skybox. All the models were free models found online.

2. Controls

The controls are fairly simple. The mouse controls the direction of the warrior. Pressing the spacebar fires a blast. ‘Esc’ exits the program. Warrior movement is controlled from the numeric keypad as follows:

Up Arrow       – move forward
Down Arrow  – move backward
Left Arrow     – move sideways left
Right Arrow   – move sideways right

3. Running the Game

The game was written in Java using the eclipse IDE, so it can be run from Eclipse, or it can be run from the executable jar file. In this case the native lwjgl libraries must reside in same folder as the executable jar, in a \lib\native\windows sub folder. The executable jar file and native library can be downloaded here: ApeAttack_jar.zip

 4. The Software

Even for a demo game, the code can start to get pretty complex. Simply put, the Ape Attack code initializes the JPCT environment, loads the models, sets up the sound effects and camera then enters the main game loop. All the models and game objects have individual class instances which are stored in Array Lists which are updated in the game loop.

4.1 Java Library Path

java.library.path is a system property used by Java to search for native libraries. In this application, the native libraries consist of the Lightweight Java Gaming Library (lwjgl) dlls. To set the java.library.path property under program control, the following code must be run in the main() routine before the application is started:

// Set the java.library.path to the LWJGL native library
System.setProperty("java.library.path", "lib\\native\\windows");
Field fieldSysPath = ClassLoader.class.getDeclaredField( "sys_paths" );
fieldSysPath.setAccessible( true );
fieldSysPath.set( null, null );


4.2 Loading Models

Models and textures are set-up in the EnvGen.java class. In order to create an executable jar file that can be “run” or  executed by double-clicking the jar file. The resources (images etc.) must be read as a runtime class. I usually place my resources (Images and Sounds) in the Eclipse  “src” folder for convenience. This way they will be transferred to the bin folder during the build process, and to the jar file during export. When the code is executed from the jar file, the getClass().getResourceAsStream() command looks for the resource in the jar file instead of any upper level folder. This is illustrated by the code snippet below:

InputStream inStr;

inStr = getClass().getResourceAsStream("Images/warrior/warrior.jpg");
tm.addTexture("warrior", new Texture(inStr));
inStr = getClass().getResourceAsStream("Images/warrior/warrior.md2");
playerModel = Loader.loadMD2(inStr, 0.8f);

Modifications to existing or even new objects can be added in EnvGen.java.

4.3 Source Code

The source code, resource folders (Images and Sounds),  and native library can be downloaded here: ApeAttack_source.zip

 5. Things To Do

As mentioned, this program is strictly a game demo. I was primarily interested in loading and manipulating animated models. To do that, I needed a place to put them, hence the very rudimentary environment. Finally I couldn’t very well have the characters just bumping into things so I got sidetracked with the A* algoritm. All these elements can be improved, but this isn’t a bad starting point.