-
Notifications
You must be signed in to change notification settings - Fork 0
/
bBall.js
82 lines (71 loc) · 2.15 KB
/
bBall.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
function simulateMouseEvent(target, options, type) {
const event = target.ownerDocument.createEvent('MouseEvents');
const opts = {
type: type,
canBubble: true,
cancelable: true,
view: target.ownerDocument.defaultView,
detail: 1,
screenX: 0,
screenY: 0,
clientX: 0,
clientY: 0,
ctrlKey: false,
altKey: false,
shiftKey: false,
metaKey: false,
button: 0,
relatedTarget: null,
...options
};
event.initMouseEvent(
opts.type,
opts.canBubble,
opts.cancelable,
opts.view,
opts.detail,
opts.screenX,
opts.screenY,
opts.clientX,
opts.clientY,
opts.ctrlKey,
opts.altKey,
opts.shiftKey,
opts.metaKey,
opts.button,
opts.relatedTarget
);
target.dispatchEvent(event);
};
/**
* Shoots a ball
*
*/
function bBall() {
const canvas = document.querySelector('canvas');
const x = window.innerWidth / 2;
const y = window.innerHeight;
simulateMouseEvent(canvas, { clientX: x, clientY: y * 0.68 }, 'mousedown');
simulateMouseEvent(canvas, { clientX: x, clientY: y * 0.43 }, 'mouseup');
}
/**
* Creates a sequence of ball shoots
*
* @param {int} mainIntervalTime Time in miliseconds for the main interval
* @param {int} ballsNumber Number of balls to shoot each interval
* @param {int} timeBetweenBalls time in miliseconds between shootings
* @param {int} maxShoots max ammount of shoots to make before killing the interval
*/
function bBallSequence(mainIntervalTime, ballsNumber, timeBetweenBalls, maxShoots){
let shootCounter = 0;
const sequenceInterval = setInterval(() => {
for (let i = 0; i < ballsNumber; i++) {
setTimeout(() => {
shootCounter ++;
shootCounter === maxShoots ? clearInterval(sequenceInterval) : bBall();
}, timeBetweenBalls * i);
}
}, mainIntervalTime);
}
// Init ball sequence
bBallSequence(1750, 3, 200, 20);