-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinput.js
More file actions
62 lines (54 loc) · 2.32 KB
/
input.js
File metadata and controls
62 lines (54 loc) · 2.32 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
function doTouchStart(event) {
if(event.touches[0].clientX - CANVAS.offsetLeft >= CANVAS_WIDTH - 150 && event.touches[0].clientY - CANVAS.offsetTop >= (CANVAS_HEIGHT / 2 + 75) && event.touches[0].clientY - CANVAS.offsetTop <= (CANVAS_HEIGHT / 2 + 150)) {
firePlayer();
}
// Right
if(event.touches[0].clientX - CANVAS.offsetLeft <= 235 && event.touches[0].clientX - CANVAS.offsetLeft >= 135 &&
event.touches[0].clientY - CANVAS.offsetTop >= (CANVAS_HEIGHT / 2 + 75) && event.touches[0].clientY - CANVAS.offsetTop <= (CANVAS_HEIGHT / 2 + 150)) {
movePlayer(2, 0);
}
// Left
if(event.touches[0].clientX - CANVAS.offsetLeft <= 135 && event.touches[0].clientX - CANVAS.offsetLeft >= 20 &&
event.touches[0].clientY - CANVAS.offsetTop >= (CANVAS_HEIGHT / 2 + 75) && event.touches[0].clientY - CANVAS.offsetTop <= (CANVAS_HEIGHT / 2 + 150)) {
movePlayer(-2, 0);
}
// Up
if(event.touches[0].clientX - CANVAS.offsetLeft <= 160 && event.touches[0].clientX - CANVAS.offsetLeft >= 95 &&
event.touches[0].clientY - CANVAS.offsetTop >= (CANVAS_HEIGHT / 2 + 10) && event.touches[0].clientY - CANVAS.offsetTop <= (CANVAS_HEIGHT / 2 + 75)) {
movePlayer(0, -2);
}
// Down
if(event.touches[0].clientX - CANVAS.offsetLeft <= 160 && event.touches[0].clientX - CANVAS.offsetLeft >= 95 &&
event.touches[0].clientY - CANVAS.offsetTop >= (CANVAS_HEIGHT / 2 + 10 + 145) && event.touches[0].clientY - CANVAS.offsetTop <= (CANVAS_HEIGHT - 20)) {
movePlayer(0, 2);
}
}
function doMouseDown(event) {
if(player.bullets.length < 5) {
firePlayer();
playSound(AudioAssets.bitPopSound);
}
}
function doKeyDown(event) {
if(event.keyCode === 87) {
// W - Up
movePlayer(0, -2);
}
else if(event.keyCode === 83) {
// S - Down
movePlayer(0, 2);
}
else if(event.keyCode === 65) {
// A - Left
movePlayer(-2, 0);
}
else if(event.keyCode === 68) {
// D - Right
movePlayer(2, 0);
}
}
function initInputs() {
CANVAS.addEventListener("touchstart", doTouchStart, {passive: true});
window.addEventListener("mousedown", doMouseDown, false);
window.addEventListener("keydown", doKeyDown, false);
}