-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathactivePointer.js
More file actions
107 lines (93 loc) · 2.85 KB
/
Copy pathactivePointer.js
File metadata and controls
107 lines (93 loc) · 2.85 KB
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
import { BLAST_HOLD_THRESHOLD, SLINGSHOT_MOVE_THRESHOLD } from "./constants.js";
import { drawHoldBlastPreview, makeHoldBlast } from "./holdBlast.js";
import { drawSlingshotPreview, makeSlingshot } from "./slingshot.js";
export const makeActivePointer = (
canvasManager,
audioManager,
scoreStore,
tutorialManager,
pointerId,
startPosition,
ballAtPoint,
onTrigger,
defaultPointerAction
) => {
const pointerStart = Date.now();
let currentPosition = { ...startPosition };
let hitSlingshotDistanceThresholdInTime = false;
const getDistance = () =>
Math.sqrt(
(startPosition.x - currentPosition.x) ** 2 +
(startPosition.y - currentPosition.y) ** 2
);
const setPosition = ({ x, y }) => {
currentPosition = { x, y };
if (
Date.now() - pointerStart <= BLAST_HOLD_THRESHOLD &&
getDistance() > SLINGSHOT_MOVE_THRESHOLD
) {
hitSlingshotDistanceThresholdInTime = true;
}
};
const isSlingshot = () => hitSlingshotDistanceThresholdInTime;
const isHoldBlast = () =>
Date.now() - pointerStart > BLAST_HOLD_THRESHOLD &&
!hitSlingshotDistanceThresholdInTime;
const slingshotAllowed = () =>
tutorialManager.isTutorialComplete() ||
(!tutorialManager.isTutorialComplete() &&
tutorialManager.canMakeSlingshot());
const blastAllowed = () =>
tutorialManager.isTutorialComplete() ||
(!tutorialManager.isTutorialComplete() && tutorialManager.canMakeBlast());
const tapAllowed = () =>
tutorialManager.isTutorialComplete() ||
(!tutorialManager.isTutorialComplete() && tutorialManager.canMakeTap());
const trigger = () => {
if (isSlingshot() && slingshotAllowed()) {
onTrigger(
makeSlingshot(canvasManager, scoreStore, startPosition, currentPosition)
);
audioManager.playMiss();
} else if (isHoldBlast() && blastAllowed()) {
onTrigger(
makeHoldBlast(
canvasManager,
scoreStore,
startPosition,
Date.now() - pointerStart
)
);
audioManager.playImpact();
} else if (tapAllowed()) {
defaultPointerAction(currentPosition, ballAtPoint);
}
};
const draw = () => {
if (isSlingshot() && slingshotAllowed()) {
drawSlingshotPreview(
canvasManager,
startPosition,
currentPosition,
!tutorialManager.isTutorialComplete()
);
if (!tutorialManager.isTutorialComplete()) {
tutorialManager.previewingSlingshot();
}
} else if (isHoldBlast() && blastAllowed()) {
drawHoldBlastPreview(canvasManager, startPosition, pointerStart);
if (!tutorialManager.isTutorialComplete()) {
tutorialManager.previewingBlast();
}
}
};
return {
getId: () => pointerId,
setPosition,
trigger,
draw,
getDuration: () => Date.now() - pointerStart,
isSlingshot,
isHoldBlast,
};
};