-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrols.js
343 lines (281 loc) · 8.59 KB
/
controls.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
params = params || {};
// Crypto messages
function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function getRandomText(number, random) {
// var letters = 'ϏϐϑϖϗϘϙϚϛϜϝϞϟϠϡϰϱϴϵ϶ϷϸϻϼϽϾϿἇᾯᾚᾈᾠ𐆂𐅊𐅋𐅌𐅍𐅎𐅏𐅐𐅑𐅒𐅓𐅔𐅕𐅖𐅗𐅘𐅙𐅚𐅛𐅜𐅝𐅞𐅟𐅠𐅡𐅢𐅣𐅤𐅥𐅦𐅧𐅨𐅩𐅪𐅫𐅬𐅭𐅮𐅯𐅰𐅱𐅲𐅳𐅴𐅵𐅶𐅷𐅸𐅹𐅺𐅻𐅼𐅽𐅾𐅿𐆀𐆁𐆃𐆄𐆅𐆆𐆇𐆈𐆉𐆊𐆋𐆌𐆍𐆎𐆠𝈀𝈁𝈂𝈃𝈄𝈅𝈆𝈇𝈈𝈉𝈊𝈋𝈌𝈍𝈎𝈏𝈐𝈑𝈒𝈓𝈔𝈕𝈖𝈗𝈘𝈙𝈚𝈛𝈜𝈝𝈞𝈟𝈠𝈡𝈢𝈣𝈤𝈥𝈦𝈧𝈨𝈩';
var letters = 'repoakrgeorkghoesrkrgoerger';
var text = '0x';
number = number || 64;
if (random) number = Math.floor(Math.random() * number)
for (var i = 0; i < number; i++) {
text += letters[Math.floor(Math.random() * letters.length)];
}
return text;
}
AFRAME.registerComponent('text_animation', {
init: function () {
var color = params.color || getRandomColor();
this.entities = document.querySelectorAll('a-text');
for (let i = 0; i < this.entities.length; i++) {
var el = this.entities[i]
el.setAttribute('rotation', { x: 0, y: 0, z: 90 });
el.setAttribute('color', color);
var new_value = params.letters_text || getRandomText(params.letter_number, params.letters_random)
console.log(new_value)
el.setAttribute('value', new_value);
var position = el.getAttribute('position')
var new_position = params.letter_position || 100;
position.x = Math.floor(Math.random() * new_position)
el.setAttribute('position', position);
// el.setAttribute('font', "DejaVuSans-msdf.json");
}
//console.log("init")
},
tick: function () {
//console.log("tick")
// Don't call query selector in here, query beforehand.
for (let i = 0; i < this.entities.length; i++) {
var el = this.entities[i]
/*
el.opacity = el.opacity || 1
el.setAttribute('opacity', Math.round(Math.random() * 20));
*/
function random_direction() {
return !!((Math.abs(+new Date() % 4 * -1) % 2) * -1)
}
var direction = el.getAttribute('direction')
// console.log("direction",direction)
if (!direction) {
direction = 1
if (random_direction()) direction = -1;
}
el.setAttribute('direction', direction);
var value = Math.random() * 10;
// console.log(value, !(Math.round(value * 100) % 3))
// if (/*position.y > 10 &&*/ !(Math.round(value * 1000) % 99) /*&& +new Date() % 20*/) direction *= -1;
// el.setAttribute('direction', direction);
var speed = el.getAttribute('speed')
if (!speed) speed = Math.random() / 10 + 0.1;
el.setAttribute('speed', speed);
var position = el.getAttribute('position')
if (Math.abs(position.y) > 30) {
position.y *= -1;
if (random_direction()) {
// direction = 1;
// position.y = 20;
// console.log(direction, speed, position.y, )
// el.setAttribute('direction', direction);
speed = Math.random() / 10 + 0.1;
el.setAttribute('speed', speed);
}
}
position.y += speed * direction;
el.setAttribute('position', position);
var rotation = el.getAttribute('rotation');
rotation.y += 1;
el.setAttribute('rotation', rotation);
//el.opacity -= 0.1
}
}
});
// Physics
var jumpHappens = false;
var fall_down = false;
function jump(){
if (fall_down) return
jumpHappens = true;
setTimeout(function () {
jumpHappens = false;
fall_down = true
}, 300)
var position = player.getAttribute('position');
position.y += 0.2;
// console.log(position.y)
player.setAttribute('position', position);
}
function stopJumping(){
jumpHappens = false;
fall_down = false
}
document.addEventListener('keydown', function (e) {
var player = document.querySelector('#player')
console.log(e)
var key = e.code;
// alert("Key:'"+e.keyCode+"'")
// alert("Keys:'"+JSON.stringify(navigator.getGamepads() )+"'")
if (key == "Space" || e.keyCode == 13) {
console.log(44)
jump()
}
})
document.addEventListener('keyup', function (e) {
if (e.code == "Space" || e.keyCode == 13) {
stopJumping()
}
})
AFRAME.registerComponent('player', {
init: function () {
console.log("reg", this.el)
this.el.addEventListener('physicscollided', function (e) {
console.log('Player has collided with ', e.detail.body.el);
e.detail.target.el; // Original entity (playerEl).
e.detail.body.el; // Other entity, which playerEl touched.
e.detail.contact; // Stats about the collision (CANNON.ContactEquation).
e.detail.contact.ni; // Normal (direction) of the collision (CANNON.Vec3).
});
// this.el.addEventListener('keydown', function(e) {
// console.log('key ', e);
// });
// document.querySelector('#player').addEventListener('keydown', function(e) {
// console.log('key ', e);
// });
},
tick: function () {
var player = document.querySelector('#player')
var position = player.getAttribute('position');
if (jumpHappens) {
position.y += 0.2;
}
if (position.y > 1.6 && !jumpHappens) {
position.y -= 0.35
// position.y += position.y *3;
console.log("fall down", position.y)
}
if (position.y < 1.6 && !jumpHappens) {
position.y = 1.6
// position.y += position.y *3;
}
//movement via gamepad
var direction = new THREE.Vector3();
this.el.sceneEl.camera.getWorldDirection(direction);
// console.log(direction, position)
if (forward) {
direction.multiplyScalar(forward)
position.add(direction)
}
if (backward) {
direction.multiplyScalar(backward)
position.add(direction)
}
if (backward) {
direction.multiplyScalar(backward)
position.add(direction)
}
// position.add(direction)
// console.log(position.y)
player.setAttribute('position', position);
}
})
var gamepadPressed = [];
// gamepad control
function gamepadState(){
var gamepads = navigator.getGamepads();
var gamepad = gamepads[0];
if (gamepad){
var ga = gamepad.axes.map(function(value){
return parseInt(value)
});
var gb = gamepad.buttons.map(function(value){
return value.pressed;
});
gamepadPressed = [];
forward = 0;
ga.forEach(function(value, index){
if (!!value) {
// console.log("Axes: ", index)
// alert("Axes: "+ index)
gamepadPressed.push("axes" + index);
}
})
gb.forEach(function(value, index){
if (!!value) {
// console.log("Button: ", index)
// alert("Button: "+ index)
gamepadPressed.push("button" + index);
}
controlGamepad ("button" + index,value)
})
controlGamepad
}
// setTimeout(gamepadState,0)
window.requestAnimationFrame(gamepadState)
}
function controlGamepad (el,value){
// console.log(el,value)
switch (el) {
case "button0":
case "button7":
if (value){
jump();
}
else {
stopJumping()
}
break;
case "button12":
if (value) {
forward = 0.2;
}
else {
forward = 0;
}
break;
case "button13":
if (value) {
backward = -0.2;
}
else {
backward = 0;
}
break;
case "button14":
if (value) {
left = -0.2;
}
else {
left = 0;
}
break;
case "button15":
if (value) {
right = -0.2;
}
else {
right = 0;
}
break;
default:
// forward = 0
break;
}
// window.requestAnimationFrame(controlGamepad)
}
function simulateKeyPress(code) {
var evt = document.createEvent("KeyboardEvent");
evt.initKeyboardEvent("keypress", true, true, window,
0, 0, 0, 0,
0, "e".charCodeAt(code))
var body = document.body;
var canceled = !body.dispatchEvent(evt);
if(canceled) {
console.log("canceled");
// A handler called preventDefault
} else {
console.log("ok");
// None of the handlers called preventDefault
}
}
var forward = 0;
var backward = 0;
var left = 0;
var right = 0;
// setTimeout(gamepadState,20)
window.requestAnimationFrame(gamepadState)
// window.requestAnimationFrame(controlGamepad)