-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.js
32 lines (29 loc) · 1.11 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
//PROJECT OF AHMAD NAEEM CHEEMA
let inputDirection = { x: 0, y: 0}
let lastInputDirection = { x: 0, y: 0}
//Event Listener to Get Key Input from Player
window.addEventListener('keydown', e => {
switch(e.key) {
case 'ArrowUp':
if (lastInputDirection.y !== 0) break //To Make Sure It Doesn't The Opposite Direction
inputDirection = { x: 0, y: -1}
break
case 'ArrowDown':
if (lastInputDirection.y !== 0) break //To Make Sure It Doesn't The Opposite Direction
inputDirection = { x: 0, y: 1}
break
case 'ArrowLeft':
if (lastInputDirection.x !== 0) break //To Make Sure It Doesn't The Opposite Direction
inputDirection = { x: -1, y: 0}
break
case 'ArrowRight':
if (lastInputDirection.x !== 0) break //To Make Sure It Doesn't The Opposite Direction
inputDirection = { x: 1, y: 0}
break
}
})
//Function to Get Input Direction
export function getInputDirection(){
lastInputDirection = inputDirection
return inputDirection
}