mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
Added rudimentary Gamepad controller code
This commit is contained in:
parent
281cbdc3a4
commit
ce0d0204ea
3 changed files with 90 additions and 2 deletions
80
app/Game/Client/Control/Input/GamepadInput.js
Normal file
80
app/Game/Client/Control/Input/GamepadInput.js
Normal file
|
|
@ -0,0 +1,80 @@
|
||||||
|
define([
|
||||||
|
"Game/Client/Control/Input/XyInput",
|
||||||
|
"Game/Config/Settings",
|
||||||
|
"Lib/Utilities/NotificationCenter",
|
||||||
|
"Lib/Utilities/RequestAnimFrame"
|
||||||
|
],
|
||||||
|
|
||||||
|
function (Parent, Settings, Nc, requestAnimFrame) {
|
||||||
|
|
||||||
|
function GamepadInput(playerController) {
|
||||||
|
this.playerController = playerController;
|
||||||
|
Parent.call(this);
|
||||||
|
|
||||||
|
this.gamepad = null;
|
||||||
|
this.oldHoldingPressure = 0;
|
||||||
|
this.keyChanged = {
|
||||||
|
w: false,
|
||||||
|
a: false,
|
||||||
|
d: false
|
||||||
|
};
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
window.addEventListener("gamepadconnected", function(e) {
|
||||||
|
self.gamepad = e.gamepad;
|
||||||
|
});
|
||||||
|
|
||||||
|
window.addEventListener("gamepaddisconnected", function(e) {
|
||||||
|
self.gamepad = null;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
GamepadInput.prototype = Object.create(Parent.prototype);
|
||||||
|
|
||||||
|
GamepadInput.prototype.update = function() {
|
||||||
|
if(this.gamepad) {
|
||||||
|
var x = this.gamepad.axes[2];
|
||||||
|
var y = -this.gamepad.axes[3];
|
||||||
|
|
||||||
|
// Looking direction
|
||||||
|
this.playerController.xyInput.onXyChange(x, y);
|
||||||
|
|
||||||
|
// Pointer finger holding item
|
||||||
|
var holdingPressure = this.gamepad.axes[5];
|
||||||
|
if((holdingPressure > 0 && this.oldHoldingPressure <= 0)
|
||||||
|
|| (holdingPressure <= 0 && this.oldHoldingPressure > 0)) {
|
||||||
|
this.playerController.handActionRequest(x, y);
|
||||||
|
}
|
||||||
|
this.oldHoldingPressure = holdingPressure;
|
||||||
|
|
||||||
|
this.simulateKeyboard();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
GamepadInput.prototype.simulateKeyboard = function() {
|
||||||
|
// walking
|
||||||
|
/*
|
||||||
|
a:0 b:1 x:2 y:3
|
||||||
|
lb:4 rb:5 jl:6 jr:7
|
||||||
|
start:8 back:9 xbox:10
|
||||||
|
u:11 d:12 l:13 r:14
|
||||||
|
*/
|
||||||
|
this.key(11, 87);
|
||||||
|
this.key(13, 65);
|
||||||
|
this.key(14, 68);
|
||||||
|
};
|
||||||
|
|
||||||
|
GamepadInput.prototype.key = function(i, key) {
|
||||||
|
if(this.keyChanged[key] != this.gamepad.buttons[i].pressed) {
|
||||||
|
var evt = {keyCode:key}
|
||||||
|
if(this.gamepad.buttons[i].pressed) window.onkeydown(evt);
|
||||||
|
else window.onkeyup(evt)
|
||||||
|
|
||||||
|
this.keyChanged[key] = this.gamepad.buttons[i].pressed;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
return GamepadInput;
|
||||||
|
|
||||||
|
});
|
||||||
|
|
@ -2,10 +2,11 @@ define([
|
||||||
"Game/Core/Control/PlayerController",
|
"Game/Core/Control/PlayerController",
|
||||||
"Game/Client/Control/KeyboardInput",
|
"Game/Client/Control/KeyboardInput",
|
||||||
"Game/Client/Control/Input/MouseInput",
|
"Game/Client/Control/Input/MouseInput",
|
||||||
"Lib/Utilities/NotificationCenter"
|
"Lib/Utilities/NotificationCenter",
|
||||||
|
"Game/Client/Control/Input/GamepadInput",
|
||||||
],
|
],
|
||||||
|
|
||||||
function (Parent, KeyboardInput, MouseInput, Nc) {
|
function (Parent, KeyboardInput, MouseInput, Nc, GamepadInput) {
|
||||||
|
|
||||||
function PlayerController (me) {
|
function PlayerController (me) {
|
||||||
|
|
||||||
|
|
@ -13,6 +14,7 @@ function (Parent, KeyboardInput, MouseInput, Nc) {
|
||||||
|
|
||||||
this.keyboardInput = new KeyboardInput(this);
|
this.keyboardInput = new KeyboardInput(this);
|
||||||
this.xyInput = new MouseInput(this);
|
this.xyInput = new MouseInput(this);
|
||||||
|
this.gamepadInput = new GamepadInput(this);
|
||||||
|
|
||||||
this.ncTokens = [
|
this.ncTokens = [
|
||||||
Nc.on(Nc.ns.client.input.xy.change, this.setXY, this),
|
Nc.on(Nc.ns.client.input.xy.change, this.setXY, this),
|
||||||
|
|
@ -44,6 +46,10 @@ function (Parent, KeyboardInput, MouseInput, Nc) {
|
||||||
|
|
||||||
PlayerController.prototype = Object.create(Parent.prototype);
|
PlayerController.prototype = Object.create(Parent.prototype);
|
||||||
|
|
||||||
|
PlayerController.prototype.update = function() {
|
||||||
|
this.gamepadInput.update();
|
||||||
|
};
|
||||||
|
|
||||||
PlayerController.prototype.init = function (keys) {
|
PlayerController.prototype.init = function (keys) {
|
||||||
|
|
||||||
this.keyboardInput.registerKey(keys.a, 'moveLeft', 'stop');
|
this.keyboardInput.registerKey(keys.a, 'moveLeft', 'stop');
|
||||||
|
|
|
||||||
|
|
@ -50,6 +50,8 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
|
||||||
|
|
||||||
this.animationRequestId = requestAnimFrame(this.update.bind(this));
|
this.animationRequestId = requestAnimFrame(this.update.bind(this));
|
||||||
|
|
||||||
|
if(this.me && this.me.playerController) this.me.playerController.update();
|
||||||
|
|
||||||
this.physicsEngine.update();
|
this.physicsEngine.update();
|
||||||
|
|
||||||
if(this.me) {
|
if(this.me) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue