/
tquery.playerinput.keyboard.js
77 lines (70 loc) · 2.53 KB
/
tquery.playerinput.keyboard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//////////////////////////////////////////////////////////////////////////////////
// Constructor //
//////////////////////////////////////////////////////////////////////////////////
tQuery.PlayerInput.registerStatic('Keyboard', function(opts){
// handle arguments polymorphism
if( opts instanceof tQuery.PlayerInput ) opts.playerInput = opts;
// handle arguments default value
opts = tQuery.extend(opts, {
world : tQuery.world
});
// argments sanity check
console.assert(opts.playerInput instanceof tQuery.PlayerInput)
// your code goes here
var input = opts.playerInput;
this._world = opts.world;
// init input if needed
var actions = this._actions();
if( input.right=== undefined ) input.right = actions.right
if( input.up === undefined ) input.up = actions.up
if( input.left === undefined ) input.left = actions.left
if( input.down === undefined ) input.down = actions.down
// init callback to update inputPlayer
this._callback = this._world.loop().hook(function(delta, now){
var actions = this._actions();
if( actions.right !== input.right ){
input.right = actions.right;
input.dispatchEvent('change', 'right', input.right)
}
if( actions.up !== input.up ){
input.up = actions.up;
input.dispatchEvent('change', 'up', input.up)
}
if( actions.left !== input.left ){
input.left = actions.left;
input.dispatchEvent('change', 'left', input.left)
}
if( actions.down !== input.down ){
input.down = actions.down;
input.dispatchEvent('change', 'down', input.down)
}
}.bind(this));
});
/**
* explicit destructor
*/
tQuery.PlayerInput.Keyboard.prototype.destroy = function(){
this._world.loop().unhook(this._callback)
this._joystick.destroy()
};
//////////////////////////////////////////////////////////////////////////////////
// comment //
//////////////////////////////////////////////////////////////////////////////////
tQuery.PlayerInput.registerStatic('createKeyboard', function(opts){
return new tQuery.PlayerInput.Keyboard(opts)
});
/**
* return current actions based on keyboard.pressed
* compatible with arrow, asdw
* @return {[type]} [description]
*/
tQuery.PlayerInput.Keyboard.prototype._actions = function() {
var keyboard = tQuery.keyboard();
var actions = {
left : keyboard.pressed("left") || keyboard.pressed("a") || keyboard.pressed("q"),
right : keyboard.pressed("right") || keyboard.pressed("d"),
up : keyboard.pressed("up") || keyboard.pressed("w") || keyboard.pressed("z"),
down : keyboard.pressed("down") || keyboard.pressed("s"),
};
return actions
};