-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.js
100 lines (89 loc) · 2.76 KB
/
input.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
const gameBoard = document.getElementById('game-board');
// Intializing inputDirection with 'Right' direction
let inputDirection = { x: 1, y: 0 };
let lastInputDirection = { x: 0, y: 0 }
// Intializing empty swipes to register touch input
let swipeStart, swipeEnd;
window.addEventListener('keydown', (e) => {
// Handling Keyboard input
switch (e.key) {
case 'ArrowUp':
case 'W':
case 'w':
if ( lastInputDirection.y !== 0) break
inputDirection = { x: 0, y: -1 }
break
case 'ArrowDown':
case 'S':
case 's':
if ( lastInputDirection.y !== 0) break
inputDirection = { x: 0, y: 1 }
break
case 'ArrowLeft':
case 'A':
case 'a':
if ( lastInputDirection.x !== 0) break
inputDirection = { x: -1, y: 0 }
break
case 'ArrowRight':
case 'D':
case 'd':
if ( lastInputDirection.x !== 0) break
inputDirection = { x: 1, y: 0 }
break
}
})
export function getInputDirection() {
// Function to get Input Direction so that snakeHead is pointing towards the direction
lastInputDirection = inputDirection;
return inputDirection;
}
function handleSwipeEvents(swipeStart, swipeEnd) {
let diffX, diffY;
// Calculating difference in position to determine swipes
diffX = Math.abs(swipeStart.x - swipeEnd.x);
diffY = Math.abs(swipeStart.y - swipeEnd.y);
if (diffX > diffY) {
// Left or Right
if (swipeStart.x > swipeEnd.x && lastInputDirection.x == 0) {
// Left Swipe
inputDirection = { x: -1, y: 0 }
} else if (lastInputDirection.x == 0) {
// Right Swipe
inputDirection = { x: 1, y: 0 }
}
} else {
// Up or Down
if (swipeStart.y > swipeEnd.y && lastInputDirection.y == 0) {
// Up Swipe
inputDirection = { x: 0, y: -1 }
} else if (lastInputDirection.y == 0) {
// Down Swipe
inputDirection = { x: 0, y: 1 }
}
}
}
export function reset() {
inputDirection = { x: 1, y: 0 };
lastInputDirection = { x: 0, y: 0 }
swipeStart = { x: null, y: null}
swipeEnd = { x: null, y: null}
}
gameBoard.addEventListener('touchstart', (e) => {
e.preventDefault();
// Getting swipeStart
swipeStart = {
x: e.touches[0].clientX,
y: e.touches[0].clientY
}
})
gameBoard.addEventListener('touchend', (e) => {
e.preventDefault();
// Getting swipeEnd
swipeEnd = {
x: e.changedTouches[0].clientX,
y: e.changedTouches[0].clientY
}
// Calling helper fn to determine which swipe is this
handleSwipeEvents(swipeStart, swipeEnd);
})