Is a library for using game controllers with nodejs. Currently only the dualshock 3 is supported, but support for xbox360 controller is planned for a future release.
Easy install using npm
npm install node-pad
Beware it uses a third party node module with c++ bindings to communicate with hid devices and is not guaranteed to work on your system.
First, some taster code. Just to blow your mind.
var dualshock3 = require('node-pad').dualshock3
dualshock3.on('connect', function(controller) {
var player = game.createPlayer()
controller.on('join', function(){
player.joinGame()
controller.lthumb.on('move', function(event){
player.moveInDirection({
x: event.x,
y: event.y
})
})
controller.lthumb.on('release', function(){
player.stopMoving()
})
controller.rthumb.on('move', function(event){
player.updateCameraDirection({
x: event.x,
y: event.y
})
})
controller.rthumb.on('release', function(){
player.stopCamera()
})
controller.r2.on('press', function(){
player.fireWeapon()
})
controller.l2.on('press', function(pressure){
player.throwGrenade(pressure)
})
controller.cross.on('press', function(pressure){
player.jump(pressure)
})
controller.l3.on('press', function(){
player.enterCrouchingPosition()
})
controller.l3.on('release', function(){
player.leaveCrouchingPosition()
})
})
controller.on('disconnect', function(){
player.exitGame()
})
})
The nodepad module fires a connect
event on the coresponding controller type when a new controller is plugged in.
nodepad.dualshock3.on('connect', function(controller){
// controller object is available
})
In the case of the dualshock3 there is an additional event join
that is fired when a controlle is connected and has started to send data. Sometimes it starts right away and sometimes it waits for the ps button to get pressed. This has to be figured out in a future release.
nodepad.dualshock3.on('connect', function(controller){
controller.on('join', function(){
// Bind events
})
})
The controller object has buttons, dpads and thumbs(sticks) that you can bind events to. What kind of elements it has depend on the controller layout and naming.
The dualshock3 controller object has this layout:
Thes following buttons are pressure sensitive.
They emit the followng events
function(pressure){}
function(pressure){}
function(){}
The following buttons are regular buttons:
They emit the following events
function(){}
function(){}
Thumbs are controller joysticks and has a y and x position.
They emit the following events
function(event){
event.x
event.y
}
function(x){}
function(y){}
function(){}
To hack on this module please fork it and get all ninja on it. But remember to install the node-hid dependency.
# clone your fork
git clone "your.fork.url"
cd node-pad
# install dependencies
npm install
To run tests
cd node-pad
npm test