Permalink
Browse files

add scores and all the game mechanics and screens

  • Loading branch information...
1 parent a90469d commit d2862962edf4fb3aa5345c8ca4583cbf46980619 @jlongster committed Dec 1, 2012
View
@@ -204,6 +204,7 @@ function handleHit(shooter, target) {
function handleDeath(killerUser, killedUser) {
var spawnPoint = Math.floor(Math.random() * 4);
killedUser.entity.restart(spawnPoint);
+ killedUser.numDeaths++;
var obj = {
type: p.cmdPacket.typeId,
@@ -216,6 +217,7 @@ function handleDeath(killerUser, killedUser) {
obj.from = killedUser.id;
killedUser.room.broadcast(killedUser, p.cmdPacket(obj));
+ killerUser.numKills++;
}
function newUserid() {
@@ -246,7 +248,9 @@ function getUser(id) {
function createUser(stream, room) {
var user = {
stream: stream,
- id: newUserid()
+ id: newUserid(),
+ numDeaths: 0,
+ numKills: 0
};
if(!user.id) {
@@ -338,6 +342,33 @@ function createUser(stream, room) {
message: user.name + ' has joined'
}));
+ if(room.count() > 1) {
+ if(!room.startTime()) {
+ room.start();
+ room.broadcast(null, p.gameStartPacket({
+ type: p.gameStartPacket.typeId,
+ from: 0,
+ started: room.startTime()
+ }));
+
+ room.onEnd(function() {
+ room.broadcast(null, p.gameOverPacket({
+ type: p.gameOverPacket.typeId,
+ from: 0,
+ scores: room.getScores(),
+ nextGameId: 'game' + Math.floor(Math.random() * 10000)
+ }));
+ });
+ }
+ else {
+ room.broadcast(null, p.gameStartPacket({
+ type: p.gameStartPacket.typeId,
+ from: 0,
+ started: room.startTime()
+ }));
+ }
+ }
+
return user;
}
@@ -377,15 +408,15 @@ function createRoom(name) {
var scene = new Scene(255 * 4, 255 * 4);
var room = new Room(name, scene);
level.createLevel(scene);
- room.start();
+ room.startSyncing();
console.log('room "' + name + '" created [' + roomCount() + ']');
return room;
}
function destroyRoom(room) {
- room.stop();
+ room.stopSyncing();
ROOMS[room.name] = null;
console.log('room "' + room.name + '" destroyed! [' + roomCount() + ']');
@@ -421,14 +452,35 @@ bserver.on('connection', function(client) {
function joinRoom(data) {
var packet = p.objectifyPacket(data);
- if(typeof packet.room === 'string' &&
- packet.room !== '') {
- if(!ROOMS[packet.room]) {
- ROOMS[packet.room] = createRoom(packet.room);
+ if(typeof packet.room === 'string' && packet.room !== '') {
+ if(playerCount() > 1200) {
+ stream.write(p.cmdPacket({
+ type: p.cmdPacket.typeId,
+ from: 0,
+ method: 'fullRoom',
+ args: ['server']
+ }));
+ }
+ else {
+ var room = ROOMS[packet.room];
+
+ if(!room) {
+ room = ROOMS[packet.room] = createRoom(packet.room);
+ }
+
+ if(room.count() >= MAX_PER_ROOM) {
+ stream.write(p.cmdPacket({
+ type: p.cmdPacket.typeId,
+ from: 0,
+ method: 'fullRoom',
+ args: ['room']
+ }));
+ }
+ else {
+ stream.removeListener('data', joinRoom);
+ user = createUser(stream, ROOMS[packet.room]);
+ }
}
-
- stream.removeListener('data', joinRoom);
- user = createUser(stream, ROOMS[packet.room]);
}
}
View
@@ -4,13 +4,50 @@ function Room(name, scene) {
this.name = name;
this.scene = scene;
this.players = [];
+ this.started = null;
}
Room.prototype.start = function() {
+ this.started = Date.now();
+
+ var _this = this;
+ setTimeout(function() {
+ _this.end();
+ }, 60 * 5 * 1000);
+};
+
+Room.prototype.end = function() {
+ if(this._onEnd) {
+ this._onEnd();
+ }
+};
+
+Room.prototype.onEnd = function(func) {
+ this._onEnd = func;
+};
+
+Room.prototype.getScores = function() {
+ var scores = {};
+
+ for(var i=0, l=this.players.length; i<l; i++) {
+ var player = this.players[i];
+
+ scores[player.name] = { deaths: player.numDeaths,
+ kills: player.numKills };
+ }
+
+ return scores;
+};
+
+Room.prototype.startTime = function() {
+ return this.started;
+};
+
+Room.prototype.startSyncing = function() {
this.scene.start(this);
};
-Room.prototype.stop = function() {
+Room.prototype.stopSyncing = function() {
this.scene.stop(this);
};
View
@@ -55,7 +55,7 @@ canvas {
color: black;
}
-#intro button {
+button {
border: 1px solid black;
font-size: 1.5em;
color: white;
View
@@ -101,3 +101,94 @@
-webkit-transition: opacity 1s;
-moz-transition: opacity 1s;
}
+
+#timer {
+ position: absolute;
+ top: 0;
+ left: 0;
+ padding: 1em;
+
+ display: none;
+ font-size: 1.5em;
+ background-color: white;
+}
+
+.overlay {
+ position: absolute;
+ top: 0;
+ left: 0;
+ right: 0;
+ bottom: 0;
+}
+
+.overlay > div {
+ margin: 0 auto;
+ margin-top: 10em;
+ width: 30em;
+ padding: 2em;
+ border: 4px solid black;
+ background-color: white;
+}
+
+#scores {
+ display: none;
+}
+
+#scores h2 {
+ margin-top: 0;
+}
+
+#scores ul {
+ margin: 0;
+ padding: 0;
+ list-style: none;
+}
+
+#scores ul li {
+ margin: 1em 0;
+ padding: 5px 0;
+ border-bottom: 1px solid #d0d0d0;
+}
+
+#scores ul span {
+ display: inline-block;
+ width: 6em;
+}
+
+#scores ul span.total {
+ text-align: right;
+}
+
+#scores ul li.header {
+ font-weight: bold;
+ font-style: italic;
+}
+
+#scores .winner {
+ font-weight: bold;
+ font-size: 1.5em;
+ color: #339933;
+ text-align: center;
+}
+
+#scores .newgame {
+ margin-top: 2em;
+ text-align: center;
+ font-size: 1.1em;
+ font-style: italic;
+}
+
+#fullRoom {
+ position: absolute;
+ top: 0;
+ left: 0;
+ display: none;
+}
+
+#fullRoom .server, #fullRoom .room {
+ display: none;
+}
+
+#fullRoom button {
+ font-size: 1em;
+}
View
@@ -0,0 +1,106 @@
+
+var Game = sh.Obj.extend({
+ start: function(startTime) {
+ this.startTime = startTime;
+ messages.notify('Game has started!');
+
+ $('#timer').show();
+ this.updateTimer();
+
+ var _this = this;
+ this._interval = setInterval(function() { _this.updateTimer(); }, 1000);
+ },
+
+ end: function(obj) {
+ clearInterval(this._interval);
+ input.deactivate();
+
+ this.showScores(obj.scores);
+
+ var newgame = $('#scores .newgame');
+ var i = 9;
+ newgame.text('Next game starts in 10 seconds...');
+
+ var ival = setInterval(function() {
+ newgame.text('Next game starts in ' + i + ' seconds...');
+ i--;
+
+ if(i <= 0) {
+ clearInterval(ival);
+ window.location.href = '/' + obj.nextGameId;
+ }
+ }, 1000);
+ },
+
+ setFull: function(reason) {
+ this.full = reason;
+
+ $('#ingame .initialOverlay').hide();
+ input.deactivate();
+
+ var msg = $('#fullRoom');
+ msg.show();
+ msg.find('.' + reason).show();
+ },
+
+ isFull: function() {
+ return this.reason;
+ },
+
+ showScores: function(obj) {
+ var scores = $('#scores').show();
+ var ul = $('<ul>');
+ var sorted = [];
+
+ // Ruh roh
+ ul.append('<li class="header"><span>Name</span>' +
+ '<span>Kills</span><span>Deaths</span>' +
+ '<span class="total">Total</span></li>');
+
+ for(var k in obj) {
+ if(obj.hasOwnProperty(k)) {
+ var data = obj[k];
+ data.name = k;
+ sorted.push(data);
+ }
+ }
+
+ sorted.sort(function(v1, v2) {
+ return (v1.kills - v1.deaths) < (v2.kills - v2.deaths);
+ });
+
+ sorted.forEach(function(obj) {
+ var kills = obj.kills;
+ var deaths = obj.deaths;
+
+ ul.append('<li>' +
+ '<span>' + obj.name + '</span>' +
+ '<span>' + kills + '</span>' +
+ '<span>' + deaths + '</span>' +
+ '<span class="total">' + (kills - deaths) + '</span>' +
+ '</li>');
+ });
+
+ var container = scores.children('div');
+ container.find('.results').append(ul);
+ container.find('.winner span').text(sorted[0].name);
+ },
+
+ updateTimer: function() {
+ function pad(n) {
+ if(n < 10) {
+ return '0' + n;
+ }
+ else {
+ return n;
+ }
+ }
+
+ var timer = $('#timer');
+ var now = Date.now();
+ var elapsed = Math.floor((now - this.startTime) / 1000.0);
+ var left = (5 * 60) - elapsed;
+
+ timer.text(Math.floor(left / 60.0) + ':' + pad(left % 60));
+ }
+});
@@ -1,10 +1,10 @@
$(function() {
- $('#intro button.play-start').click(function() {
+ $('button.play-start').click(function() {
var name = 'game' + Math.floor(Math.random() * 100000);
window.location.href = '/' + name;
});
- $('#intro button.play-find').click(function() {
+ $('button.play-find').click(function() {
window.location.href = '/find';
});
Oops, something went wrong.

0 comments on commit d286296

Please sign in to comment.