-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
111 lines (79 loc) · 2.54 KB
/
script.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
101
102
103
104
105
106
107
108
109
110
111
document.addEventListener('DOMContentLoaded', function(){
const element = document.querySelector('#test1 .entity')
// bind key handling
const handling = new HandlingRegister()
handling.bind()
const entity = new MovingEntity(element, handling)
let count = 0
setInterval(() => {
requestAnimationFrame(() => tick(count++))
}, 25)
function tick(frame){
entity.render()
}
})
class HandlingRegister {
activeKeys = []
#binds = {
'keydown': this.onKeyDown,
'keyup': this.onKeyUp,
}
bind(){
for(const type in this.#binds)
document.addEventListener(type, this.#binds[type].bind(this))
}
unbind(){
for(const type in this.#binds)
document.removeEventListener(type, this.#binds[type].bind(this))
}
onKeyDown(e){
if(!e.code || this.activeKeys.includes(e.code))
return;
this.activeKeys.push(e.code)
}
onKeyUp(e){
if(!e.code || !this.activeKeys.includes(e.code))
return;
const i = this.activeKeys.indexOf(e.code)
this.activeKeys.splice(i, 1)
}
}
class MovingEntity {
constructor(target, handling) {
this.handling = handling
this.target = target;
this.speedX = 0;
this.speedY = 0;
}
#getCurrentPos(){
return {
x: parseFloat(this.target.style.left || 0),
y: parseFloat(this.target.style.top || 0)
}
}
render(){
const currentPos = this.#getCurrentPos()
this.target.style.left = currentPos.x + this.speedX + 'px';
this.target.style.top = currentPos.y + this.speedY + 'px';
let dirX = 0,
dirY = 0;
if(this.handling.activeKeys.includes('KeyA'))
dirX += 1
if(this.handling.activeKeys.includes('KeyD'))
dirX -= 1
if(this.handling.activeKeys.includes('KeyW'))
dirY += 1
if(this.handling.activeKeys.includes('KeyS'))
dirY -= 1
this.speedX = dirX || Math.abs(this.speedX) > .5 ? (this.speedX + (dirX * 10 + this.speedX) * -.1) : 0
this.speedY = dirY || Math.abs(this.speedY) > .5 ? (this.speedY + (dirY * 10 + this.speedY) * -.1) : 0
// animations
if(this.speedX > 0){
this.target.setAttribute('data-state', 'right')
}
else if(this.speedX < 0){
this.target.setAttribute('data-state', 'left')
}
this.target.setAttribute('data-idle', !this.speedX ? 'true' : 'false')
}
}