Curious about 3D game development? Now is the time to learn! This five-part tutorial series will demonstrate how to build a simple game with ShiVa3D Suite, a cross-platform 3D game engine and authoring tool. This is the third installment of the series, where you will learn how to edit the scenes of a game and begin coding AI Models.

Developing the Game in the ShiVa Editor - Continued

In part 2, we started by describing how to develop the game using ShiVa Editor. We introduced the ShiVa Editor modules used in developing the tutorial application, and we talked about the Collada model files representing the main characters in the application. We then discussed some initial steps necessary to create the application, such as creating the game and the scene and importing the Collada models. In part 3, we will show how to edit the scene of our application. We will also start entering the code for the AIModels of the game. In particular, we will enter the code for the DuckAI.

Edit the Scene

We will now set various properties of the scene.

We assume that the image file marble.jpg has been copied under the folder named D:\temp\skybox. In your environment, if you copy the file to another location be sure to change the instructions below accordingly.

In the DataExplorer, go to Import -> Texture. In the dialog, select the file as shown below and click Import.

Figure 33. Importing The Texture

Now, in the DataExplorer's Textures folder, you should see a texture named marble.

Figure 34. Textures Folder

Let's edit various attributes of the scene using the Ambience Editor. Bring up Ambience Editor and Data Explorer side by side and double click on MyScene in the Scenes folder. At this point, you may be prompted to save the scene, as shown below. Click yes and dismiss the dialog.

Figure 35. Save Prompt

Now, the Ambience Editor and Data Explorer should look side by side as follows.

Figure 36. Ambience Editor

In Lighting section, adjust the Skylight color to obtain a fading yellow tone as shown below.

Figure 37. Skylight Color

For each of the skybox elements in the Sky section, select the marble.

Figure 38. Skybox Settings

Bring Game Editor and Data Explorer side by side. Select Duck -> Games in Data Explorer and double-click on Duck. This will bring up the game in the Game Editor. Select Resources -> Fonts in the Data Explorer. In the Game Editor, press the Resources tab. Drag and drop the Default Font from the Data Explorer into the Resources tab (see below).

Figure 39. Default Font

We will now add a tag to egg. (An object can be searched and located in a game programmatically based on its tag.) Bring Scene Explorer and Data Explorer side by side. On Data Explorer, select Duck -> Scenes -> MyScene and double click. At this point, you may be prompted to save MyScene as follows. Click yes and dismiss the dialog.

Figure 40. Save Prompt for MyScene

In the Scene Explorer, highlight the sphere and from the right-click menu select the Edit Selection Tag. In the dialog, type 'egg' for the tag. Those steps are shown in the two consecutive images below.

Figure 41. Editing the Selection Tag

Figure 42. Tag Dialog

AIModels and the Code

We will now create the AIModels and the associated code for our game. Recall from previous discussion that the AIModels represent the behavior of the objects in a game. The Duck and the Egg will each have their own AIModels, DuckAI and EggAI respectively. In addition, the game itself will have its own AIModel, called MainAI. The MainAI will be responsible for initializing the camera and the scene as well as processing and dispatching user events.

Let us first review some of the concepts pertaining to AIModels, borrowing information from the ShiVa Editor help documentation.

A handler contains code to process a particular event. In addition to a pre-defined set of handlers, one can also define custom handlers. We will be implementing only a subset of the pre-defined handlers, as listed below.

onInit: This handler is called when the object associated with the AIModel is created; typically used for initialization tasks.

onEnterFrame: A game is a loop of frames. This handler is called on each frame. You would mainly use this handler to define the motion of an object.

onTouchSequenceBegin: This handler is called when the user starts touching the screen of the device. This event handler is a way to notify the AIModel code that a sequence of touch events is starting.

onTouchSequenceChange: This handler is called for each touch event in the touch sequence, passing the number of touches and coordinates of each touch.

onTouchSequenceEnd: This handler is called when user stops touching screen of the device. This event handler is a way to notify the AIModel code that the sequence of touch events has ended. The touch events to be processed by onTouchSequenceBegin, onTouchSequenceChange and onTouchSequenceEnd are available only for a multitouch compatible device.

onSensorCollisionBegin: This event handler is called when a sensor associated with the object collides with another sensor.

A function contains code for processing specific tasks. Functions are not predefined, you would define and implement them as you need. A function can send or post events to a handler. (The basic difference between sending and posting an event is that the execution of a sendEvent is instantaneous, whereas the execution of a postEvent is performed after a requested delay.) A function can be directly called from another function or handler. In addition, a function can be the target of a sendEvent or a postEvent.

A variable can be globally accessed from the functions and handlers in a particular AIModel.

AIModels also support the concept of state which is not used in this tutorial.

Let us now start defining the AIModels in our game. Bring up the Scene Explorer and the Data Explorer side by side and double click on MyScene in the Data Explorer under the Scenes folder. (If you are prompted, save MyScene and dismiss the dialog.) On the Scene Explorer, select the Objects tab and select duck, then right-click menu Controllers -> AI -> Create AI. This is shown below.

Figure 43. Creating the DuckAI

Name the AI as DuckAI in the dialog as shown below and press OK.

Figure 44. Create Dialog for the DuckAI

Similarly, on the Scene Explorer's Objects tab, select sphere, then right-click menu Controllers -> AI -> Create AI. Name the AI as EggAI in the dialog and click OK.

In Data Explorer, you should now see three AIModels in the Resources -> AIModels folder: DuckAI, EggAI, MainAI. Open the Game Editor and the Data Explorer side by side. In Game Editor, select the Main tab. From Resources -> AIModels folder, drag and drop MainAI into the User MainAI's text area in Game Editor (see below).

Figure 46. The MainAI

Now, open the AIModel Editor and Data Explorer side by side. Double click on the DuckAI, EggAI and MainAI. You should see a separate tab for each of the AIModels in AIModel Editor as shown below.

Figure 47. AIModel Editor

Enter Variables and Code for the DuckAI

On the DuckAI tab, click Add Variable and add a variable of type boolean. Name it isPositive with true being the initial value (shown below). Click OK to dismiss the dialog.

Figure 48. Defining a Variable

Similarly, add a variable named 'score' where the type is 'number' and the initial value is 0. Even if you type it as 0, the variable will be initialized as a float, i.e. 0.000.

Bring the AIModel Editor and the Script Editor side by side. On the AIModel Editor's DuckAI tab, double-click onInit(). A skeleton code for this will be opened in the Script Editor as shown below.

Figure 52. onInit() Skeleton Code

Copy and paste the following in the script editor, completely replacing the existing code (a review of the code will be given in a later section). In the Script Editor click Control+S to save the script.

Closing Remarks for Part 3

In part 3, we showed how to edit the scene of our application. We also started entering the code for the AIModels of the game. So far, we have completed the code for the DuckAI. In part 4, we will finish coding for the remaining AIModels, EggAI and the MainAI, and perform unit testing by animating the game. We will then export the game from the ShiVa Editor for importing into the Shiva Authoring Tool. Finally, we will discuss two different authoring options in the Shiva Authoring Tool, one for generating an Android executable and another one for generating an Eclipse project.