-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.js
131 lines (112 loc) · 3.73 KB
/
ui.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
/***
* Vex webgl port
* Autor: Bernard Bekker
* Year: 2013
*
* This code is shared under the:
* Creative Commons Attribution-NonCommercial 4.0 International Public License
*
* http://creativecommons.org/licenses/by-nc/4.0/deed.en_US
*
*/
UI = {};
UI.init = function(){
//setup UI elements
UI.Scoreboard.update();
UI.PlayerList.reset();
UI.PlayerList.init();
vex.players.forEach(function(p){
UI.PlayerList.addPlayer(p);
});
window.setInterval(UI.PlayerList.update,250);
}
//Called when the game is finished
function gameFinished(){
UI.showFinishedScreen();
}
UI.showFinishedScreen = function(){
var sorted = vex.players.sort(function(p,p2){return p2.score - p.score});
document.getElementById("winning_player").textContent = sorted[0].name;
var r = document.getElementById("results");
var verhaal = "";
sorted.forEach(function(p){
verhaal += p.name+": "+ p.score+"</br>";
});
r.innerHTML = verhaal;
document.getElementById("resultcontainer").setAttribute("style", "");
}
UI.Scoreboard = {
update : function(){
UI.Scoreboard.keepUpdating = 1;
var scoreboard = document.getElementById("scoreboard");
for(var iplayer in vex.players){
var player = vex.players[iplayer];
if(!UI.Scoreboard.scores[iplayer] || UI.Scoreboard.scores[iplayer] != player.score){
var elem = document.getElementById("player_"+player.id);
if(elem){
elem.textContent = player.name+": "+player.score;
}else{
var newElem = document.createElement("div");
newElem.setAttribute("id","player_"+player.id);
newElem.textContent = player.name+": "+player.score;
scoreboard.appendChild(newElem);
}
UI.Scoreboard.scores[iplayer] = player.score;
}
}
if(UI.Scoreboard.keepUpdating == 1)
setTimeout(UI.Scoreboard.update,100);
},
stopUpdate : function(){
this.keepUpdating = 0;
},
scores : [],
lastUpdate : 0,
keepUpdating: 1
}
UI.PlayerList = {
init : function(){
this.holderElem = document.getElementById("player_list");
this.protoElem = document.getElementById("proto_player_list_item");
},
addPlayer : function(player){
var elem = this.protoElem.cloneNode(true);
elem.setAttribute("id","list_player"+player.id);
elem.children[0].textContent = player.name;
this.playerElems[player.id] = elem;
this.elemVals[player.id] = {};
this.holderElem.appendChild(elem);
},
update : function(){
for(var pId in UI.PlayerList.playerElems){
var player = vex.players[pId];
var elem = UI.PlayerList.playerElems[pId];
var vals = UI.PlayerList.elemVals[pId];
if(player.ship && vals.hp != player.ship.hp){
elem.children[3].children[0].setAttribute("style","height: "+player.ship.hp+"%");
vals.hp = player.ship.hp;
}
if(!player.ship && vals.hp != 0){
elem.children[3].children[0].setAttribute("style","height: "+0+"%");
vals.hp = 0;
}
if(vals.score != player.score){
elem.children[2].textContent = player.score;
vals.score = player.score;
}
}
},
stopUpdate : function(){
this.keepUpdating = 0;
},
reset : function(){
this.holderElem.innerHTML = "";
this.holderElem = {};
this.playerElems = [];
this.elemVals = [];
},
keepUpdating : 1,
holderElem : {},
playerElems : [],
elemVals : []
}