added pointer locking and viewport moving

This commit is contained in:
Jeena 2014-12-06 20:20:49 +01:00
parent 072e984215
commit 6d9d02615a
12 changed files with 236 additions and 141 deletions

View file

@ -0,0 +1,78 @@
define([
"Game/Client/Control/Input",
"Game/Config/Settings"
],
function (Parent, Settings) {
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.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;
});

View file

@ -0,0 +1,162 @@
define([
"Game/Client/Control/Input",
"Game/Client/Control/KeyboardInput",
"Game/Client/View/DomController",
"Game/Config/Settings",
],
function (Parent, KeyboardInput, DomController, Settings) {
function KeyboardAndMouse(playerController) {
Parent.call(this);
this.x = 0;
this.y = 0;
this.playerController = playerController;
this.keyboardInit();
this.mouseInit();
}
KeyboardAndMouse.prototype = Object.create(Parent.prototype);
KeyboardAndMouse.prototype.keyboardInit = function() {
this.keyboardInput = new KeyboardInput(this.playerController);
var keys = {
w:87,
a:65,
s:83,
d:68,
f:70,
g:71,
k:75,
up: 38,
left: 37,
down: 40,
right: 39,
space: 32,
tab: 9,
plus: 187,
plusfx: 171,
minus: 189,
minusfx: 173,
zero: 48
}
this.keyboardInput.registerKey(keys.a, 'moveLeft', 'stop');
this.keyboardInput.registerKey(keys.left, 'moveLeft', 'stop');
this.keyboardInput.registerKey(keys.d, 'moveRight', 'stop');
this.keyboardInput.registerKey(keys.right, 'moveRight', 'stop');
this.keyboardInput.registerKey(keys.w, 'jump', 'jumpStop');
this.keyboardInput.registerKey(keys.up, 'jump', 'jumpStop');
this.keyboardInput.registerKey(keys.space, 'jump', 'jumpStop');
this.keyboardInput.registerKey(keys.plus, 'zoomIn');
this.keyboardInput.registerKey(keys.plusfx, 'zoomIn');
this.keyboardInput.registerKey(keys.minus, 'zoomOut');
this.keyboardInput.registerKey(keys.minusfx, 'zoomOut');
this.keyboardInput.registerKey(keys.zero, 'zoomReset');
this.keyboardInput.registerKey(keys.tab, 'showInfo', 'hideInfo');
this.keyboardInput.registerKey(keys.f, 'handActionLeft');
this.keyboardInput.registerKey(keys.g, 'handActionRight');
this.keyboardInput.registerKey(keys.k, 'suicide');
};
KeyboardAndMouse.prototype.mouseInit = function() {
var canvas = DomController.getCanvas();
var self = this;
canvas.onmousemove = function(e){
// -1 +1 +1 +1 xy scaling should
// -1 -1 +1 -1 be like this
var movementX = e.movementX ||
e.mozMovementX ||
e.webkitMovementX ||
0;
var movementY = e.movementY ||
e.mozMovementY ||
e.webkitMovementY ||
0;
self.x += movementX / Settings.VIEWPORT_SPEED_FACTOR;
if(self.x > 1) {
self.x = 1;
}
if(self.x < -1) {
self.x = -1;
};
self.y -= movementY / Settings.VIEWPORT_SPEED_FACTOR;
if(self.y > 1) {
self.y = 1;
}
if(self.y < -1) {
self.y = -1;
}
self.onXyChange(self.x, self.y);
}
canvas.onmousedown = function(e) {
var x = (((e.clientX - this.offsetLeft) / Settings.STAGE_WIDTH) * 2) - 1;
var y = (((Settings.STAGE_HEIGHT - (e.clientY - this.offsetTop)) / Settings.STAGE_HEIGHT) * 2) -1;
self.playerController.handActionRequest(x, y);
}
};
KeyboardAndMouse.prototype.update = function(e) {
/*
var movementX = e.movementX ||
e.mozMovementX ||
e.webkitMovementX ||
0;
var movementY = e.movementY ||
e.mozMovementY ||
e.webkitMovementY ||
0;
//console.log(movementX, movementY);
this.x += movementX;
if(this.x > 1) {
this.x = 1;
}
if(this.x < -1) {
this.x = -1;
};
this.y += movementY;
if(this.y > 1) {
this.y = 1;
}
if(this.y < -1) {
this.y = -1;
}
this.onXyChange(this.x, this.y);
*/
};
return KeyboardAndMouse;
});