-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopponents.js
50 lines (42 loc) · 1000 Bytes
/
opponents.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
/* opponents.js contains an array with the opponents information,
* and attaches helper functions to retreive and update opponents.
* There are also two functions to check if an opponent is visible
* on-screen.
*/
/* globals collides, area, lens
*/
// List of other players
const opponents = [];
opponents.get = function(id) {
return opponents.find(opp => opp.id == id);
}
opponents.indexOf = function(o) {
if (o) {
return opponents.map(opp => opp.id).indexOf(o.id);
} else {
return -1;
}
}
opponents.set = function(o) {
const i = opponents.indexOf(o);
if (i >= 0) {
opponents[i] = o;
} else {
opponents.push(o);
}
}
opponents.remove = function(o) {
const i = opponents.indexOf(o);
if (i >= 0) {
opponents.splice(i, 1);
}
}
function opponentsOnScreen() {
return opponents.some(opp => collides(area(opp), lens));
}
function onScreen(id) {
const opp = opponents.get(id);
if (opp) {
return collides(area(opponents.get(id)), lens);
}
}