This game has been built using the Microsoft Platform Starter Kit Game RubbaRabbit platformer
We are actually going to consider two variants:
A game for Windows 8. x and Web Browser game.
These samples include some C++ code this C++ code is simply required as its the library project-strapping above XInput to enable it to be able to work in your HTML5/JS game
So firstly we need to add the game contraoller to the project. You need to add the C++ .cpp files and header .h files to your solution within the game and add a link to it within your project for Windows 8. x References:
If you wish, you can open up the C++ file with Visual Studio to see what they actually do, simply they are an interface to access the controller: the constructor to obtain a reference to the controller and function getState to retrieve its current state with the projection on the Xbox controller buttons.
Now you need to add support for the GamePad in the game itself. To do this, usually, you have to make quite a small change in processing input from the player.
The game loop
So the game loop is simply the code inside the game which loops on a regular cycle, The game loop forms every scene of the game. In our case we want to check whether the user pressed the button.
In this example the game loop is defined inside the function update:
You need to include the function which is processing the buttons and sticks:
As you can see, the engine developers are already taken care of that the user can send a command not only with different buttons with the keyboard jaws.pressed("a")
What nice is that they have also added support for virtual buttons on a touch device (variables touchleft, touchright , etc.).
Your code must have some variables that meet different game actions and imput controls which support various input methods/devices.
Adding Gamepad support
To add gamepad support is really simple
Firstly we need to initialize the controller and them simply check the status on a regular basis.
To initialize the controller we create a Controller via the designer that is available to us from the connected earlier C++ libraries:
Next, we need to periodically update the status of the controller, for this will describe updateState feature that will query the call on every frame of the animation:
Note that in this case we’ve have added support for touch, keyboard and controller input so a variety of control methods could be used.
GamePad support in browser
GamePad support is currently being developed in W3C standard Gamepad API that will consistently work with different types of game controllers and browsers.
The W3C standard assumes "General" model
The game loop and game code
To demonstrate, I am continuing to use the same example of the Starter Kit, only this time, create an empty Web project and copy the original source files.
Because the game is simply HTML5/JS , the game simply runs in your browser. Everything else remains unchanged, and we use the same assumptions about the game loop, which we did for a project under Windows 8.
What’s great about HTML5/JS is that you can use code from the Babylon.js library which support gamepad input
babylon.gamepads.ts (GitHub). You can simply copy the library itself or make it a fork.
The library makes a few important things:
- The Gamepad API to simulate an event-driven model (if you need);
- makes it easy to access the individual elements of the controller (such as a joystick axis brings together into one object);
- distinguish between an Xbox controller
Please note that the library is written in TypeScript.
You can simply add the library inside the project, Visual Studio includes support for the TypeScript and automatically generates the .js files when saving.
Do not forget to connect the library to the page with the game:
The wiring diagram of the controller is very similar to what we did in the case of Windows 8 x:
Inside features BABYLON.Gamepads is passed to an event handler connection controller from your computer. As you can see, adding support to the project controller is about 20 lines of code
Version of the code with event-based programming model:
As a result, we can easily control the character in the game directly from the Xbox controller
- Starter-Kits for games on the Windows 8: Platformer Starter Kit, Tower Defence Starter Kit
- W3C GamePad API W3C Draft standard for browsers
- babylon.gamepads.ts Wrapper on the GamePad API
- Microsoft Virtual Academy Course on the development of universal applications for HTML/JS
- Download Visual Studio
- Become a Windows 8 and Windows Phone developer
- Windows Insider – Get access to Windows 10 Preview