-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview-app.js
89 lines (76 loc) · 2.03 KB
/
view-app.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
//The View
//move to view-app.js
Game.Views.App = Backbone.View.extend({
curves: Game.curves,
el:$("#players"),
events: {
'click #add-new-player':'newPlayer',
'keydown':'keypressGame',
'keyup':'keyupGame',
'click #round-score':'restartGame'
},
initialize: function(){
_.bindAll(this,'update','restart','newPlayer','updateScores');
_.each(Game.keyMaps,function(keyMap,key){
if(keyMap.inUse==false)
{
$('#new-player-controls').append(
$('<option></option>').val(key).html(keyMap.label)
);
}
});
},
restart: function(){
config.context.clearRect(0,0,config.canvasWidth,config.canvasHeight);
this.curves.each(function(curve){
curve.initialize();
})
},
newPlayer: function(){
var pos = this.$('#new-player-controls').val();
var curve = new Game.Curve({
name:this.$('#new-player-name').val(),
color:Game.keyMaps[pos].color,
upKey:Game.keyMaps[pos].up,
downKey:Game.keyMaps[pos].down,
keyLabel:Game.keyMaps[pos].label,
});
curve.view = new Game.Views.Curve({model:curve});
Game.keyMaps[pos].inUse = true;
$('#new-player-controls option[value='+pos+']').remove();
curve.playerView = new Game.Views.Players({model:curve});
$('#players').append(curve.playerView.render().el);
this.curves.add(curve);
},
updateScores: function(){
this.curves.updateScores();
if(this.curves.alive() < 2){
Game.Controller.finshRound();
rankings = this.curves.gameRankings();
_(rankings).each(function(curve){
$('#round-score-players').append('<div class="color" style="background:'+curve.get('color')+'"></div>'+curve.get('name')+'<br/>');
});
$('#round-score').fadeIn();
}
},
restartGame: function(){
$('#round-score').fadeOut();
$('#round-score-players').empty();
Game.Controller.restart();
},
update: function(){
this.curves.each(function(curve){
curve.view.render();
});
},
keypressGame: function(e){
if(e.keyCode === 80)
{
Game.Controller.pause();
}
Game.keys[e.keyCode] = true;
},
keyupGame: function(e){
Game.keys[e.keyCode] = false;
}
});