-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbossPlayer.js
More file actions
67 lines (57 loc) · 1.68 KB
/
Copy pathbossPlayer.js
File metadata and controls
67 lines (57 loc) · 1.68 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
63
64
65
66
67
document.addEventListener("keydown", keyDownHandler, false);
document.addEventListener("keyup", keyUpHandler, false);
const inputKey = { left: 37, up: 38, right: 39, down: 40 };
let rightDown = false;
let leftDown = false;
let upDown = false;
let downDown = false;
function fighter(position, size) {
this.position = position;
this.size = size;
this.dirX = 0;
this.dirY = 0;
this.update = function() {
this.dirX = 0;
if (rightDown) this.dirX++;
if (leftDown) this.dirX--;
this.dirY = 0;
if (downDown) this.dirY++;
if (upDown) this.dirY--;
this.position.x += this.dirX * 10;
this.position.y += this.dirY * 10;
this.position.clamp( new vector2D(0, 0), new vector2D(window.innerWidth - this.size.x, window.innerHeight - this.size.y) );
}
this.draw = function() {
ctx = world.context;
ctx.fillStyle = "white";
ctx.fillRect(this.position.x - this.size.x / 2, this.position.y - this.size.y / 2, this.size.x, this.size.y);
}
}
function keyDownHandler(event) {
if (event.keyCode === inputKey.right) {
rightDown = true;
}
if (event.keyCode === inputKey.left) {
leftDown = true;
}
if (event.keyCode === inputKey.down) {
downDown = true;
}
if (event.keyCode === inputKey.up) {
upDown = true;
}
}
function keyUpHandler(event) {
if (event.keyCode === inputKey.right) {
rightDown = false;
}
if (event.keyCode === inputKey.left) {
leftDown = false;
}
if (event.keyCode === inputKey.down) {
downDown = false;
}
if (event.keyCode === inputKey.up) {
upDown = false;
}
}