-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.js
83 lines (75 loc) · 3.44 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
class Input {
walk = false
turnLeft = false
turnRight = false
mousePressed = false
firstX = 0
firstY = 0
x = 0
y = 0
dir = 0
constructor(domElement) {
this.guiCanvas = document.createElement('canvas')
this.guiCanvas.setAttribute('id', 'gui')
this.guiCanvas.setAttribute('style', `position: fixed; width: 100%; height: 100%; top: 0px; left: 0px; pointer-events: none; z-index: 2;`)
document.body.append(this.guiCanvas)
this.gui = this.guiCanvas.getContext('2d')
this.guiCanvas.width = innerWidth * devicePixelRatio
this.guiCanvas.height = innerHeight * devicePixelRatio
this.gui.scale(devicePixelRatio, devicePixelRatio)
this.gui.lineWidth = 3
addEventListener('resize' , () => {
this.guiCanvas.width = innerWidth * devicePixelRatio
this.guiCanvas.height = innerHeight * devicePixelRatio
this.gui.scale(devicePixelRatio, devicePixelRatio)
this.gui.lineWidth = 3
})
addEventListener('keydown', e => {
if(e.repeat) return
if(e.code == 'ArrowLeft' || e.code == 'KeyA') this.turnLeft = true
if(e.code == 'ArrowRight' || e.code == 'KeyD') this.turnRight = true
if(e.code == 'ArrowUp' || e.code == 'KeyW') this.walk = true
})
addEventListener('keyup', e => {
if(e.code == 'ArrowLeft' || e.code == 'KeyA') this.turnLeft = false
if(e.code == 'ArrowRight' || e.code == 'KeyD') this.turnRight = false
if(e.code == 'ArrowUp' || e.code == 'KeyW') this.walk = false
})
for(let event of ['touchstart', 'mousedown']) domElement.addEventListener(event, e => {
if(event.startsWith('mouse') && e.button != 0) return
this.mousePressed = true
this.walk = true
this.firstX = (e.type.startsWith('touch'))? e.touches[0].clientX : e.clientX
this.firstY = (e.type.startsWith('touch'))? e.touches[0].clientY : e.clientY
this.x = this.firstX
this.y = this.firstY
})
for(let event of ['touchmove', 'mousemove']) addEventListener(event, e => {
if(!this.mousePressed) return
this.x = (e.type.startsWith('touch'))? e.touches[0].clientX : e.clientX
this.y = (e.type.startsWith('touch'))? e.touches[0].clientY : e.clientY
this.dir = Math.atan2(-(this.y - this.firstY), this.x - this.firstX)
})
for(let event of ['touchend', 'mouseup']) addEventListener(event, e => {
this.mousePressed = false
this.walk = false
this.turnLeft = false
this.turnRight = false
})
}
drawUI() {
this.gui.clearRect(0, 0, this.guiCanvas.width, this.guiCanvas.height)
if(this.mousePressed) {
this.gui.beginPath()
this.gui.strokeStyle = `rgba(255, 255, 255, 0.3)`
this.gui.arc(this.firstX, this.firstY, 50, 0, 6.28318)
this.gui.stroke()
this.gui.beginPath()
this.gui.fillStyle = `rgba(255, 255, 255, 0.8)`
let dir = Math.atan2(this.y - this.firstY, this.x - this.firstX)
let dist = Math.min(Math.sqrt(Math.pow(this.firstX - this.x, 2) + Math.pow(this.firstY - this.y, 2)), 50)
this.gui.arc(this.firstX + Math.cos(dir) * dist, this.firstY + Math.sin(dir) * dist, 25, 0, 6.28318)
this.gui.fill()
}
}
}