-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathengine.js
124 lines (106 loc) · 2.76 KB
/
engine.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
// La taille du plateau en pixels
const GAME_SIZE = 550;
// L'objet contenant les joueurs
const players = {};
// L'objet qui représente l'étoile
const star = {height: 20, width: 20};
// La liste des noms de joueurs
const namePlayers = require('./public/name-players.json');
/**
* Cette fonction vérifie que la position donnée est bien dans le canvas
* @param pos
* @returns {boolean}
*/
function isInBounds(pos) {
return pos >= 0 && pos <= GAME_SIZE - 30;
}
/**
* Cette fonction limite la vélocité à 5
* @param pos
* @returns {boolean}
*/
function isAcceptableVel(vel) {
return vel >= -5 && vel <= 5;
}
/**
* Cette fonction permet de faire bouger un joueur
* @param pos
* @returns {boolean}
*/
function movePlayer(id, x, y) {
const newX = players[id].x + x;
const newY = players[id].y + y;
if (isInBounds(newX) && isInBounds(newY)) {
players[id].x = newX;
players[id].y = newY;
} else {
players[id].velX = 0;
players[id].velY = 0;
}
}
/**
* Cette fonction permet de faire accélerer un joueur
* @param pos
* @returns {boolean}
*/
function accelPlayer(id, x, y) {
const newVelX = players[id].velX + x;
const newVelY = players[id].velY + y;
if (isAcceptableVel(newVelX) && isAcceptableVel(newVelY)) {
players[id].velX = newVelX;
players[id].velY = newVelY;
}
}
/**
* Cette fonction transforme une chaîne de caractère en une couleur hexadécimale
* @param pos
* @returns {boolean}
*/
function stringToColour(str) {
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
let colour = '#';
for (let i = 0; i < 3; i++) {
const value = (hash >> (i * 8)) & 0xFF;
colour += ('00' + value.toString(16)).substr(-2);
}
return colour;
}
/**
* Cette fonction permet de générer l'étoile
*/
function generateStar() {
star.x = Math.floor(Math.random() * (GAME_SIZE-star.width));
star.y = Math.floor(Math.random() * (GAME_SIZE-star.height));
console.log("generate new star ", star);
}
/**
* Cette fonction permet de récupérer un nom pour le joueur
* @returns {string}
*/
function nameOfPlayer() {
return namePlayers[Math.floor(Math.random() * namePlayers.length)];
}
/**
* Cette fonction indique si le joueur est en collision avec l'étoile
* @param player
* @returns {boolean}
*/
function checkCollisionWithStar(player) {
return player.x < star.x + star.width &&
player.x + player.width > star.x &&
player.y < star.y + star.height &&
player.height + player.y > star.y;
}
module.exports = {
players: players,
stringToColour: stringToColour,
movePlayer: movePlayer,
accelPlayer: accelPlayer,
nameOfPlayer: nameOfPlayer,
star: star,
generateStar: generateStar,
checkCollisionWithStar: checkCollisionWithStar
};