The gamepad package
The gamepad is the standard input method in console games, but it can also be used in other devices such as PCs, and thanks to the gamepad
package it is really easy to incorporate it into your game.
First of all, you will need to add the package as a dependency to your game. You can do it using the command line clockwork-tools
, opening a command line in your project folder and typing
clockwork add gamepad
Once you have added the dependency, you just need to spawn a gamepad
object in any level you want to detect gamepad input. Add it to the level like this:
<object name="gamepad" type="gamepad" x="0" y="0" ></object>
and you will be ready to start detecting keyboard input!
The gamepad
object you just created will trigger several events that let you know when the user is interacting with it, here is a component that will listen to all of them:
{
name: "gamepadListener",
events: [
{
name: "gamepadAxis", code: function (event) {
//This event is triggered each frame, sends the state of the thumbsticks
event.player; //The id of the gamepad
event.values.forEach(function(thumbstick, i){
i; //The id of the thumbstick
thumbstick.x; //The x value of that thumbstick
thumbstick.y; //The y value of that thumbstick
});
}
},
{
name: "gamepadTrigger", code: function (event) {
//This event is triggered each frame, sends the state of the triggers
event.player; //The id of the gamepad
event.leftValue; //The value of the left trigger
event.rightValue; //The value of the right trigger
}
},
{
name: "gamepadDown", code: function (event) {
//This event is triggered when a button is pressed
event.player; //The id of the gamepad
event.name; //The name of the button
}
},
{
name: "gamepadUp", code: function (event) {
//This event is triggered when a button is released
event.player; //The id of the gamepad
event.name; //The name of the button
}
},
{
name: "gamepadDisconnected", code: function (event) {
//This event is triggered when a gamepad is disconnected
event.player; //The id of the gamepad
}
}
]
}
Just like that, you can add your own logic to detect gamepad input and act acordingly.
Finally, remember that if you need to remember something about how the component is used, you can quickly access the package documentation from Visual Studio Code, running the Browse Clockwork package documentation
command.