Configuring the Controllers - Getting Started with OUYA (2014)

Getting Started with OUYA (2014)

Chapter 5. Configuring the Controllers

This chapter will assess the main features of the buttons for the controls and the hardware for the OUYA console.

Configuring the Controllers

The OUYA Controller

The control pad has the following features:

· It is a 2.4 GHz RF wireless controller and a standard Bluetooth controller

· Two (2) AA batteries for each controller

· A touchpad that works like a mouse

· Four digital buttons (O, U, Y, and A)

· A directional pad (D-Pad) that supports four direction buttons

· Two analog sticks (LS and RS)

· Two digital buttons that activate when the analog sticks are pushed straight down (L3 and R3)

· Two shoulder buttons (L1 and R1)

· Two analog triggers (L2 and R2)

· The OUYA (U) button (used to bring you back to the OUYA main menu).

Configuring the Controllers

A description of buttons

Setting the controls

For this book, we will use a limited version of "The Last Maya" as our game is about the actual origin of Maya culture.

The Assets folder of Project Unity contains 3D models that include pyramids, characters, scripts, and the main scene maya.unity.

If you purchase this book, all these resources will be present in the code bundle, in the Mayan_Project1.zip file. Open Mayan_Project1.zip and unzip the project, including Asset/maya, and open the main stage by double-clicking on the maya.unity file.

The purpose of using the OUYA framework is to have an input access tool that allows multiple drivers for multiple controls. It should be clarified that this is the Ouya SDK + Unity plugin, and not the OUYA input framework.

To change the settings in our scene, you need to use the OuyaInputHandler.cs script and the InputManager.asset file. To do this, you need to execute the following steps:

1. Copy the InputManager.asset file to the ProjectSetting folder of your project. In this section, it is very important to check the variables present in the OuyaInputManager.cs file with the configuration of the buttons. This will overwrite any custom changes to the input preferences.

2. Copy the OuyaInputHandlerExample.cs file to the Scripts folder, which is located in the Asset/Ouya/Example/Scripts directory, and rename the script as OuyaInputHandler.cs.

3. Place the OuyaGameObject.cs script in the opening scene that is in the Asset/Ouya/SDK folder and write your developer UUID in the beginning of the section. The SDK comes with an OuyaGameObject prefab, and the developer ID can be set using the inspector of Unity.

4. Another optional script is OuyaInputTest.cs, which is useful for connecting any OuyaGameObject with the GUI's interaction. If not required in your project, you can remove the OuyaInputTest.cs script from the Scripts folder.

Creating the first scene of the game

In the project that is open, it identifies the entire directory structure, including the OUYA SDK directory structure, NGUI, and The Last Maya files.

Creating the first scene of the game

The folder's project structure

Inside the mayan directory, you will find all of the main 3D objects of the project: pyramids, oracle, animations, and the main character.

Creating the first scene of the game

3D models of The Last Maya file

Next, to ensure that our character can move around within the scene, perform the following steps:

1. Insert a terrain in the scene and our character can collide with this terrain. This allows the character to interact with what is basically the "ground" of the scene. Another option could be to add a floor (plane) that interacts with the character.

2. Add a light to the scene. This light allows the player to see the details of the imported models. The light source is needed to illuminate the objects that are present in the scene.

3. Select the main mayan character on the hierarchy window with the F key on the keyboard. Find it in the "script third person" controller contained in the Standard Assets folder and drag the script to the character.

4. The Maya.unity project contains Standard Assets/Character Controllers assets by default. Make sure the controller (the green cylinder) covers the whole body of the character.

5. Click on the Project window and place yourself in Standard Assets/Character Controllers and drag the "third person" code to our character imported as maya.unitypackage.

6. Remove any other camera in the scene and only use the camera that is associated with the character. This camera allows you to incorporate the coordinate axes (X, Y, and Z) to our character.

A character imported in the scene would look like what is shown in the following screenshot:

Creating the first scene of the game

The character has six basic animations that can be seen from the inspector's window of Unity3D. These are walk, run, stand, idle, active, and attack. Each of these animations must be configured in the OuyaInputHandler.cs file linked to a button. It uses the legacy animation system in Unity.

You can find the animations in the mayan folder and drag them to the controller script that appears in the project window section. Each animation contains a logical sequence of frames linked to the character.

Creating the first scene of the game

Character animations

The character must contain the character controller scripts and have associated the OuyaInputHandler.cs file (refer to the following screenshot):

Creating the first scene of the game

Assigning the scripts for a character's controller

To ensure that the character can move through the scenario, you need to locate the controller script in the positions of the X, Y, and Z axes, completely covering the 3D model of the character. It is also necessary for the controller script to be integrated with theOuyaInputHandler.cs file.

Assigning the OUYA controls

The primary code to be amended is present in the OuyaInputManager.cs file where you put the developer UUID and the OuyaInputHandler file, which sets the coordinates and animations of the Ouya control buttons.

For this purpose, we must consider the elements of conventions regarding the joystick and D-pad: the top axis is positive (+) and down axis is negative (-). Additionally, the right axis is positive (+) and the left is negative (-).

In OUYA's analog command, the X and Y values are generated in the range of -1 to +1 with zero being neutral.

Assigning the OUYA controls

The axis joystick and D-pad

The next step is to configure animations and buttons for the character. Before proceeding, we must consider the state of the button.

Each button calls OUYA's control animations such as walk or run. For the following example, the run and idle animations are observed in detail in the Update() function of the OuyaInputHandler.cs script, linked to the character, as shown in the following code:

//ATTACK STANCE:

if (b.Equals(OuyaSDK.KeyEnum.BUTTON_Y) && bs.Equals(OuyaSDK.InputAction.KeyDown))

{

if (!isBattleStance)

{

//Set Battle stance.

this.animation.Play("idle");

isBattleStance = true;

}

else

{

if (this.animation.IsPlaying("attack_1"))

{

this.animation.Play("idle");

isBattleStance = false;

}

}

}

//ATTACK:

if (b.Equals(OuyaSDK.KeyEnum.BUTTON_U) && bs.Equals(OuyaSDK.InputAction.KeyDown))

{

this.animation.Play("attack_1");

}

//RUN:

if (b.Equals(OuyaSDK.KeyEnum.BUTTON_Y) && bs.Equals(OuyaSDK.InputAction.KeyDown))

{

this.animation.Play("run");

}

//FAKE READY:

if (b.Equals(OuyaSDK.KeyEnum.BUTTON_A) && bs.Equals(OuyaSDK.InputAction.KeyDown))

{

this.animation.Play("ready");

}

}

The menu or system button

The SDK includes interfaces for the OUYA menu button. To use the menu or system OUYA button (U), remember to press the menu button twice or press and hold it to go back to OuyaController.Button_menu.

A single press will now send an OuyaController.BUTTON_MENU (KEYCODE_MENU) keycode to the app, emulating a normal menu button. To bring up the system menu, you may now tap the OUYA button twice or press the OUYA buttons on newer controllers for a long time.

When the system button is pressed, the pause menu will be shown:

public void OuyaMenuButtonUp()

{

// your code here

}

The menu or system button

The menu or system button

Differences between an analog joystick and a touchpad

Both the analog stick and the touchpad use the OnGenericMotionEvent event to report a state. To differentiate them, we must assess the input source.

In order to assess the input source, we query the InputDevice class. Depending on the value returned, we can tell if the input is provided by the joystick or the touchpad. The values returned for the specific device are shown as follows:

Joystick == InputDevice.SOURCE_CLASS_JOYSTICK

Touchpad == InputDevice.SOURCE_CLASS_POINTER

This querying can be illustrated with the following example:

@Override

public boolean onGenericMotionEvent(final MotionEvent event) {

//Get the player event

int player = OuyaController.getPlayerNumByDeviceId(event.getDeviceId());

//Joystick

if((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {

float LS_X = event.getAxisValue(OuyaController.AXIS_LS_X);

//do other things with joystick

}

//Touchpad

if((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {

//Print the pixel coordinates of the cursor

Log.i("Touchpad", "Cursor X: " + event.getX() + "Cursor Y: " + event.getY());

}

return true;

}

The difference between using the analog stick and the touchpad is the level of sensitivity and the degrees of freedom for the controls. The joystick is efficient for 3D games and the touchpad is not good for 2D gaming; it is more like using a mouse pointing device. In general, using the touchpad to control a character is more difficult than using the analog stick. It is a great feature that can be used to great effect in the right context, but it needs to be implemented with care.

Testing changes in the state of the button

There is a function within the script that detects when a button is pressed or released, so it can process its current state.

The following is the first section of code that shows the state of the button in use, that is, when an action is executed:

if (c.buttonChangedThisFrame(OuyaController.BUTTON_O)) {

if (c.buttonPressedThisFrame(OuyaController.BUTTON_O)) {

Execute_Action();

The second part of the code (the action is suspended) is as follows:

}

else if (c.buttonReleasedThisFrame(OuyaController.BUTTON_O) {

End_Action();

}

}

The OuyaController class contains OUYA-specific constants for buttons and axes. A small selection is shown in the following code:

public static final int BUTTON_O;

public static final int BUTTON_U;

public static final int BUTTON_Y;

public static final int BUTTON_A;

The following are the three basic events to check the status of the onKeyDown, onKeyUp, or onGenericMotionEvent buttons:

@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

boolean handled = OuyaController.onKeyDown(keyCode, event);

return handled || super.onKeyDown(keyCode, event);

}

@Override

public boolean onKeyUp(int keyCode, KeyEvent event) {

boolean handled = OuyaController.onKeyUp(keyCode, event);

return handled || super.onKeyUp(keyCode, event);

}

@Override

public boolean onGenericMotionEvent(MotionEvent event) {

boolean handled = OuyaController.onGenericMotionEvent(event);

return handled || super.onGenericMotionEvent(event);

}

Once OuyaController gets the events, you can then get an instance of the class by using the device ID or player number, as shown in the following code:

OuyaController c = OuyaController.getControllerByDeviceId(deviceId);

OuyaController c = OuyaController.getControllerByPlayer(playerNum);

It is a good practice to clear the input state when the scene loads and when it is destroyed, as shown in the following code:

void Awake()

{

Input.ResetInputAxes();

}

void OnDestroy()

{

Input.ResetInputAxes();

}

When a controller connects or disconnects, a calibration event will be fired, as shown in the following code:

public void OuyaOnJoystickCalibration()

{

// your code here

}

For the button input, the examples use the following:

OuyaExampleCommon.GetButton (GetButton, GetButtonDown, GetButtonUp)

For the axis input, the examples use the following:

OuyaExampleCommon.GetAxis (GetAxis, GetAxisRaw)

Compiling and executing the game

To compile the game, we must incorporate the game scenes in the Build Settings window. The SceneInit starter Kit (level 0) and Maya are the scenes that correspond to the game (level 1), as shown in the following screenshot:

Compiling and executing the game

Setting up Build Settings

We name our APK file THE_LAST_MAYA, select Android as the platform, and set 16 as the Minimum API level value by allowing correct compilation. The other parameters in the Player Settings window are set by default. They are important because OUYA has input preferences for default values.

The following fields need to be filled in the Identification section:

· Bundle Identifier: This is the project name.

· Bundle Version: By default, it is 1.0.

· Minimun API Level: Select API level 16, which corresponds to Android 4.1. This is the minimum version that supports OUYA.

The following are the fields to be filled in the Configuration section:

· Device Filter: Select only ARMv7 (this is the recommended option for OUYA)

· Install Location: Select Prefer External (this is the recommended option for OUYA)

· Graphics Level: Select Open GL ES 2.0 (this is the recommended option for OUYA)

Compiling and executing the game

Settings for Android

And finally, we click on the Build section in the Build Settings window, to start the compilation.

Summary

This chapter covered the initial steps to optimize the development of a project's scenes, the basic controls, and how to set up the coordinates. The compilation of the game for Android and the OUYA console was also covered.

In the next chapter, you will learn all about making money with your game using the OUYA marketplace.