Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/garbage-collector/js/input.js
1036 views
1
/*globals define*/
2
define([
3
'utils'
4
], function( Utils ) {
5
'use strict';
6
7
function Input() {
8
this.mouse = {
9
x: 0,
10
y: 0,
11
12
down: false
13
};
14
15
this.controls = {
16
TOP: false,
17
RIGHT: false,
18
BOTTOM: false,
19
LEFT: false
20
};
21
22
this.keys = [];
23
this.game = null;
24
25
this.touches = [];
26
27
this.initialTouch = null;
28
this.touch = null;
29
30
// Pixel radius where touch difference does nothing.
31
this.deadzone = 20;
32
// Maximum radius for touch movement.
33
this.touchLimit = 100;
34
}
35
36
Input.prototype = {
37
onKeyDown: function( event ) {
38
this.keys[ event.which ] = true;
39
40
// Arrow keys.
41
if ( event.which === 37 ||
42
event.which === 38 ||
43
event.which === 39 ||
44
event.which === 40 ) {
45
event.preventDefault();
46
}
47
},
48
49
onKeyUp: function( event ) {
50
this.keys[ event.which ] = false;
51
},
52
53
/**
54
* Accounts for difference between canvas dimensions and
55
* computed CSS-specified dimensions, as well as any canvas offsets.
56
*/
57
normalizeTouch: function( event ) {
58
var x = event.pageX,
59
y = event.pageY;
60
61
if ( this.game ) {
62
var canvasWidth = this.game.canvas.width,
63
canvasHeight = this.game.canvas.height;
64
65
var computedStyle = window.getComputedStyle( this.game.canvas );
66
67
var computedWidth = parseInt( computedStyle.width, 10 ),
68
computedHeight = parseInt( computedStyle.height, 10 );
69
70
var xRatio = canvasWidth / computedWidth,
71
yRatio = canvasHeight / computedHeight;
72
73
x -= this.game.canvas.offsetLeft;
74
y -= this.game.canvas.offsetTop;
75
76
x *= xRatio;
77
y *= yRatio;
78
}
79
80
return {
81
pageX: x,
82
pageY: y
83
};
84
},
85
86
onTouchStart: function( event ) {
87
this.touches = [].slice.call( event.touches );
88
if ( !this.initialTouch ) {
89
this.initialTouch = this.normalizeTouch( this.touches[0] );
90
}
91
},
92
93
onTouchMove: function( event ) {
94
event.preventDefault();
95
this.touches = [].slice.call( event.touches );
96
this.touch = this.normalizeTouch( this.touches[0] );
97
},
98
99
onTouchEnd: function( event ) {
100
this.touches = [].slice.call( event.touches );
101
if ( !this.touches.length ) {
102
this.initialTouch = null;
103
this.touch = null;
104
}
105
},
106
107
update: function( dt ) {
108
var controls = this.controls;
109
110
Object.keys( controls ).forEach(function( control ) {
111
controls[ control ] = false;
112
});
113
114
// Keyboard update.
115
// Arrow keys.
116
if ( this.keys[ 37 ] ) { controls.LEFT = true; }
117
if ( this.keys[ 38 ] ) { controls.TOP = true; }
118
if ( this.keys[ 39 ] ) { controls.RIGHT = true; }
119
if ( this.keys[ 40 ] ) { controls.BOTTOM = true; }
120
121
// WASD.
122
if ( this.keys[ 65 ] ) { controls.LEFT = true; }
123
if ( this.keys[ 87 ] ) { controls.TOP = true; }
124
if ( this.keys[ 68 ] ) { controls.RIGHT = true; }
125
if ( this.keys[ 83 ] ) { controls.BOTTOM = true; }
126
127
this.updatePlayer( dt );
128
},
129
130
updatePlayer: function( dt ) {
131
if ( this.game ) {
132
var controls = this.game.input.controls;
133
134
var ax = 0,
135
ay = 0;
136
137
if ( controls.LEFT ) { ax -= 800; }
138
if ( controls.RIGHT ) { ax += 800; }
139
if ( controls.TOP ) { ay -= 800; }
140
if ( controls.BOTTOM ) { ay += 800; }
141
142
if ( this.touch ) {
143
var x = this.touch.pageX,
144
y = this.touch.pageY;
145
146
var xi = this.initialTouch.pageX,
147
yi = this.initialTouch.pageY;
148
149
var dx = x - xi,
150
dy = y - yi;
151
152
var dz = this.deadzone;
153
154
var angle;
155
// Parameters of touch extents.
156
var xt, yt;
157
158
if ( Utils.distanceSquared( x, y, xi, yi ) > dz * dz ) {
159
angle = Math.atan2( dy, dx );
160
161
// Subtract deadzone.
162
dx -= dz * Math.cos( angle );
163
dy -= dz * Math.sin( angle );
164
165
// Get parameter up to touch limit.
166
xt = Utils.inverseLerp( Math.abs( dx ), 0, this.touchLimit );
167
yt = Utils.inverseLerp( Math.abs( dy ), 0, this.touchLimit );
168
169
xt = Utils.clamp( xt, 0, 1 );
170
yt = Utils.clamp( yt, 0, 1 );
171
172
// Determine sign.
173
xt *= ( dx < 0 ? -1 : 1 );
174
yt *= ( dy < 0 ? -1 : 1 );
175
176
ax = xt * 800;
177
ay = yt * 800;
178
}
179
}
180
181
// Move along camera direction.
182
var camera = this.game.camera;
183
if ( camera.angle ) {
184
var cos = Math.cos( -camera.angle ),
185
sin = Math.sin( -camera.angle );
186
187
var rax = cos * ax - sin * ay,
188
ray = sin * ax + cos * ay;
189
190
ax = rax;
191
ay = ray;
192
}
193
194
this.game.player.accelerate( ax * dt, ay * dt );
195
}
196
},
197
198
draw: function( ctx ) {
199
if ( this.game && this.initialTouch ) {
200
var x = this.initialTouch.pageX,
201
y = this.initialTouch.pageY;
202
203
ctx.lineWidth = 8;
204
ctx.strokeStyle = 'rgba(0, 0, 0, 0.2)';
205
206
ctx.beginPath();
207
ctx.arc( x, y, this.deadzone, 0, Utils.PI2 );
208
209
ctx.moveTo( x + this.touchLimit, y );
210
ctx.arc( x, y, this.touchLimit, 0, Utils.PI2 );
211
212
ctx.moveTo( x + this.deadzone + this.touchLimit, y );
213
ctx.arc( x, y, this.deadzone + this.touchLimit, 0, Utils.PI2 );
214
ctx.stroke();
215
}
216
}
217
};
218
219
return Input;
220
});
221
222