-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
201 lines (175 loc) · 4.42 KB
/
index.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
const canvas = document.querySelector("canvas");
const c = canvas.getContext("2d")
canvas.width = 1024
canvas.height = 576
c.fillRect(0, 0, canvas.width, canvas.height)
const gravity = 0.7
class Sprite {
constructor({ position, velocity, color = 'red' }) {
this.position = position
this.velocity = velocity
this.width = 50
this.height = 150
this.lastkey
this.attackBox = {
position: {
x: this.position.x,
y: this.position.y
},
width: 100,
height: 50
}
this.color = color
this.isAttacking
}
draw() {
c.fillStyle= this.color
c.fillRect(this.position.x, this.position.y, this.width, this.height)
// attack box
//if (this.isAttacking) {
c.fillStyle = 'green'
c.fillRect(this.attackBox.position.x,
this.attackBox.position.y,
this.attackBox.width,
this.attackBox.height
)
// }
}
update() {
this.draw()
this.attackBox.position.x =this.position.x
this.attackBox.position.y =this.position.y
this.position.x += this.velocity.x
this.position.y += this.velocity.y
if (this.position.y + this.height + this.velocity.y >= canvas.height) {
this.velocity.y = 0
} else this.velocity.y += gravity
}
attack() {
this.isAttacking = true
setTimeout(() => {
this.isAttacking = false
}, 100)
}
}
//player.draw()
const player = new Sprite({
position: {
x: 0,
y: 0
},
velocity: {
x: 0,
y: 0
}
})
//enemy.draw()
const enemy = new Sprite({
position: {
x: 400,
y: 100
},
velocity: {
x: 0,
y: 0
},
color: 'blue'
})
console.log(player)
const keys = {
//player keys
a: {
pressed: false
},
d: {
pressed: false
},
// enemy keys
ArrowRight: {
pressed: false
},
ArrowLeft: {
pressed: false
}
}
function animate() {
window.requestAnimationFrame(animate)
c.fillStyle = 'black'
c.fillRect(0, 0, canvas.width, canvas.height)
player.update()
enemy.update()
//console.log("go")
player.velocity.x = 0
enemy.velocity.x = 0
// player movement
if (keys.a.pressed && player.lastkey === 'a') {
player.velocity.x = -5
} else if (keys.d.pressed && player.lastkey === 'd') {
player.velocity.x = 5
}
// enemy movement
if (keys.ArrowLeft.pressed && enemy.lastkey === 'ArrowLeft') {
enemy.velocity.x = -5
} else if (keys.ArrowRight.pressed && enemy.lastkey === 'ArrowRight') {
enemy.velocity.x = 5
}
// detect for collision
if (player.attackBox.position.x + player.attackBox.width >= enemy.position.x && player.attackBox.position.x <= enemy.position.x + enemy.width && player.attackBox.position.y + player.attackBox.height >= enemy.position.y && player.attackBox.position.y <= enemy.position.y + enemy.height && player.isAttacking) {
console.log('go');
}
}
animate()
window.addEventListener('keydown', (event) => {
console.log(event.key)
switch (event.key) {
// player keys
case 'd':
keys.d.pressed = true
player.lastkey ='d'
break
case 'a':
keys.a.pressed = true
player.lastkey = 'a'
break
case 'w':
player.velocity.y = -20
break
case ' ':
player.attack()
break
// enemy keys
case 'ArrowRight':
keys.ArrowRight.pressed = true
enemy.lastkey = 'ArrowRight'
break
case 'ArrowLeft':
keys.ArrowLeft.pressed = true
enemy.lastkey = 'ArrowLeft'
break
case 'ArrowUp':
enemy.velocity.y = -20
break
}
console.log(event.key)
})
window.addEventListener('keyup', (event) => {
// player keys
switch (event.key) {
case 'd':
keys.d.pressed = false
break
case 'a':
keys.a.pressed = false
break
}
// enemy keys
switch (event.key) {
case 'ArrowRight':
keys.ArrowRight.pressed = false
break
case 'ArrowLeft':
keys.ArrowLeft.pressed = false
break
}
console.log(event.key)
})