-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.js
85 lines (76 loc) · 2.23 KB
/
player.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
class Player extends DOMObject {
points = 0;
element;
constructor(name, domId, height, width) {
super(domId, height, width);
this.name = name;
this.domId = domId;
this.height = height;
this.width = width;
this.element = document.getElementById(domId);
this.speed = 140;
}
/**
* Display info
*
*/
displayPlayerInfo() {
console.log("Player Data:\n" + "Name: " + this.name);
}
/**
* @returns name of player
*/
getName() {
console.log("name", this.name);
return this.name;
}
/**
* Change player's name
* @param {string} name name to change to
*/
setName(name) {
this.name = name;
}
/**
* Moves player with index up an this.getSpeed() amount of pixels
* @param {number} index player index: 0 for player 1 and index 1 for player 2
* @returns
*/
moveUp(index) {
let playerBrick = document.getElementsByClassName("brick")[index];
const currentPosition = parseInt(window.getComputedStyle(playerBrick).top);
if (currentPosition <= 0) {
return;
}
if (currentPosition - this.getSpeed() <= 0) {
playerBrick.style.top = 0 + "px";
return;
}
// - because in the HTML coordinate system + is downwards and - is upwards
requestAnimationFrame(() => {
playerBrick.style.top = currentPosition - this.getSpeed() + "px";
});
}
/**
* Moves the player brick element down a number of pixels
* @param {number} index 0 for player 1 and index 1 for player 2
* @returns void when movement should be cancelled
*/
moveDown(index) {
const playerBrick = document.getElementsByClassName("brick")[index];
const currentPosition = parseInt(window.getComputedStyle(playerBrick).top);
const gameCanvas = playerBrick.parentElement; // Assuming the gameCanvas is the direct parent
if (
currentPosition + playerBrick.clientHeight + this.getSpeed() >
gameCanvas.clientHeight
) {
playerBrick.style.top =
gameCanvas.clientHeight - playerBrick.clientHeight + "px";
return;
}
// + because in the HTML coordinate system + is downwards and - is upwards
requestAnimationFrame(() => {
playerBrick.style.top = currentPosition + this.getSpeed() + "px";
});
}
}