Permalink
Browse files

Merge pull request #2 from skinofstars/master

RESTART!!
  • Loading branch information...
2 parents 644e5f5 + a7b9820 commit 137af5aff0a86dfb64c960ff2fb1d6f3ddc0fbca @benfoxall committed Feb 28, 2012
Showing with 60 additions and 10 deletions.
  1. +15 −1 public/client.js
  2. +32 −7 public/controller.js
  3. +13 −2 public/player.html
View
16 public/client.js
@@ -78,15 +78,29 @@
this.els.right.addEventListener('click', function(){
player.emit('right');
})
+
+ this.els.restart.addEventListener('click', function(){
+ player.emit('restart');
+ })
// player is assigned an id
this.on('id', function(id){
//color the buttons
// console.log('-----')
this.els.left.style.color = fills[id%fills.length]
this.els.right.style.color = fills[id%fills.length]
+
+ });
+
+ this.on('gameover', function(){
+ // show restart button
+ this.els.restart.style.display = 'block';
});
+ this.on('removerestart', function(){
+ // restart pressed, remove button
+ this.els.restart.style.display = 'none';
+ });
this.emit('player');
@@ -125,4 +139,4 @@
return document.getElementById(id);
}
-})(window, document, io);
+})(window, document, io);
View
39 public/controller.js
@@ -33,6 +33,9 @@ var controller = function(io){
var arenas = [];
var players = [];
+ var gameover = function(){
+
+ }
/*
Arena & Controller logic
@@ -83,6 +86,7 @@ var controller = function(io){
player.posArray = [0,y];
player.nextDirection = 'right';
player.arena = startArena;// XXX temporary
+ player.emit('removerestart');
});
}
@@ -100,8 +104,17 @@ var controller = function(io){
}
});
+ // XXX err, are we removing players? this may be duff
+ if(this.players.length==0)
+ {
+ // needs to restart
+ each(this.players, function(player){
+ player.restart();
+ });
+ }
};
+
// When a link between arenas is requested - we set this variable
var linkArena;
var linkEdge;
@@ -214,7 +227,7 @@ var controller = function(io){
// [[x,y,playerid],[x,y,playerid]]
playerCollision = checkCoordinateInArray(head, this.usedpoints);
if(playerCollision){
- console.log('BOOM!');
+ return true;
}
}
@@ -246,6 +259,12 @@ var controller = function(io){
}[this.nextDirection];
});
+ this.on('restart', function(){
+ console.log('>.< Player '+this.id+' says RESTART!!');
+
+ game.start(); // best way?
+ });
+
};
Player.prototype.advance = function(){
@@ -260,8 +279,8 @@ var controller = function(io){
if(!this.arena){
// we arent on an arena anymore
- console.log("KABOOM");
-
+ console.log("KABOOM player "+this.id);
+ this.restart();
} else {
this.checkCollision();
// store the point for collisions
@@ -271,15 +290,21 @@ var controller = function(io){
}
-
-
+ }
+
+ Player.prototype.restart = function(){
+ //console.log("KABOOM");
+ this.emit('gameover', true);
}
Player.prototype.checkCollision = function(){
// actually, we just ask the arena for now
- this.arena.checkCollision([this.x,this.y,this.id]);
-
+ if(this.arena.checkCollision([this.x,this.y,this.id]) === true)
+ {
+ console.log('BOOM!');
+ this.restart();
+ }
}
View
15 public/player.html
@@ -18,19 +18,30 @@
float:left;
font-size:4em;
}
+ button#restart{
+ width:100%;
+ position:absolute;
+ top:0;
+ left:0;
+ font-size:4em;
+ display:none;
+ xz-index: -1;
+ }
</style>
</head>
<body>
<button id="left">&larr;</button>
<button id="right">&rarr;</button>
+ <button id="restart">RESTART!!</button>
<script src="/socket.io/socket.io.js"></script>
<script src="client.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
new PlayerView({
left:el('left'),
- right:el('right')
+ right:el('right'),
+ restart:el('restart')
})
</script>
</body>
-</html>
+</html>

0 comments on commit 137af5a

Please sign in to comment.